Skip to content

End a page server stopped by a signal through exit_serving - #3452

Merged
max-sixty merged 2 commits into
mainfrom
agent-a9b4fc3990e8e8fa4
Oct 21, 2026
Merged

max-sixty merged 2 commits into
mainfrom
agent-a9b4fc3990e8e8fa4

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

A page server stopped by Ctrl-C, SIGTERM or SIGHUP now ends through exit_serving, the path leaf server stop takes, 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's finally while 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_serve is about to start those threads, it installs handlers (server.exit_serving_on_signals) that call exit_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_serving stops the title App Servers and calls os._exit, which releases the lease as every thread stops. If serve_forever ever raises, the traceback is printed and the process also ends through exit_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 run stopped 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 = False on 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 --persistent on 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:

Signal Trials Exit status Exited within 10 s Median / max exit Lease held after exit Title servers started Left running
SIGTERM 30 143 30 0.07 / 0.13 s 0 17 0
SIGINT 30 130 30 0.07 / 0.13 s 0 18 0
SIGHUP 10 129 10 0.01 / 0.02 s 0 5 0

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 --follow pass.

This was written by Claude Code on behalf of max-sixty

max-sixty and others added 2 commits October 21, 2026 00:03
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]>
@max-sixty
max-sixty merged commit 4a18d5e into main Oct 21, 2026
3 checks passed
@max-sixty
max-sixty deleted the agent-a9b4fc3990e8e8fa4 branch October 21, 2026 07:23
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]>
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.

2 participants