From 78f282007980fe9c9ef143a6bc6fb76282957ab6 Mon Sep 17 00:00:00 2001 From: Weicao-CatilGrass <1992414357@qq.com> Date: Tue, 9 Jun 2026 06:20:21 +0800 Subject: Replace `OnceLock>>` with custom `ProgramCell` type Reduces indirection and allows taking ownership of the program instance for proper cleanup before `process::exit()` --- mingling_core/src/program/once_exec.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'mingling_core/src/program/once_exec.rs') diff --git a/mingling_core/src/program/once_exec.rs b/mingling_core/src/program/once_exec.rs index f6eb0c0..fe90784 100644 --- a/mingling_core/src/program/once_exec.rs +++ b/mingling_core/src/program/once_exec.rs @@ -13,11 +13,9 @@ where F: FnOnce(&'static Program) -> Fut + Send + Sync, Fut: Future + Send, { - THIS_PROGRAM.get_or_init(|| Some(Box::new(self))); + THIS_PROGRAM.set(Box::new(self)); let program = THIS_PROGRAM - .get() - .unwrap() - .as_ref() + .get_raw() .unwrap() .downcast_ref::>() .unwrap(); @@ -100,7 +98,12 @@ where where C: 'static + Send + Sync, { - std::process::exit(self.exec().await) + let exit_code = self.exec().await; + // SAFETY: exec() is synchronous — it returns only after all + // chain handlers and renderers have finished. No code still + // holds references from get_raw() at this point. + drop(unsafe { THIS_PROGRAM.take() }); + std::process::exit(exit_code) } } @@ -115,11 +118,9 @@ where C: 'static + Send + Sync, F: FnOnce(&'static Program) -> R + Send + Sync, { - THIS_PROGRAM.get_or_init(|| Some(Box::new(self))); + THIS_PROGRAM.set(Box::new(self)); let program = THIS_PROGRAM - .get() - .unwrap() - .as_ref() + .get_raw() .unwrap() .downcast_ref::>() .unwrap(); @@ -165,9 +166,7 @@ where }; let program = THIS_PROGRAM - .get() - .unwrap() - .as_ref() + .get_raw() .unwrap() .downcast_ref::>() .unwrap(); @@ -232,6 +231,11 @@ where where C: 'static + Send + Sync, { - std::process::exit(self.exec()) + let exit_code = self.exec(); + // SAFETY: exec() is synchronous — it returns only after all + // chain handlers and renderers have finished. No code still + // holds references from get_raw() at this point. + drop(unsafe { THIS_PROGRAM.take() }); + std::process::exit(exit_code) } } -- cgit