|
1 | 1 | //! Shutdown deadline and SIGHUP (`packages/cli/src/shutdown-deadline.ts`, `index.ts:102`). |
2 | 2 | //! |
3 | 3 | //! On SIGTERM or SIGINT the CLI runs its registered teardown steps (`on_shutdown`), newest first, and exits |
4 | | -//! with 130, as Effect's `runMain` does after an interrupt. Teardown gets 10 s; after that, or on a second |
| 4 | +//! with 130 after SIGINT and 143 after SIGTERM, as the TS `runMain` teardown does. Teardown gets 10 s; after that, or on a second |
5 | 5 | //! signal, it prints what is still pending to stderr and exits with 1. With a terminal on stdin or stdout, |
6 | 6 | //! SIGHUP counts as SIGTERM; without one (nohup, the background service) SIGHUP keeps its default, so nohup's |
7 | 7 | //! ignore still holds. While the TUI runs, opentui owns these signals and quits the app itself. |
@@ -75,11 +75,16 @@ pub fn wait_child( |
75 | 75 | *lock(&exited.0) = true; |
76 | 76 | exited.1.notify_all(); |
77 | 77 | drop(registration); |
78 | | - // Teardown owns the exit once it started; returning would race it with the command's own exit code. |
| 78 | + park_during_shutdown(); |
| 79 | + status |
| 80 | +} |
| 81 | + |
| 82 | +/// Teardown owns the exit once it started; a command that returns would race it with its own exit code, so |
| 83 | +/// this parks the thread instead. |
| 84 | +pub fn park_during_shutdown() { |
79 | 85 | while SHUTDOWN.get().is_some_and(|shutdown| shutdown.started.load(Ordering::SeqCst)) { |
80 | 86 | std::thread::park(); |
81 | 87 | } |
82 | | - status |
83 | 88 | } |
84 | 89 |
|
85 | 90 | #[cfg(unix)] |
@@ -197,6 +202,8 @@ impl Shutdown { |
197 | 202 | std::mem::take(&mut *lock(&self.steps)).into_iter().rev().map(|(_, label, step)| (label, step)).collect(); |
198 | 203 | *lock(&self.queue) = steps.iter().map(|(label, _)| (label.clone(), None)).collect(); |
199 | 204 | let teardown = self.clone(); |
| 205 | + // `runMain` reports every signal interruption as 130; the CLI turns a SIGTERM into 143. |
| 206 | + let code = if name == "SIGTERM" { 143 } else { 130 }; |
200 | 207 | let _ = std::thread::Builder::new().name("opencode-shutdown".into()).spawn(move || { |
201 | 208 | for (_, step) in steps { |
202 | 209 | if let Some(first) = lock(&teardown.queue).first_mut() { |
@@ -206,7 +213,7 @@ impl Shutdown { |
206 | 213 | lock(&teardown.queue).remove(0); |
207 | 214 | } |
208 | 215 | if !teardown.done.swap(true, Ordering::SeqCst) { |
209 | | - (teardown.exit)(130); |
| 216 | + (teardown.exit)(code); |
210 | 217 | } |
211 | 218 | }); |
212 | 219 | let timer = self.clone(); |
@@ -304,7 +311,7 @@ mod tests { |
304 | 311 | } |
305 | 312 |
|
306 | 313 | #[test] |
307 | | - fn a_signal_runs_the_steps_newest_first_and_exits_130() { |
| 314 | + fn a_signal_runs_the_steps_newest_first_and_exits_143_after_sigterm() { |
308 | 315 | let probe = probe(Duration::from_secs(10)); |
309 | 316 | let order = Arc::new(Mutex::new(Vec::new())); |
310 | 317 | let step = |name: &'static str| { |
@@ -316,11 +323,18 @@ mod tests { |
316 | 323 | probe.shutdown.register("last".into(), step("last")); |
317 | 324 | probe.shutdown.unregister(removed); |
318 | 325 | probe.shutdown.signal("SIGTERM"); |
319 | | - assert_eq!(probe.exits.recv_timeout(Duration::from_secs(5)), Ok(130)); |
| 326 | + assert_eq!(probe.exits.recv_timeout(Duration::from_secs(5)), Ok(143)); |
320 | 327 | assert_eq!(*order.lock().unwrap(), ["last", "first"]); |
321 | 328 | assert_eq!(text(&probe), ""); |
322 | 329 | } |
323 | 330 |
|
| 331 | + #[test] |
| 332 | + fn sigint_exits_130() { |
| 333 | + let probe = probe(Duration::from_secs(10)); |
| 334 | + probe.shutdown.signal("SIGINT"); |
| 335 | + assert_eq!(probe.exits.recv_timeout(Duration::from_secs(5)), Ok(130)); |
| 336 | + } |
| 337 | + |
324 | 338 | #[test] |
325 | 339 | fn a_stuck_step_is_reported_at_the_deadline() { |
326 | 340 | let probe = probe(Duration::from_millis(100)); |
|