Skip to content

Commit 62c4f5e

Browse files
authoredApr 12, 2024··
fix(es/plugin): Create tokio runtime only if necessary (#8845)
**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.
1 parent 187ceb1 commit 62c4f5e

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed
 

‎crates/swc/src/plugin.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,19 @@ impl RustPlugins {
5555
return Ok(n);
5656
}
5757

58-
tokio::runtime::Runtime::new()
59-
.unwrap()
60-
.block_on(async move {
61-
self.apply_inner(n).with_context(|| {
62-
format!(
63-
"failed to invoke plugin on '{:?}'",
64-
self.metadata_context.filename
65-
)
66-
})
58+
let fut = async move {
59+
self.apply_inner(n).with_context(|| {
60+
format!(
61+
"failed to invoke plugin on '{:?}'",
62+
self.metadata_context.filename
63+
)
6764
})
65+
};
66+
if let Ok(handle) = tokio::runtime::Handle::try_current() {
67+
handle.block_on(fut)
68+
} else {
69+
tokio::runtime::Runtime::new().unwrap().block_on(fut)
70+
}
6871
}
6972

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

0 commit comments

Comments
 (0)
Please sign in to comment.