Skip to content

fix: retry a leaf.page read once when the container connection drops - #5815

Merged
leaf-agent merged 1 commit into
mainfrom
fix/issue-5814
Nov 10, 2026
Merged

leaf-agent merged 1 commit into
mainfrom
fix/issue-5814

Conversation

@leaf-agent

Copy link
Copy Markdown
Collaborator

A leaf.page read that hits a dropped container connection now gets one more attempt in the Worker instead of reaching the visitor as a 500. This is the failure behind publish-site's red run on 37de78c5458c (run 42909821856): the open page's /api/news poll landed while the verifier's media upload was waking the container, the SDK answered 500 Error proxying request to container: Error: Container port connection closed unexpectedly., and the saved-state browser-error gate counted the console error. #5810 had just moved that gate behind the media write.

Cause. Containers SDK 0.3.7 catches a proxy failure and returns a plain 500 with one of two bodies: Container suddenly disconnected, try again or Error proxying request to container: … (dist/lib/container.js, the catch in containerFetch). LeafWebsiteSession.forward logged that 500 and returned it unchanged. The same shape (one /api/news or /api/state 500 at the Worker-to-container boundary, then a successful read) was written off as transient in #2331, #3115 and #3165.

Change. forward classifies both SDK bodies as disconnected. It sends a disconnected GET or HEAD once more and returns the second answer. A write is never resent, because it may already have reached Python. A 500 that Python stamped with Leaf-Release, or any other unclassified 500, passes through as before. The http_error record gains retried, and worker/README.md documents it.

Verification. The new Worker test (reads again once when the container connection drops, but never resends a write) fails on main with expected 500 to be 200 and passes here. pnpm run check:worker passes (92 tests plus tsc). The start-up race needs Docker, which this sandbox doesn't have (#4293), so the real check is the next publish-site run, which goes through a container start in the saved-state step.

Closes #5814 — automated triage

The Containers SDK turns a lost container connection into a plain 500
("Container suddenly disconnected, try again" or "Error proxying request to
container: ..."). The session's forward logged that 500 and returned it, so a
page's /api/news poll that landed while a write woke its container showed the
visitor a console error, and failed publish-site's saved-state browser-error
gate after #5810 moved that gate behind the media write.

forward now classifies both SDK bodies as `disconnected` and sends a GET or
HEAD once more, returning the second answer. A write is never resent, since it
may already have reached Python. The http_error record gains `retried`.

The existing logging test no longer expects a disconnected GET to surface as a
500; a new test covers both SDK bodies, a second loss reaching the caller, and
a POST that is not resent.

Closes #5814
@leaf-agent
leaf-agent merged commit 7d3a418 into main Nov 10, 2026
6 checks passed
@leaf-agent
leaf-agent deleted the fix/issue-5814 branch November 10, 2026 22:34
max-sixty pushed a commit that referenced this pull request Nov 12, 2026
…n drops (#5862)

`publish-site` [run
43160732700](https://github.com/max-sixty/leaf/actions/runs/43160732700)
failed because the hosted journey's browser-error gate caught one 500
from `POST /examples/triage-board/api/user-view`. Cloudflare logged that
500 at 05:32:55 UTC as `{component: "leaf-container", event:
"http_error", method: "POST", source: "disconnected", retried: false}`,
which is the Containers SDK's connection-loss response. The container
stayed up through it: the same hosted turn kept logging steps and
finished normally about 7 s later.

#5815 made `forward` resend a `GET` or `HEAD` once after a dropped
connection, but no write, since a write may already have reached Python.
A view report is a write Python answers the same way when it arrives
twice. `observe_user_view` ignores a report whose sequence is not newer
than the session's stored one, and still answers 204. So it's safe to
resend. `forward` now resends a view report once too, from a copy of the
request it takes before the first attempt. Every other write still
passes its loss to the caller, and the README's description of `retried`
now says so.

The new case in the Worker test checks that a disconnected `POST
api/user-view` is sent again with the same body, answered with the
second response, and logged with `retried: true`. It fails on `main`
(500 instead of 204) and passes here. `pnpm run check:worker` (type
check and the Worker suite) passes.

This change doesn't explain why the container connection drops while the
container is still running. #4656 tracks that cause. It applies to the
writes this change leaves alone, such as `api/event`.

Refs #5861 — automated triage

Co-authored-by: leaf-agent <[email protected]>
leaf-agent pushed a commit that referenced this pull request Nov 18, 2026
…ol, and record why a container stops answering (#6370)

leaf.page's "Container suddenly disconnected" 500s (#4656) are a
keep-alive race. The Workers runtime keeps an idle connection to a
container for 5 s (kj's default `HttpClientSettings::idleTimeout`, which
workerd's `api/container.c++` leaves unset), and uvicorn closes an idle
keep-alive connection after 5 s too, so a request the pool sends at the
5 s mark is lost. Both drops in the last seven days of records came from
a connection idle 4.86–5.03 s. A local `wrangler dev` repro lost 10 of
42 requests at a 5000 ms idle gap and none at any other gap, and 0 of 54
with a 75 s server keep-alive. `LeafHTTPServer` now keeps idle
connections for 60 s.

The Nov 15 13 s "taking too long to accept the connection" reads as the
container's VM stopping (CPU below idle, no log line, Codex's timers
late), which no change here repairs. For the next one, `http_error`
records carry the SDK's failure sentence as `detail`, and a watcher
thread in the container (`worker/leaf_website/stalls.py`) records
`container_stalled` or `container_unresponsive`, which tell a stopped VM
from a blocked server.

To weigh: the keep-alive applies to every Leaf server, local ones too,
and a stop still closes idle connections at once. uvicorn's shutdown
still answers an in-flight request without `Connection: close`, so a
container that stops mid-request can still drop a pooled request;
#5815's resend covers reads.

Max asked for the diagnosis and logging; a Codex review found four
watcher and privacy issues, fixed or answered here. `pnpm run
check:worker`, the server keep-alive and stop tests, and the adapter
start/stop test pass.

Refs #4656

> _This was written by Claude Code on behalf of max-sixty_
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.

verify-site saved-state: the page's /api/news gets a 500 when the container connection drops during the media write's wake

1 participant