GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(cli-rs): exit 143 after SIGTERM · anomalyco/opencode@6431b26

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/cli-rs
    • crates/cli/src
      • signals.rs
    • parity/deviations
      • G16-shutdown.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/cli-rs/crates/cli/src/signals.rs‎

Copy file name to clipboardExpand all lines: packages/cli-rs/crates/cli/src/signals.rs
+20-6Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Shutdown deadline and SIGHUP (`packages/cli/src/shutdown-deadline.ts`, `index.ts:102`).
22
//!
33
//! 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
55
//! signal, it prints what is still pending to stderr and exits with 1. With a terminal on stdin or stdout,
66
//! SIGHUP counts as SIGTERM; without one (nohup, the background service) SIGHUP keeps its default, so nohup's
77
//! ignore still holds. While the TUI runs, opentui owns these signals and quits the app itself.
@@ -75,11 +75,16 @@ pub fn wait_child(
7575
*lock(&exited.0) = true;
7676
exited.1.notify_all();
7777
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() {
7985
while SHUTDOWN.get().is_some_and(|shutdown| shutdown.started.load(Ordering::SeqCst)) {
8086
std::thread::park();
8187
}
82-
status
8388
}
8489

8590
#[cfg(unix)]
@@ -197,6 +202,8 @@ impl Shutdown {
197202
std::mem::take(&mut *lock(&self.steps)).into_iter().rev().map(|(_, label, step)| (label, step)).collect();
198203
*lock(&self.queue) = steps.iter().map(|(label, _)| (label.clone(), None)).collect();
199204
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 };
200207
let _ = std::thread::Builder::new().name("opencode-shutdown".into()).spawn(move || {
201208
for (_, step) in steps {
202209
if let Some(first) = lock(&teardown.queue).first_mut() {
@@ -206,7 +213,7 @@ impl Shutdown {
206213
lock(&teardown.queue).remove(0);
207214
}
208215
if !teardown.done.swap(true, Ordering::SeqCst) {
209-
(teardown.exit)(130);
216+
(teardown.exit)(code);
210217
}
211218
});
212219
let timer = self.clone();
@@ -304,7 +311,7 @@ mod tests {
304311
}
305312

306313
#[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() {
308315
let probe = probe(Duration::from_secs(10));
309316
let order = Arc::new(Mutex::new(Vec::new()));
310317
let step = |name: &'static str| {
@@ -316,11 +323,18 @@ mod tests {
316323
probe.shutdown.register("last".into(), step("last"));
317324
probe.shutdown.unregister(removed);
318325
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));
320327
assert_eq!(*order.lock().unwrap(), ["last", "first"]);
321328
assert_eq!(text(&probe), "");
322329
}
323330

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+
324338
#[test]
325339
fn a_stuck_step_is_reported_at_the_deadline() {
326340
let probe = probe(Duration::from_millis(100));
Collapse file

‎packages/cli-rs/parity/deviations/G16-shutdown.md‎

Copy file name to clipboard
+7-6Lines changed: 7 additions & 6 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# G16: shutdown deadline
22

3-
TypeScript: SIGTERM or SIGINT interrupts the main fiber, so every finalizer runs. The exit code is 130. The
4-
deadline reports the `Teardown.pending()` items of the LayerNode layers.
3+
TypeScript: SIGTERM or SIGINT interrupts the main fiber, so every finalizer runs. The exit code is 130, or 143
4+
after SIGTERM. The deadline reports the `Teardown.pending()` items of the LayerNode layers.
55

66
Rust: a signal runs only the steps that commands register with `signals::on_shutdown`, newest first, then exits
7-
with 130. The handler's own drops do not run. The report lists those steps. If no step is registered, the line is
8-
"nothing tracked; the stuck work is not a registered teardown step". The deadline, the second-signal exit, and the
9-
SIGHUP rule (only with a terminal on stdin or stdout) match TS. While the TUI runs, opentui handles the three
10-
signals and quits the app, so the TUI exits through its normal path with code 0.
7+
with the same code. `run` registers a step that cancels its Session and jobs and waits for that. The handler's own
8+
drops do not run. The report lists those steps. If no step is registered, the line is "nothing tracked; the stuck
9+
work is not a registered teardown step". The deadline, the second-signal exit, and the SIGHUP rule (only with a
10+
terminal on stdin or stdout) match TS. While the TUI runs, opentui handles the three signals and quits the app, so
11+
the TUI exits through its normal path with code 0.
1112

1213
Reason: Rust commands block their thread and have no interruptible fiber. A standalone server needs no step: its
1314
stdin lease ends when the client exits.

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.