Skip to content

Commit

Permalink
fix(es/plugin): Create tokio runtime only if necessary (#8845)
Browse files Browse the repository at this point in the history
**Description:**

Create tokio runtime if it's not in an async context.

**Related issue:**

 - web-infra-dev/rspack#6209

This maybe related to this issue, since creating new tokio runtime consumes a lot of system resource.
  • Loading branch information
h-a-n-a authored Apr 12, 2024
1 parent 187ceb1 commit 62c4f5e
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions crates/swc/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,19 @@ impl RustPlugins {
return Ok(n);
}

tokio::runtime::Runtime::new()
.unwrap()
.block_on(async move {
self.apply_inner(n).with_context(|| {
format!(
"failed to invoke plugin on '{:?}'",
self.metadata_context.filename
)
})
let fut = async move {
self.apply_inner(n).with_context(|| {
format!(
"failed to invoke plugin on '{:?}'",
self.metadata_context.filename
)
})
};
if let Ok(handle) = tokio::runtime::Handle::try_current() {
handle.block_on(fut)
} else {
tokio::runtime::Runtime::new().unwrap().block_on(fut)
}
}

#[tracing::instrument(level = "info", skip_all, name = "apply_plugins")]
Expand Down

0 comments on commit 62c4f5e

Please sign in to comment.