Skip to content

fix(cli): install the shutdown signal handlers before serving - #9073

Merged
kixelated merged 2 commits into
releasefrom
fix/cli-signal-before-serve
Nov 18, 2026
Merged

kixelated merged 2 commits into
releasefrom
fix/cli-signal-before-serve

Conversation

@kixelated

@kixelated kixelated commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

On release, the CLI can die from a SIGTERM sent right after its listener comes up. drive spawns shutdown_signal() as a task, and tokio installs a signal's handler only when that future is first polled. A listener can bind and log "listening" before the task runs, so a SIGTERM in that window takes the default action and kills the process instead of shutting it down. rs/moq-cli/tests/stdin_shutdown.rs hits this under load:

thread 'a_stopped_import_exits_with_its_stdin_writer_still_open' panicked at rs/moq-cli/tests/stdin_shutdown.rs:71:5:
CLI exited with signal: 15 (SIGTERM)

One re-run passed. It failed inside a Nix build of moq-cli, which runs the test suite, so on release the flake also fails any build that rebuilds the CLI.

main fixed the same race in #5575 ("catch shutdown signals before serving") as part of a larger signal rework. This is the release-line minimum.

Changes

rs/moq-cli/src/main.rs:

  • shutdown_signal is now a plain function. It installs the SIGINT and SIGTERM handlers (Ctrl-C on Windows) when called and returns the future that waits on them.
  • run_stages spawns it before spawn_moq binds the stage listeners, and drive no longer spawns it.
  • New unit test a_signal_before_the_first_poll_is_caught: it makes the future, sends the process a SIGTERM before polling it, and expects the future to see the signal.

Validation

  • The new test passes here. With registration moved back into the future (the release behaviour), the test binary is killed by the SIGTERM (exit 143).
  • sh/rs/select.sh check and sh/rs/select.sh test over this diff: clean, 257 tests pass.
  • Not run here: the Windows build. CI's platform lane covers it.

Impact

  • Wire and public API: none.
  • Behaviour: a SIGINT or SIGTERM arriving during startup now shuts the CLI down cleanly instead of killing it. moq play is unchanged; it has its own Ctrl-C handling.

Caused by

#4287 (close the relay connection on SIGINT and SIGTERM) spawned the handler as a task in drive. main replaced that in #5575, which was never backported.

Models

Size: S
Implementer: Claude Code, claude-opus-5-5, unknown effort

(Written by Claude Opus 5.5)

🤖 Generated with Claude Code

Review

Rewritten by quest review after each round.

  • Head: ffbf34d

  • Base: origin/release at 2ab31a1

  • Risk: medium, the base has no RISK.md to name what is always high or customer-facing, so every change is medium

  • Classified: medium by default

  • Reviewer: Codex, gpt-6.1-sol, medium effort

  • Session: 01a1ed01-887e-7f63-8360-cbab5579df41, round 2

  • Score: 9/10, The only change since the last review clarifies a comment accurately and introduces no actionable defects; builds and tests were not run as instructed.

  • Rounds: 2; first round 0; findings 0: fixed 0, refuted 0, open 0

  • Findings: none

  • Reviewer: Codex, gpt-6.1-sol, low effort, shadow

  • Session: 01a1ed01-887e-77d0-96f3-ff6b12c7943f

  • Rounds: 1; first round 0; findings 0: fixed 0, refuted 0, open 0

`drive` spawned `shutdown_signal()` as a task, and tokio installs a
signal's handler on that future's first poll. A listener could bind and
log before the task ran, so a SIGTERM sent right after took the default
action and killed the process. `stdin_shutdown` hit this under load
("CLI exited with signal: 15 (SIGTERM)").

`shutdown_signal` now installs both handlers when called, and
`run_stages` calls it before anything binds. `main` fixed the same race
in #5575 with a larger rework; this is the release-line minimum.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ One minor suggestion inline: the new comment claims more scope than the change covers.

Approach: sound. The fix rests on the real difference between tokio::signal::unix::signal, which installs the OS handler when it is called, and tokio::signal::ctrl_c, which is an async fn and therefore installs on first poll. Creating the future in run_stages before spawn_moq closes the window that let a SIGTERM sent right after "listening" kill the process. Spawning it there rather than globally is also the right call: moq play only catches SIGINT, so installing a SIGTERM handler on that path would make playback unkillable by a supervisor.

Reviewed changes

  • Handlers installed eagerly: shutdown_signal is now a sync fn that creates the SIGTERM and SIGINT Signals (Ctrl-C on Windows) and returns the future that waits on them, so registration happens at construction. This is the whole behavioral fix.
  • Spawned before any stage binds: run_stages spawns that future into its JoinSet ahead of spawn_moq, and drive drops its own spawn. drive's only other callers are the unit tests, so no path loses the handler.
  • Regression test: a_signal_before_the_first_poll_is_caught builds the future, sends the process a SIGTERM, then awaits it under a timeout. With lazy registration the test binary is killed by the signal, so it fails for the right reason.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash | 𝕏

Comment thread rs/moq-cli/src/main.rs Resolved
… bind

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated
kixelated enabled auto-merge November 18, 2026 05:02

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

  • Comment scoped accurately: run_stages now says the handlers precede "the stage listeners" rather than "anything binds", which matches that --iroh-enabled binds its endpoint in Net earlier. This is the only change since the prior review, and it addresses the earlier inline suggestion; that thread is resolved.

(Written by DeepSeek V4.1 Flash)

Pullfrog  | View workflow run | Using deepseek-v4.1-flash | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

  • Nothing new since the prior approval: ffbf34d21 is still the head, so the only delta relative to the last review is the comment scope fix it already covered. run_stages says the handlers precede "the stage listeners", which is accurate given that --iroh-enabled binds its endpoint in Net earlier, and the single prior inline thread is resolved.
  • Fix still verified in source: shutdown_signal() installs the SIGTERM and SIGINT Signals when called and returns the waiting future, and run_stages spawns it before spawn_moq binds any stage listener. drive holds only the join loop, and moq play keeps its own SIGINT-only handling.

(Written by DeepSeek V4.1 Flash)

Pullfrog  | View workflow run | Using deepseek-v4.1-flash | 𝕏

@kixelated
kixelated merged commit df1562c into release Nov 18, 2026
21 checks passed
@kixelated
kixelated deleted the fix/cli-signal-before-serve branch November 18, 2026 05:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant