Skip to content

Commit e106158

Browse files
authored
fix(core): remove hang risk caused by coop budget in tokio (#8434)
1 parent bd9b7a0 commit e106158

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

crates/rspack_core/src/compiler/module_executor/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use tokio::sync::{
1212
mpsc::{unbounded_channel, UnboundedSender},
1313
oneshot,
1414
};
15+
use tokio::task;
1516

1617
use self::{
1718
ctrl::{CtrlTask, Event, ExecuteParam},
@@ -72,8 +73,9 @@ impl ModuleExecutor {
7273
let (stop_sender, stop_receiver) = oneshot::channel();
7374
self.event_sender = Some(event_sender.clone());
7475
self.stop_receiver = Some(stop_receiver);
75-
76-
tokio::spawn(async move {
76+
// avoid coop budget consumed to zero cause hang risk
77+
// related to https://tokio.rs/blog/2020-04-preemption
78+
tokio::spawn(task::unconstrained(async move {
7779
let _ = run_task_loop_with_event(
7880
&mut ctx,
7981
vec![Box::new(CtrlTask::new(event_receiver))],
@@ -89,7 +91,7 @@ impl ModuleExecutor {
8991
stop_sender
9092
.send(ctx.transform_to_make_artifact())
9193
.expect("should success");
92-
});
94+
}));
9395
}
9496

9597
pub async fn hook_after_finish_modules(&mut self, compilation: &mut Compilation) {

crates/rspack_core/src/utils/task_loop.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use std::{
1010

1111
use rspack_error::Result;
1212
use rspack_util::ext::AsAny;
13-
use tokio::sync::mpsc::{self, error::TryRecvError};
13+
use tokio::{
14+
sync::mpsc::{self, error::TryRecvError},
15+
task,
16+
};
1417

1518
/// Result returned by task
1619
///
@@ -81,12 +84,12 @@ pub async fn run_task_loop_with_event<Ctx: 'static>(
8184
let tx = tx.clone();
8285
let is_expected_shutdown = is_expected_shutdown.clone();
8386
active_task_count += 1;
84-
tokio::spawn(async move {
87+
tokio::spawn(task::unconstrained(async move {
8588
let r = task.background_run().await;
8689
if !is_expected_shutdown.load(Ordering::Relaxed) {
8790
tx.send(r).expect("failed to send error message");
8891
}
89-
});
92+
}));
9093
}
9194
TaskType::Sync => {
9295
// merge sync task result directly

0 commit comments

Comments
 (0)