Skip to content

fix(server): run the graceful shutdown on SIGTERM (R-13, S-43) - #39

Merged
TurkerMertkan merged 2 commits into
developfrom
s43-sigterm
Nov 24, 2026
Merged

TurkerMertkan merged 2 commits into
developfrom
s43-sigterm

Conversation

@TurkerMertkan

Copy link
Copy Markdown
Collaborator

Summary

  • The server only listened for SIGINT; systemctl stop/restart and deploys send SIGTERM, so the process died without its shutdown (poll wait, SVM stop, HAL shutdown, /dev/watchdog magic close, socket/PID cleanup, log flush) (R-13).
  • SIGTERM listener installed first in main (a SIGTERM during startup stays pending), new select! branch, engine stopped names the signal.
  • Shutdown hardening from review: STOPPING=1, watchdog kick after the poll wait, exit after cleanup if the poll is stuck (tokio runtime drop would wait forever), visible errors for magic close, socket and PID file removal, no false signal on a closed stream.
  • Unit file unchanged.

Behaviour change

  • Every stop/restart runs the shutdown: up to 5 s poll wait plus SVM stop.
  • With the kernel watchdog enabled (R-42, off in the field), stopping the service no longer resets the board.

Test plan

  • bash tools/gates.sh: fmt, clippy, test (2921 passed, 0 failed), hal, docs
  • Linux CI: tests/sigterm.rs (SIGTERM after and during startup; exit 0, engine stopped with SIGTERM, PID file and socket removed). Unix only, not compiled on Windows.

Docs: DEPLOYMENT_CHECKLIST.md §7, .claude/rules/engine-async.md.

🤖 Generated with Claude Code

https://claude.ai/code/session_01QKRNiek8qYRoRt7dcrDDVC

TurkerMertkan and others added 2 commits November 24, 2026 14:38
The server only listened for SIGINT. systemctl stop/restart and the
deploy scripts send SIGTERM, so the process died at once: no poll wait,
no SVM stop, no HAL shutdown, no /dev/watchdog magic close, no IPC
socket or PID file cleanup, and the last log lines could be lost.

- signal.rs: TermSignal (SIGTERM on Unix, never fires elsewhere). A
  closed signal stream is logged and no longer reported as a signal
  (also for SIGHUP).
- main.rs: the SIGTERM listener is installed first, before the PID file,
  /dev/watchdog and the IPC socket exist; a SIGTERM during startup stays
  pending and the first loop turn shuts down. select! gets a SIGTERM
  branch; "engine stopped" carries reason=SIGTERM/SIGINT/IPC command.
  Shutdown sends STOPPING=1 and kicks both watchdogs after the poll
  wait. If the poll does not come back in 5 s, the HAL shutdown skip is
  logged and the process exits after cleanup, because dropping the tokio
  runtime would wait for the stuck blocking task until systemd's SIGKILL.
- watchdog.rs: a failed magic close is a warning, not "closed".
- ipc.rs: socket cleanup warns on errors other than NotFound.
- pid.rs: a failed PID file removal goes to the log, not only stderr.

Behaviour: every stop/restart now runs the shutdown (up to 5 s poll
wait plus SVM stop), so a restart can take a few seconds longer. If the
kernel watchdog is enabled (R-42, off in the field), stopping the service
no longer resets the board. Unit file unchanged.

Tests: gates.sh fmt, clippy, test (29 suites, 2921 passed, 0 failed),
hal, docs OK. tests/sigterm.rs (Unix only, first run on Linux CI): the
real binary gets SIGTERM after startup and during startup; exit 0,
"engine stopped" with SIGTERM, PID file and socket removed. Both fail on
the old code (death by signal).

docs: DEPLOYMENT_CHECKLIST.md section 7 stop log lines;
.claude/rules/engine-async.md shutdown note.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QKRNiek8qYRoRt7dcrDDVC
Linux CI failed both SIGTERM tests: they captured only stderr, but
without --log-file the tracing logs go to stdout (tracing_subscriber::fmt
defaults to io::stdout, tracing-subscriber 0.3.22 fmt/mod.rs:232), so
"engine running" and "engine stopped" never showed up. The startup test
already saw exit 0. The tests now read stdout and stderr into one list.

No behavior change. The logging.rs comment "stderr only" for this branch
is wrong; left as is (systemd captures both, the unit uses --log-file).

Tests: Unix only, not compiled on Windows; Linux CI is the check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QKRNiek8qYRoRt7dcrDDVC
@TurkerMertkan
TurkerMertkan merged commit f8966ab into develop Nov 24, 2026
2 checks passed
@TurkerMertkan
TurkerMertkan deleted the s43-sigterm branch November 24, 2026 12:04
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