Repository navigation
Keep leaf.page's container connections open past the runtime's 5 s pool, and record why a container stops answering - #6370
Merged
Conversation
…ol, and record why a container stops answering #4656 tracks leaf.page requests that got the Containers SDK's 500 while the container kept running. The seven days of retained Workers Observability and container metrics (Nov 11–18) show two different failures under one `source: "disconnected"` label, and a third that is not a fault. **"Network connection lost" is Leaf's.** The Workers runtime reaches a container through a pooled HTTP client that keeps an idle connection for 5 s (`kj::HttpClientSettings::idleTimeout`, which `api/container.c++` leaves at its default; workerd pooled container connections from "Reuse HTTP tunnels for container fetches", Jul 29, and dropped its autogate Sep 28). uvicorn closes an idle keep-alive connection after 5 s too. A request the pool sends on a connection idle for about 5 s therefore lands on a socket uvicorn is closing, and the SDK answers "Container suddenly disconnected, try again". Both drops retained (Nov 12 05:32:55 UTC, Nov 14 23:40:08 UTC) were sent while a pooled connection had been idle 4.86–5.03 s, after a burst of concurrent requests had opened it. A local `wrangler dev` repro (a Container Durable Object, uvicorn 0.54 in Docker, bursts of three requests spaced by a sweep of gaps) failed 10 of 42 requests at a 5000 ms gap and none at 4985–4995 or 5005–5020 ms. With a 75 s server keep-alive, 0 of 54 failed at 5000 ms. `LeafHTTPServer` now keeps an idle connection for 60 s (`KEEP_ALIVE_SECONDS`), so the client always closes an idle connection first. Local Leaf servers keep idle browser connections longer too, and a stop still closes them at once. **The Nov 15 accept timeout reads as Cloudflare's.** From 22:03:50.1 to 22:04:03.3 UTC, nothing in container `45598727…` appears to have run: Codex stamped a command start at 22:03:50.099 that the adapter logged 13.2 s later; Codex next called the model at 13.4 s, 0.14 s after the logs resumed, where the week's 15 other calls during a command came at 10.08–10.27 s; and the container's 10 s CPU sample for the window shows 19 ms of CPU, below the 30 ms an idle container uses. The week's 20 other commands over 9 s peaked at 50–94% CPU, and memory stayed at 536 MB of 3 GiB. CPU starvation or memory pressure would show CPU, and a blocked event loop would leave Codex running and the kernel accepting connections, which the runtime said the container did not. It is one event in a week of fleet records, and nothing in Leaf can repair it. **"Can't read from request stream because client disconnected" is a visitor who left while their container started.** It made 13 of the week's 19 `disconnected` records. The SDK's own console error lost its message for the accept timeout, so the `http_error` record now carries the SDK's sentence as `detail`, which tells these three apart. `worker/leaf_website/stalls.py` adds a thread in the container. Each second it sends Leaf's server a loopback health check and judges the one it sent a second earlier. It writes `container_stalled` when the thread itself woke a second or more late, with the VM's CPU, steal and pressure-stall time over the gap, and `container_unresponsive` when the earlier check is still unanswered, with the frames the serving thread is in. In a one-vCPU Docker container, a 4 s `docker pause` logged `container_stalled` (`stalledMs` 3804) and dropped the health check it interrupted, four busy processes logged nothing, and a server holding its health route logged `container_unresponsive` within a second, naming the holding frame, or `select` when a route thread held it. The resend #5815 added stays: its case was a connection that closed while the container started, which the keep-alive change does not reach. uvicorn's shutdown also answers a request in flight without `Connection: close`, so a pooled client can still reuse that connection as a container stops; the resend covers reads there. `pnpm run check:worker` passes (91 tests), as do the server keep-alive and stop tests in `tests/process/test_interact_server.py` and `tests/model/test_server_lifetime.py` and the adapter's start and stop test. A check against `LeafHTTPServer` reused a connection after 6 s idle; with `KEEP_ALIVE_SECONDS = 5` the same reuse got `RemoteDisconnected`. Refs #4656
…pare only counters both readings hold A host pause while the watcher read its counters fell after the tick's lateness was measured, so the pending health check was reported as unanswered and the stall only on the next tick. A counter that failed to read once and then succeeded raised KeyError in `spent` and ended the watcher. Found by a Codex review.
leaf-agent
approved these changes
Nov 18, 2026
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.
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'sapi/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 localwrangler devrepro 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.LeafHTTPServernow 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_errorrecords carry the SDK's failure sentence asdetail, and a watcher thread in the container (worker/leaf_website/stalls.py) recordscontainer_stalledorcontainer_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