Skip to content

preview: a reply sent before the request body ended stays whole and closes the connection - #687

Closed
CMGS wants to merge 3 commits into
mainfrom
server/preview-early-reply
Closed

CMGS wants to merge 3 commits into
mainfrom
server/preview-early-reply

Conversation

@CMGS

@CMGS CMGS commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #683.

What

The preview proxies (proxyLocal and forward in sandboxd/server/preview.go) cut a reply when the guest app answered before the browser had sent the whole request body. A reply written before the node has read the request body to its end now reaches the browser whole and carries Connection: close; the node closes that connection after it. A request with no body, or whose body the node had read to its end before the reply, keeps its connection.

Two rules:

  • The guest's early reply (ModifyResponse) and the node's own error reply (ErrorHandler) set Connection: close while the upload has not reached its end. Go's server closes the request body at the reply's header write unless the reply closes the connection; that close is what cut the Transport's read on main. With the header the server leaves the body alone, and the next request never meets the unread upload because there is no next request on that connection.
  • The body handed to ReverseProxy is a wrapper whose Close only stops later reads. ReverseProxy and the Transport both close the request body before the reply is complete; the real body's close waits for the rest of the upload, while a chunked reply's last chunk is written only after the handler returns, so a client that stops sending at the status and the handler would wait on each other.

The guest Transport is unchanged. serve's own early answers (403, 502 for a wrong owner) are written before the proxy and are unchanged.

Production lines: +47 -0 in preview.go. Tests: +357. Docs: the "Early replies" bullet in docs/deploy.md.

The branch has three commits; the second and third are review rounds. The first carried an EnableFullDuplex call and an ExpectContinueTimeout on the guest Transport. The call had no effect (net/http reads the flag in one place, after the check a reply with Connection: close already fails). The timeout broke a guest that refuses an Expect upload without Connection: close: cut reply, stalled connection, and through a forward hop two nodes waiting on each other. Both are gone.

Tests

All drive a real http.Server over TCP. New in preview_test.go:

Test Pins
TestPreviewRelaysAWholeReplyWhenTheGuestAnswersBeforeTheUploadEnds (on the owner, through a forward hop) the issue's case: a 256 KiB reply to a 16 KiB upload still in progress arrives whole with Connection: close
TestPreviewGivesAClientThatStopsSendingAtAnEarlyChunkedAnswerTheWholeReply a client that stops sending at the status gets the whole chunked reply
TestPreviewRelaysAnExpectContinueUploadTheGuestReads (on the owner, through a forward hop) an Expect: 100-continue upload the guest reads gets 100 Continue, then 200, and keeps the connection
TestPreviewConnectionServesTheNextRequestAfterAFullySentUpload (declared length, chunked) a fully sent upload does not cost the connection
TestPreview502BeforeTheUploadEndsAnnouncesTheClose (upload in progress, no body, upload in progress through a forward hop) the node's own 502 carries Connection: close only while an upload is in progress
TestPreviewGivesAPoolingClientTheWholeEarlyRefusalAndItsNextRequest Go's pooling http.Client reads the whole early 403 and its next request succeeds
TestPreviewRelaysAnUpgradedConnectionBothWays an upgraded connection still relays both ways

Each rule fails a test when removed:

Change Result
main's preview.go the first test fails on both paths (browser read 40960 of 262144 reply bytes, err unexpected EOF); the two "upload in progress" 502 rows time out, the 502 does not arrive while the client holds the rest of its upload; the pooling client gets a cut 403
the two ModifyResponse setters removed the first test (both subtests) and the pooling-client test fail with a cut reply, 3 of 3
the wrapper's Close passed to the real body the stop-sending test and the two "upload in progress" 502 rows time out, 3 of 3
closeIfUploading removed from one ErrorHandler the matching 502 row times out, and only that row

Runs of -run TestPreview with -race on the final head: 30 of 30 on macOS, 20 of 20 in a Linux container.

Cases walked

Exit Reply framing Client Outcome
guest's reply Content-Length or chunked reads while it sends whole reply, Connection: close, connection closed after it
guest's reply chunked stops sending at the status whole reply; the node waits for the client to close
guest's reply any body read to its end first whole reply, connection kept
node's 502 (failed dial, unreachable owner) Content-Length upload in progress 502 with Connection: close
node's 502 Content-Length no body 502, connection kept
101 none upgrade with no body relayed both ways, no header added

Every exit of ReverseProxy.ServeHTTP that writes a final reply passes one of the two hooks; interim (1xx) replies do not, and they do not end the request.

One case is worse than main

An Expect: 100-continue upload with at most 256 KiB still to send, refused by a guest that closes its connection without reading the body, with a reply of tens of KiB. Linux, Go guest, raw client:

Upload Reply main: cut this change: cut
16 KiB 3 bytes 0 of 300 0 of 300
16 KiB 64 KiB 2 of 300 (-race: 1 of 150) 98 of 300 (-race: 68 of 150)
1 MiB 64 KiB 69 of 200 64 of 200
1 MiB 8 KiB 2 of 200 1 of 200

Cause: when a write of the upload to the guest fails, Go's Transport closes the request body and then the guest connection, which ends the read of the reply. On main that close of the request body is the real one and reads up to 256 KiB of the upload first; the delay happens to let the reply finish. The wrapper's Close returns at once. With more than 256 KiB left main's close gives up at once too, and the two match. Without Expect the reply is copied before the write fails, and this change is better than main (see the limits below).

Who can reach it: a client that sends Expect: 100-continue on a body of at most 256 KiB. Browsers do not send Expect. curl 8.7.1 sends it only above 1 MiB (measured: 1048576 bytes no, 1048577 bytes yes, POST and PUT), where the two arms match. Go's client sends it only when the caller sets the header. Other clients were not checked.

The root cause is the same for every row where a guest closes without reading, on both arms: a failed write of the upload must not end the read of the reply. That is a change to the guest connection, with its own review, and is recorded as #689.

Limits, stated in docs/deploy.md

Main does the same or worse in each.

  • A client that stops sending but keeps the connection open holds it until the client closes it. When more than 256 KiB of a declared Content-Length is still unsent, the node closes it after the reply instead.
  • A client that sends its whole body before it reads, with a large body still to send, can lose the reply.
  • Through a forward hop the owner node closes its side after at most 256 KiB more of the upload, so a large upload still to send together with a large reply that is read slowly can still end short.
  • A guest app that closes its connection without reading an upload that is still arriving can still cut its own reply. Without Expect, Linux, 100 runs, a 256 KiB reply to a 1 MiB upload: 94 cut on main, 22 to 34 with this change; a 4-byte reply: 0 on both.

Kept without code:

  • A client that sends Expect: 100-continue, receives 100 Continue and then sends nothing gets no reply until it sends or closes. A client must send its body after a 100 Continue. Main does the same.
  • An upgrade request that carries a body and gets a 101 before that body ended would get Connection: close on the 101. No known client sends a body with an upgrade and no known server answers 101 before reading it.

Hot path

Not on the claim path. Per preview request: the wrapper (24 bytes) and the two closures that capture it, and one atomic load per body read.

Gates

On this branch (main f23328d plus these commits): golangci-lint 0 issues in all five modules on linux and darwin, asl clean, go test -race ./... green on darwin and in a linux container. Go comment lines added: 0.

Not run on hardware or through a real browser behind a TLS proxy. The measurements above use loopback TCP guests; in production the guest connection is a relayed stream, where a guest that closed ends the stream after its reply, and a later write of the upload fails the same way.

…loses the connection

Both preview proxies run the request in HTTP/1 full duplex, so the server no
longer closes the request body under the Transport at the reply's header write.
The body handed to ReverseProxy has a Close that only stops later reads, so the
proxy's deferred close does not wait for the rest of the upload before the
reply ends. The upstream's early reply (ModifyResponse) and the proxy's own
error reply (ErrorHandler) carry Connection: close while the upload is still
running, so that connection is not reused.

The Transport to the guest waits up to one second for 100 Continue on a request
that carries Expect: 100-continue. A guest that refuses such a request without
reading the body closes at once; without the wait the proxy kept writing the
upload to that closed connection and the refusal could be cut.

Fixes #683
CMGS added 2 commits December 2, 2026 15:50
…pen; the full duplex call goes

net/http reads the full duplex flag in one place, after the check that a
reply with Connection: close already fails, so the call had no effect.
The helper only wraps the request body now and is named for that.
The Expect sentence in the docs holds for a refusal within one second.
…efusal sent without Connection: close

With ExpectContinueTimeout set, a guest that refused an Expect upload without
Connection: close got the body anyway; the closed expect reader made the write
loop drop the guest connection, cut the reply and stall the client connection.
The two "refused" subtests pinned that line and go with it.
@CMGS

CMGS commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Closing without merge. Review found a second regression against main on this head, so main stays as it is and #683 stays open.

What the two heads showed:

  • fc6beb3: an ExpectContinueTimeout on the guest Transport cut and stalled a refusal sent without Connection: close; through a forward hop both nodes waited on each other. Removed in ab441e7.
  • ab441e7: the wrapper's Close returns while the Transport can still be inside a read of the request body. When that read takes the upload's last bytes after the handler has returned, the server's end-of-request step waits on the client with no deadline. Linux, Expect upload of 16 KiB, guest answers 413 and closes: the client was held for the probe's full 3 s in 21 of 300 runs, main in 0 of 300. On the abort path (a copy error after the header) the client holds a cut reply until its own timeout, where main ends the connection at once.
  • ab441e7 also has the cell named in the description (an Expect upload with at most 256 KiB left, refused by a guest that closes without reading).

The constraint any next design has to meet: once the reply is relayed while the upload is still relayed, a read of the request body can be in flight when the handler returns. Waiting for it brings back a mutual wait with a client that stops sending at the status (a chunked reply then never gets its last chunk); not waiting gives the hold above. Ending that read needs a deliberate abort, and its side effects on the server's own drain have to be measured in every cell: the stop-sending client, chunked and declared-length uploads, Expect, the forward hop, and nothing left waiting after the client closes.

Follow-ups: #689 (a failed write of the upload ends the read of the reply), #690 (an Expect race that leaves nodes waiting; main has it). The branch is kept for reference.

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.

preview: a reply is cut when the guest app answers before the request body is fully sent

1 participant