Skip to content

Stop the page server dropping a request on a connection opened before it - #630

Merged
max-sixty merged 4 commits into
mainfrom
fix/ci-34675185881
Sep 12, 2026
Merged

max-sixty merged 4 commits into
mainfrom
fix/ci-34675185881

Conversation

@leaf-agent

@leaf-agent leaf-agent commented Sep 12, 2026 •

Copy link
Copy Markdown
Collaborator

Two repairs to the render gate's page server and its reporting, found while diagnosing test_page_fixture_renders[corpus] in the complete-suite run on ca9bf116 (run 34675185881). Neither fixes that case — the measurement is below, and the corpus sweep is still failing on main. What this does fix is a server that drops requests, and a gate reading that named the wrong wait for the drop when one reaches it.

A request written onto a connection the server closed a second ago is dropped

TemporaryPageServer.start() put a one-second read deadline on every connection. It was there for a real reason: a socket that never sends a request line otherwise parks the join server_close() does over non-daemon request threads. But a read deadline cannot tell that socket from one a browser opened ahead of need and is about to write a real request onto — Chromium preconnects, so both exist here — and a request written onto a connection the server has already closed is dropped with no response, no console entry, and no error.

Deterministic, against the server as it stood. Open a connection, wait, then send:

idle 0.2s -> b'HTTP/1.0 403 Forbidden\r\n'    (answered)
idle 1.2s -> b''                              (dropped)
idle 2.5s -> b''                              (dropped)

LeafHTTPServer now records the connections it is holding and closes their reading half in server_close(), which bounds that join directly rather than by guessing how long a silence lasts. Only the reading half: a request already being served still gets its answer, which test_temporary_server_close_waits_for_active_request pins and which is what ruled out closing both. With no deadline left, a socket opened ahead of its request is answered whenever it speaks.

This reaches a reader, not only the suite: render_gate/preview.py serves through the same server, so version check --render runs on it.

The gate named the one thing that had gone right

_render_scheme reported a timeout anywhere in start_with_pre_upgrade_proof — goto, body > main, the theme stylesheet, and the load event — as the runtime never injected its banner. In the corpus failure the runtime had started and its banner was on the page: what had not happened was the load event, because one module request was still open. Each wait now says which arrival it gave up on and what the page was still asking for:

[dark] pre-upgrade proof failed: the document never reached load; still requesting /runtime/layer-client.js

A timeout raised out of the proof is a RuntimeError now, which lands in a different arm of _render_scheme than the one it used to — and that arm returned the message on its own, so the console entries and >= 400 statuses the page had collected were dropped. A document refused with 403 said which wait stopped and nothing about why. It carries those explanations now, the way the two waits either side of it already do, and the entry the proof holds itself is left out of the requests the message names: /leaf.js is open at every wait before the release because this gate chose to hold it, not because the page is waiting for it.

Nothing on main asserted the old string any longer — #620 moved the resize case onto the probe-module failure, which was the last assertion reading it — so the branch that reports a real hang had no test at all. It has one now, and that reading is what turned this failure from unattributable into a named file.

What this does not fix

test_page_fixture_renders[corpus] still fails, at a rate that makes main's nightly a coin toss rather than a rare red. Interleaved on one idle Linux box, alternating d1319e88 and this branch, one run each per pair:

uv run pytest --run-nightly -q -n0 -p no:randomly \
  "tests/test_render_gate.py::test_page_fixture_renders[corpus]"

d1319e88      2 failed / 7
this branch   3 failed / 7

So the dropped-connection mechanism above, real as it is, is not what the corpus sweep is hitting — and a block-at-a-time comparison would have said otherwise, because the rate on this box drifts from 0 in 6 to 4 in 6 across an hour. #629 carries what is left: the failure now names its wait, sometimes a module still in flight and sometimes nothing at all, which are two different endings and neither is explained.

Verification

On Linux with the installed Chrome, at e72d5fc6:

  • uv run pytest tests — 1029 passed; four failures this sandbox owns rather than the branch (umask, the :: wildcard with no IPv6 route, and two on a read-only /tmp), all four red at 4aa102e2 under the same command.
  • uv run pytest tests/test_render_gate.py --run-nightly -n2 — 101 passed, and test_page_fixture_renders[corpus] failing as ci-fix: the corpus render sweep fails on a load event that never fires #629 describes.
  • uv run pytest tests/test_interact_server.py -n0 — 98 passed, the :: wildcard case aside.
  • uv run pre-commit run --files ... on the four files.
  • Bug-back, each new test against the source reverted: the connection opened before its request reads back b'', and the held module raises the bare TimeoutError the caller used to call a missing banner.

