TestClosedBeforeSentIsSentAgain failed on CI's race run (b77400826) and
in a full suite on mote with `Post "https://127.0.0.1:…": EOF`. It is a
product gap, not the test's timing: the same happens to a user whose
proxy or TUN closes a new connection right after the TLS handshake while
the machine is busy.
Cause, read from Go 1.26.8's net/http transport: the issue's guess (the
8 MiB write had begun) is the other way round. Once the write goroutine
starts the body, any write failure is kept as the request's error
(doBodyCopy's bodyReadError, then mapRoundTripError's req.err), so
replayable sees a write error and sends again. A bare "EOF" comes only
when the reader sees the close and closes the connection before the
write goroutine has picked the request up at all (writeLoop takes
closech): nothing was written, WroteRequest never fires, and Go turns
nothingWrittenError{transportReadFromServerError{EOF}} into io.EOF. Go
doesn't retry on a new connection, and closedBeforeSent took only write
errors, so the turn failed as a 502 counted against the account.
replayable now also sends again on a new HTTP/1.1 connection that ended
with "EOF" or a read error (a reset) while the request's write had not
finished: no WroteRequest without an error (httptrace). Once Go has
written the whole request the same end may follow a vendor that read it,
and that is never sent again, as before.
Tests: TestEndedBeforeWriteBeganIsSentAgain gives replayable what Go's
transport gives it on that path (GotConn new, no WroteRequest, io.EOF or
a read reset) twice, then an answer. A real transport can't be held at
that point from outside net/http (a held connection write already counts
as a body write error), so the order is injected with a fake transport,
not left to the scheduler. TestEndedAfterWriteIsNotSentAgain gives it a
whole write then EOF. TestClosedAfterSentIsNotSentAgain now also ends the
real TLS connection cleanly (EOF) after reading the whole 8 MiB request,
besides the reset.
Run (all on mote via mote-run, at this tree on 778b8e075):
- Before the fix (upstream_transport.go at 778b8e075, new tests in):
TestEndedBeforeWriteBeganIsSentAgain red 3 of 3 with the issue's error,
`Post "https://127.0.0.1:1/backend-api/codex/responses": EOF`.
- go test -race -count=50 -run 'TestClosed|TestEnded' ./internal/gateway/:
ok.
- go test -timeout 45m -count=1 ./internal/gateway/...: ok (internal/gateway 859s; exit 0)
- gofmt -l .: clean. go vet ./internal/gateway/, GOOS=linux go vet -tags
nogui ./..., GOOS=windows go vet ./...: ok.
Broken by hand: without `wrote.Load() ||` (an EOF resent whatever was
written), TestEndedAfterWriteIsNotSentAgain goes red (err nil, the request
sent again) and TestClosedAfterSentIsNotSentAgain goes red for both the
reset and the clean end (3 connections, called unsent), 3 of 3. With
endedUnwritten taken out (the old code), TestEndedBeforeWriteBeganIsSentAgain
goes red as above.
Measured, not injected: 1,200 real closes after the handshake on mote,
with -race, -cpu 1,2,4 and 20 busy loops beside them, gave write errors
and one "server closed idle connection", no bare EOF; the race is rare,
as on CI.
Not tried: a real TUN proxy.
Fixes#5519