diff --git a/crates/rspack_core/src/compiler/make/repair/add.rs b/crates/rspack_core/src/compiler/make/repair/add.rs index 8d98c3af939..42a4c8b4c98 100644 --- a/crates/rspack_core/src/compiler/make/repair/add.rs +++ b/crates/rspack_core/src/compiler/make/repair/add.rs @@ -17,6 +17,9 @@ pub struct AddTask { } impl Task for AddTask { + fn name(&self) -> &'static str { + "add_task" + } fn get_task_type(&self) -> TaskType { TaskType::Sync } diff --git a/crates/rspack_core/src/compiler/make/repair/build.rs b/crates/rspack_core/src/compiler/make/repair/build.rs index edbc12c3550..fb6a9f76e30 100644 --- a/crates/rspack_core/src/compiler/make/repair/build.rs +++ b/crates/rspack_core/src/compiler/make/repair/build.rs @@ -27,6 +27,9 @@ pub struct BuildTask { #[async_trait::async_trait] impl Task for BuildTask { + fn name(&self) -> &'static str { + "build_task" + } fn get_task_type(&self) -> TaskType { TaskType::Async } @@ -106,6 +109,9 @@ struct BuildResultTask { } impl Task for BuildResultTask { + fn name(&self) -> &'static str { + "build_result" + } fn get_task_type(&self) -> TaskType { TaskType::Sync } diff --git a/crates/rspack_core/src/compiler/make/repair/factorize.rs b/crates/rspack_core/src/compiler/make/repair/factorize.rs index a86e7ef11aa..034d9cf4be5 100644 --- a/crates/rspack_core/src/compiler/make/repair/factorize.rs +++ b/crates/rspack_core/src/compiler/make/repair/factorize.rs @@ -29,6 +29,9 @@ pub struct FactorizeTask { #[async_trait::async_trait] impl Task for FactorizeTask { + fn name(&self) -> &'static str { + "factorize_task" + } fn get_task_type(&self) -> TaskType { TaskType::Async } @@ -198,6 +201,9 @@ impl FactorizeResultTask { } impl Task for FactorizeResultTask { + fn name(&self) -> &'static str { + "factorize_result" + } fn get_task_type(&self) -> TaskType { TaskType::Sync } diff --git a/crates/rspack_core/src/compiler/make/repair/process_dependencies.rs b/crates/rspack_core/src/compiler/make/repair/process_dependencies.rs index 818633f3632..55f14ce9668 100644 --- a/crates/rspack_core/src/compiler/make/repair/process_dependencies.rs +++ b/crates/rspack_core/src/compiler/make/repair/process_dependencies.rs @@ -15,6 +15,9 @@ pub struct ProcessDependenciesTask { } impl Task for ProcessDependenciesTask { + fn name(&self) -> &'static str { + "process_dependencies_task" + } fn get_task_type(&self) -> TaskType { TaskType::Sync } diff --git a/crates/rspack_core/src/compiler/module_executor/ctrl.rs b/crates/rspack_core/src/compiler/module_executor/ctrl.rs index 65ad175eb62..2bc407cabe0 100644 --- a/crates/rspack_core/src/compiler/module_executor/ctrl.rs +++ b/crates/rspack_core/src/compiler/module_executor/ctrl.rs @@ -80,6 +80,9 @@ impl CtrlTask { #[async_trait::async_trait] impl Task for CtrlTask { + fn name(&self) -> &'static str { + "control_task" + } fn get_task_type(&self) -> TaskType { TaskType::Async } @@ -162,6 +165,9 @@ struct FinishModuleTask { } impl Task for FinishModuleTask { + fn name(&self) -> &'static str { + return "finish_module_task"; + } fn get_task_type(&self) -> TaskType { TaskType::Sync } diff --git a/crates/rspack_core/src/compiler/module_executor/entry.rs b/crates/rspack_core/src/compiler/module_executor/entry.rs index 29239ad78ac..4a0e4823c88 100644 --- a/crates/rspack_core/src/compiler/module_executor/entry.rs +++ b/crates/rspack_core/src/compiler/module_executor/entry.rs @@ -19,6 +19,9 @@ pub struct EntryTask { } impl Task for EntryTask { + fn name(&self) -> &'static str { + "entry_task" + } fn get_task_type(&self) -> TaskType { TaskType::Sync } diff --git a/crates/rspack_core/src/compiler/module_executor/execute.rs b/crates/rspack_core/src/compiler/module_executor/execute.rs index 31f78c79c94..377c86efd4d 100644 --- a/crates/rspack_core/src/compiler/module_executor/execute.rs +++ b/crates/rspack_core/src/compiler/module_executor/execute.rs @@ -56,6 +56,9 @@ pub struct ExecuteTask { } impl Task for ExecuteTask { + fn name(&self) -> &'static str { + "execute_task" + } fn get_task_type(&self) -> TaskType { TaskType::Sync } diff --git a/crates/rspack_core/src/compiler/module_executor/overwrite.rs b/crates/rspack_core/src/compiler/module_executor/overwrite.rs index 362820e80e6..faafbc24ad6 100644 --- a/crates/rspack_core/src/compiler/module_executor/overwrite.rs +++ b/crates/rspack_core/src/compiler/module_executor/overwrite.rs @@ -16,6 +16,9 @@ pub struct OverwriteTask { #[async_trait::async_trait] impl Task for OverwriteTask { + fn name(&self) -> &'static str { + "overwrite_task" + } fn get_task_type(&self) -> TaskType { self.origin_task.get_task_type() } diff --git a/crates/rspack_core/src/utils/task_loop.rs b/crates/rspack_core/src/utils/task_loop.rs index 17357deeb9d..3b9b0aa1629 100644 --- a/crates/rspack_core/src/utils/task_loop.rs +++ b/crates/rspack_core/src/utils/task_loop.rs @@ -37,7 +37,8 @@ pub trait Task: Send + Any + AsAny { /// Return `TaskType::Sync` will run `self::sync_run` /// Return `TaskType::Async` will run `self::async_run` fn get_task_type(&self) -> TaskType; - + /// get task name for monitor + fn name(&self) -> &'static str; /// Sync task process /// /// The context is shared with all tasks @@ -85,12 +86,16 @@ pub fn run_task_loop_with_event( let tx = tx.clone(); let is_expected_shutdown = is_expected_shutdown.clone(); active_task_count += 1; - tokio::spawn(async move { - let r = task.async_run().await; - if !is_expected_shutdown.load(Ordering::Relaxed) { - tx.send(r).expect("failed to send error message"); - } - }); + // safe expect here since spawn always return Ok + tokio::task::Builder::new() + .name(task.name()) + .spawn(async move { + let r = task.async_run().await; + if !is_expected_shutdown.load(Ordering::Relaxed) { + tx.send(r).expect("failed to send error message"); + } + }) + .expect("spawn task failed"); } TaskType::Sync => { // merge sync task result directly @@ -150,6 +155,9 @@ mod test { struct SyncTask; impl Task for SyncTask { + fn name(&self) -> &'static str { + "sync_task" + } fn get_task_type(&self) -> TaskType { TaskType::Sync } @@ -175,6 +183,9 @@ mod test { } #[async_trait::async_trait] impl Task for AsyncTask { + fn name(&self) -> &'static str { + "async_task" + } fn get_task_type(&self) -> TaskType { TaskType::Async }