Address two bugs that prevent containers from sleeping - #251
Open
connyay wants to merge 4 commits into
Open
Conversation
containerFetch held the count until the response body finished piping. If nobody read the body it stalled on backpressure, the counter never got back to zero, and every alarm renewed sleepAfter. Decrement as soon as the container responds instead, and let bytes moving through the body renew the activity timeout. A stream someone is actually reading still keeps the container awake. Fixes cloudflare#242
containerFetch counted a request as in flight until the proxied fetch settled. If the client hung up while the container was still working, that fetch could sit there forever. The count stayed above zero, every alarm renewed sleepAfter, and the container never slept. Listen on the request's abort signal too and release the count from there. An abort can race the container's own answer, so the settle-once guard WebSockets already used now fronts every release path: whichever fires first wins, the other is a no-op. The runtime only fires abort on an incoming request's signal when the enable_request_signal compatibility flag is on. Fixes cloudflare#241
connyay
marked this pull request as draft
September 4, 2026 16:08
connyay
commented
Sep 4, 2026
…rmStream Renewing the activity timeout as a body flows means the body has to pass through JavaScript. A TransformStream with a transform callback is the most expensive way to do that, because the runtime hands it 4KB at a time. A 32MB response costs about a second of CPU that way. The native IdentityTransformStream pipe moves the same body in 18ms. Read the container body with a BYOB reader into one reused buffer, sized to Content-Length when it is known, and write into an IdentityTransformStream that the runtime pumps natively. Each trip through JavaScript is then a memcpy and a promise, and the buffer size decides how many trips a body takes. Measured against a service binding under workerd, the same 32MB body takes 51ms with a known length and 80ms chunked, against 980ms with a TransformStream.
connyay
marked this pull request as ready for review
September 4, 2026 16:58
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.
Fixes #241 and #242
I personally hit #242 while testing a containers deployment. This is a nasty bug that can cause unexpected bills.