TemporaryPageServer put a one-second read deadline on every connection so
that a socket which never sends a request line could not park the join
server_close() does over non-daemon request threads. A read deadline cannot
tell that socket from one a browser opened ahead of need and is about to
write a real request onto, and a request written onto a connection the
server has already closed is dropped with no response and no error.

LeafHTTPServer now records the connections it is holding and closes their
reading half in server_close(), which bounds that join directly. Only the
reading half, so a request already being served still gets its answer.

The render gate reported a timeout anywhere in start_with_pre_upgrade_proof
as the runtime never injecting its banner, which in this failure named the
one thing that had gone right. Each wait now says which arrival it gave up
on and what the page was still asking for.
@leaf-agent leaf-agent changed the title Answer a request written onto a connection opened before it Stop the page server dropping a request on a connection opened before it Sep 12, 2026
# Conflicts:
#	skills/leaf/scripts/leaf/render_gate/scheme.py
#	tests/test_render_gate.py

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Two defects in the gate's new reporting, both on the path this PR sets out to repair — the message a version check --render reader acts on. I'm pushing the fix to this branch.

A refused document now names the wait and drops the status that explains it. Every timeout inside start_with_pre_upgrade_proof used to surface through _render_scheme's except PlaywrightTimeout branch, which appends errors and resize_notices — the console entries and the >= 400 response statuses the page collected. Converting those timeouts to RuntimeError routes them to the except RuntimeError branch instead, and that one returns the message alone. Measured on a page served under the wrong key, base against this head:

9952a449   [light] the runtime never injected its banner — 403 http://127.0.0.1:45671/versions/v1.html?t=not-the-key; Failed to load resource: the server responded with a status of 403 (Forbidden)
2f289139   [light] pre-upgrade proof failed: the document never reached an authored main

The label is right now and the evidence is gone — and since nothing asserted the old branch, the drop is silent. That branch should carry the explanations its two siblings already do.

The entry the proof holds itself is reported as something the page is waiting for. hold_entry holds /leaf.js until after the theme stylesheet wait, so it sits in open_requests at every wait before that by the gate's own choice. Holding **/theme.css against this head:

the document never reached its theme stylesheet; still requesting /leaf.js, /theme.css

A reader who chases /leaf.js there is chasing the gate's hold — the same misattribution this PR removes at the other end.

The server change reads correctly to me: SHUT_RD lands before ThreadingMixIn.server_close's join of the non-daemon request threads, so it bounds that join directly, and _Threads.append keeps the durable server (daemon threads) out of the join entirely.

…own hold out of it

A timeout inside the pre-upgrade proof now raises RuntimeError, which routes it to a branch that returned the message alone: the console entries and >= 400 statuses collected on the page were dropped, so a document refused with 403 reported the wait it stopped at and nothing about why. That branch now carries the same explanations its two siblings do.

The proof holds the Leaf entry until after the theme stylesheet wait, so /leaf.js is open at every wait before that by the gate's own choice; it is left out of the requests the message names.

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

One defect in the new reading, on the wait this PR's own diagnosis lands on.

Past the release, the entry is subtracted from the requests the message names. reaching removes held[0].request unconditionally, but the hold ends at held[0].continue_() — and the one wait after that is load. So an entry the server accepts and then drops leaves the load event waiting on the one request the message deliberately hides, and a reader gets the wait with nothing attached. That is one of the two endings #629 describes as unexplained, and it is the one this reading was supposed to remove.

Measured against this head with a server that answers everything but /leaf.js, which it accepts and drops, and the same case with the subtraction scoped to the hold:

159d9335   the document never reached load
fixed      the document never reached load; still requesting /leaf.js

I'm pushing the fix and the case that pins it.

The subtraction that keeps the gate's own hold out of a wait's message was
unconditional, so the one wait after the release — the load event — reported
an entry the server accepted and dropped as nothing at all. Past the release
it is the page's own request, and the message names it.
@max-sixty
max-sixty merged commit 287188d into main Sep 12, 2026
4 checks passed
@max-sixty
max-sixty deleted the fix/ci-34675185881 branch September 12, 2026 19:44
max-sixty added a commit that referenced this pull request Sep 17, 2026
Every page is now served by starlette over uvicorn, still one process
per page. This is the transport alone: no shared daemon, no database, no
change to event admission or projection. starlette, uvicorn and anyio
are declared in `pyproject.toml`, but `mcp` already brought them, so an
install fetches nothing new.

