Skip to content

Fix two load-only flakes: a signal-stopped server's exit livelock and a rendering turn ending before observer deliveries - #3346

Merged
max-sixty merged 4 commits into
mainfrom
agent-af01f18e6eacfbc2c
Oct 20, 2026
Merged

max-sixty merged 4 commits into
mainfrom
agent-af01f18e6eacfbc2c

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Two load-only flakes, each traced to its cause and fixed there.

A persistent server stopped by a signal could hang at exit. This is the 120 s teardown timeout that #3327 worked around in test_server_run_persistent_declines_the_claim_a_harness_session_offers by stopping the server with server stop. The page subscription's weakref.finalize (_PageChanges.connect, file_changes.py) ran at interpreter exit by default. It closed the subscription while the row publisher and the lifetime check, both daemon threads, still waited on it. Each woke, found the subscription closed, and subscribed again, which registered a new finalizer that weakref's exit handler then ran in turn. The loop ends only when the daemon threads fall behind weakref's exit handler; in CI it outlasted the fixture's 120 s wait. A user who stops a foreground leaf server run with Ctrl-C hits the same path; server stop never did, because exit_serving calls os._exit.

The finalizer now skips exit, since exit releases the native watches anyway. FileChanges.matches no longer checks the stop flag. close empties the roots matches compares, and with exit skipping the finalizer, nothing closes a subscription its owner still hands out, so the flag added nothing. The test now stops its server with Ctrl-C, as its user would, so it exercises the path that hung.

On macOS, a persistent server sent SIGTERM after printing its URL hung past 10 s in 11 of 30 trials, and sent Ctrl-C in 7 of 27. With the change, none of 84 did (30 and 27 SIGTERMs, 27 Ctrl-Cs, at delays from 0 to 1.5 s). The CI hang was on Linux; the mechanism is in Python's exit sequence rather than the platform watcher, so I expect the same fix there but did not run it on Linux.

A rendering turn ended before its IntersectionObserver deliveries. review-queue's and rust-sort's render-gate resize journeys failed now and then under load with a 32×32 keyboard stop present on one pass and missing on the other. The stop is the Copy control of a code block far down the page, which leaves layout when the runtime's near-window IntersectionObserver (anchorView, css-anchors.js) says its block is more than a screen away. The driver's requestFrame marked a turn presented from a task queued in its animation-frame callback, and Chrome and Firefox deliver that update's IntersectionObserver callbacks in a later task. So one_frame, and rendered after it, could return before the callback that takes the control out of layout.

requestFrame now starts an IntersectionObserver in the animation frame and marks the turn from a task its first notice queues, after every observer delivery of that update. one_frame's docstring already promised this ("its observer deliveries included"). It applies to page check --render as well as the suite. The new test checks the order directly: it failed 5 of 5 runs before the change and passed 5 of 5 after. review-queue's journey at 4× CPU throttling failed 3 of 14 runs before and passed 20 of 20 after; rust-sort's passed 10 of 10 after, though it had also passed 6 of 6 before at that throttle.

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

max-sixty and others added 4 commits October 20, 2026 08:15
A foreground `leaf server run` stopped by Ctrl-C or SIGTERM could hang at
exit, which is the teardown timeout #3327 worked around in
`test_server_run_persistent_declines_the_claim_a_harness_session_offers`.
The page subscription's `weakref.finalize` ran at exit by default. It
closed the subscription while the row publisher and the lifetime check,
both daemon threads, still waited on it. Each woke, found the
subscription closed and subscribed again, which registered a new
finalizer that `weakref`'s exit handler then ran, and so on. On macOS a
persistent server hung past 10 s in 11 of 30 SIGTERMs and 7 of 27
Ctrl-Cs sent after it printed its URL; with the finalizer kept off exit,
none of 84 did. `server stop` never hit this because `exit_serving`
calls `os._exit`.

The finalizer now skips exit, since exit releases the native watches
anyway. `FileChanges.matches` no longer checks the stop flag: `close`
empties the roots it compares, so a closed subscription never matched
on that check alone.

The persistent-run test now stops its server with Ctrl-C, as its user
would, in place of #3327's `server stop`, so it exercises the path that
hung.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The render-gate journeys for review-queue and rust-sort failed now and
then under load with a 32x32 keyboard stop present on one pass of the
resize journey and missing on the other: the Copy control of a code block
far down the page. Whether that control is in layout follows the
runtime's near-window IntersectionObserver (`anchorView`,
css-anchors.js). The driver's `requestFrame` counted a turn as presented
in a task queued from its animation-frame callback, and Chrome delivers
the same update's IntersectionObserver callbacks in a later task. So
`one_frame`, and `rendered` after it, could return before the callback
that takes the control out of layout, and the journey compared a reading
taken before it with one taken after.

`requestFrame` now starts an IntersectionObserver from the animation
frame and marks the turn from a task its first notice queues, which comes
after every observer delivery of that update. That is what `one_frame`'s
docstring already promised ("its observer deliveries included"), and it
holds for `page check --render` as well as the suite. The new test
checks the order directly; it failed 5 of 5 runs before the change and
passed 5 of 5 after. review-queue's journey at 4x CPU throttling had
failed 3 of 14 runs and passed 20 of 20 with the change.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit b63c8db into main Oct 20, 2026
3 checks passed
@max-sixty
max-sixty deleted the agent-af01f18e6eacfbc2c branch October 20, 2026 16:21
max-sixty added a commit that referenced this pull request Oct 21, 2026
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_

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