Repository navigation
End a page server stopped by a signal through exit_serving - #3452
Merged
Merged
Conversation
A page server stopped by Ctrl-C, SIGTERM or SIGHUP now ends through `exit_serving`, as `leaf server stop` does, so every stop has one exit path. Before, Python unwound normally: the lease was released while the row publisher and the lifetime check still ran, which left them time to overwrite a successor's row or start a title App Server after the exit handlers had stopped them. `exit_serving` stops title App Servers itself. A foreground `leaf server run` stopped with Ctrl-C now exits with status 130 and no "interrupted" error; SIGTERM keeps 143. A temporary server holds no lease and still unwinds. With no signal stop reaching interpreter exit, #3346's exit-time skip of the page subscription's finalizer is unreachable and goes. Max's decision #34 on the overnight report. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
leaf-agent
approved these changes
Oct 21, 2026
max-sixty
added a commit
that referenced
this pull request
Oct 21, 2026
A page server now installs its `exit_serving` signal handlers as its service commits, before its URL leaves the process. Before this change it installed them only after the URL was printed. This is a follow-up to #3452 (Max's decision #34 on the overnight report). **The bug.** #3452 installed the handlers just before the server started its threads, which comes after `server run` prints the URL. Ctrl-C sent as soon as the URL appeared could land in between. It then took the old unwinding path and exited 1 with `{"error": "interrupted"}` instead of 130. `test_server_run_persistent_declines_the_claim_a_harness_session_offers` sends SIGINT as soon as it reads the URL line, and it failed this way on CI in #3455, #3459 and #3460 ([run](https://github.com/max-sixty/leaf/actions/runs/39568649930/job/119117041536)). A user pressing Ctrl-C right after the URL appears hits the same window. **The fix.** `cmd_serve`'s `commit()` installs the handlers once the service is committed and before it returns. Both paths that hand the URL out run after `commit()`: `server run` prints it, and `_serve` sends the handshake confirmation that `server start` prints. A signal during the commit still unwinds, so a failed commit still rolls the service back. **Checked.** A temporary test (not committed) started `leaf server run --persistent`, read the URL line and signaled at once. With a 0.05 s sleep injected right after the URL is printed: | Version | SIGINT trials | Exited 130 | SIGTERM trials | Exited 143 | |---|---|---|---|---| | main | 100 | 0 (all exited 1, "interrupted") | 100 | 100 | | this change | 100 | 100 | 100 | 100 | SIGTERM's status doesn't show which path it took, since the unwinding path also exits 143. Without the injected sleep, 100 trials of each signal passed on both versions on a lightly loaded Mac; the failure needs CI's scheduling delay. The focused server lifetime, server process, title and preview handoff tests pass (193). > _This was written by Claude Code on behalf of max-sixty_ Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A page server stopped by Ctrl-C, SIGTERM or SIGHUP now ends through
exit_serving, the pathleaf server stoptakes, so every stop of a serving process has one exit path. This is Max's decision #34 on the overnight report.Before. On a signal, Python unwound normally so its exit handlers would stop title App Servers. The lease was released in
cmd_serve'sfinallywhile the row publisher and the lifetime check, both daemon threads, still ran through interpreter exit. In that window an old publisher could overwrite a replacement server's row, or the old lifetime check could start a Codex title App Server after the exit handlers had stopped them, leaving it orphaned.Now. Once
cmd_serveis about to start those threads, it installs handlers (server.exit_serving_on_signals) that callexit_serving(128 + signum). Uvicorn still catches SIGINT and SIGTERM first: it closes its listener, lets in-flight responses finish, and raises the signal again, which reaches the new handler.exit_servingstops the title App Servers and callsos._exit, which releases the lease as every thread stops. Ifserve_foreverever raises, the traceback is printed and the process also ends throughexit_serving. Before serving starts, a signal still unwinds, so preparation retires what it holds. A temporary server (server run --temporary) holds no lease, starts no threads and keeps unwinding.Visible change. A foreground
leaf server runstopped with Ctrl-C exits with status 130 and no{"error": "interrupted"}line, where it used to exit 1 with that line. SIGTERM and SIGHUP keep their statuses, 143 and 129.Removed. #3346 had set
atexit = Falseon the page subscription's finalizer because interpreter exit closed the subscription under the still-running server threads, which re-subscribed in a loop. No server reaches interpreter exit with those threads running now, so that setting and its comment go.Checked. A temporary test (not committed) ran
leaf server run --persistenton a page claimed by a fake Codex session, whose lifetime check starts a fake title App Server, and signaled it at random points before and after that server started. With this change:The same run on the base sources also left nothing running (0 of 60), at a median exit of 0.17 s. The window was found in review and wasn't reproduced; this change closes it by construction rather than by fixing an observed failure. The focused model and process tests for server lifetime, title servers and
events --followpass.