The point is not the line count, which comes to −28 across the change
and −75 of that in tests. It is that uvicorn now owns connection
lifetime, and the workarounds Leaf carried for `http.server` go with it:
the BrokenPipe suppression in `handle`, the news stream's `select()`
peer-gone loop, `ThreadingHTTPServer` with its open-connection set and
the `SHUT_RD` close that fixed dropped Chromium preconnects in #630,
plus `DualStackHTTPServer`, `server_at`, and three tests that reached
into `http.server` internals.

`LeafHTTPServer` opens the listening socket itself and hands uvicorn a
`dup()`, so the port is a fact before anything serves on it and a taken
address still raises out of the constructor rather than exiting the
process. `Handler` becomes an ordinary per-request object that
`page_app`, an ASGI callable in `http.py`, builds and runs through
`run_in_threadpool`; the route bodies stayed synchronous. Route scoping,
the key and cookie gate, the `Leaf-*` headers and layer gate,
`/p/<capability>` multiplexing, the website manifest lookup, preview
snapshot mode, the 50 ms `LOOK_S` re-stat, `TemporaryPageServer`,
`cmd_serve`, the detached handshake and session lifetime all keep their
meaning.

Two things were tried and left out. The `_get`/`_post` dispatch chains
stay hand-written, because `_select_page` rewrites the path after the
ASGI scope is fixed — both the MCP capability prefix and the website's
longest-prefix manifest lookup do this — so a starlette router keyed on
`scope["path"]` cannot express either, and the branch bodies are page
semantics a route table would not delete. sse-starlette was adopted and
backed out: its documented `ping=0` does not disable the keepalive but
spins `await sleep(0)` in a send loop, and it patches
`uvicorn.Server.handle_exit` process-wide at import. `StreamingResponse`
gives the disconnect cancellation the stream needs, since uvicorn
reports ASGI spec 2.3.

One behaviour change: the event door declares a 10 MiB body bound
instead of discovering it through a failed allocation, so an oversized
`Content-Length` is refused as exceeding the limit rather than as
invalid. Under ASGI the old refusal would have hung waiting for bytes.
Its test now also asserts `Connection: close`.

Review found five things, each reproduced before it was fixed. A signal
reaches uvicorn alone, and its `handle_exit` sets `should_exit` without
telling the news stream to end, so a page with one reader survived
SIGTERM past 30 seconds; `stopping` is now set in a wrapper around it,
and the same page exits in 0.5 s. The 10 MiB event bound read
`Content-Length`, which a chunked body does not carry, so 12 MiB sent in
chunks was buffered whole and then refused as invalid JSON; the bound
now belongs to the read, and the door's refusal arrives while the sender
is still writing. The news stream lost its `Cache-Control: no-store`,
which is back, with `_news` asserting it for every test that opens a
stream.

Then two more. A detached serve's stderr is a pipe its parent stops
reading once the URL is in hand, and the override that kept the old
handler quiet went with `http.server`; uvicorn's own "Invalid HTTP
request received." took its place at 31 bytes a refusal, and on the
serving loop the write that fills the pipe stops the page answering —
measured, 2,112 malformed request lines and the page never answered
again. Its loggers now hold a `NullHandler` and do not propagate, which
is what the comment beside the config already claimed, and a new test
sends 2,500 such lines before reading state. `ws="none"` keeps an
upgrade handshake on the ordinary HTTP path, where the key gate answers
it with a sentence; left on, it arrived as a scope past the gate and the
peer got a 500 with a traceback.

The comment in `worker/server.py` claimed the close was what the adapter
owed its App Server on SIGTERM. uvicorn re-raises the captured signal
with the original handler restored, so the process dies where it stood
and that `finally` never runs; the comment now says the close is for the
ordinary return.

`TODO.md` loses the row this implements.

Checks: the everyday suite passes (1,138) on the merge with main, as do
`test_website_server.py` and `test_interact_server.py` (173) and the MCP
and startup selections; pre-commit is green. Against the declared floors
— `starlette==0.48.0`, `uvicorn==0.31.1`, `anyio==4.10.0` — the server,
website and MCP suites pass (188). Server start goes from 324 ms to 346
ms over 7 paired runs, from a 20 ms import.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Opus 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