session: fix EnqueueInitialData reversing byte order on multi-call writers#147
Merged
Conversation
…iters EnqueueInitialData was implemented to prepend rather than append, on the assumption it would be called once before the SYN drains. But the SOCKS5 adapter calls it on every Write, and a fast local writer (the bench harness, or any application that issues several small Writes immediately after connecting) can fit many calls before a poll worker drains the SYN. Each prepend pushed the new data in front of what was already buffered, so the on-wire byte order ended up reversed. For any protocol whose leading bytes carry framing — a TLS record header, an HTTP request line, a length prefix — the upstream parses garbage or rejects the stream. The bug has existed since the connect_data optimization landed in v1.4. A specific consequence worth flagging: every upload-throughput bench result ever recorded for this project has been wrong. The bench's sized-sink reads the first 8 bytes as a size header. With prepend, those 8 bytes came from the LAST chunk written before the SYN drained — usually a body chunk full of zeros, decoded as size=0. The sink ACKed instantly without reading the body, and the harness measured the time-to-instant-ACK as upload throughput. That made v1.5 look like 22 MB/s when actual upload was ~5 MB/s. The "v1.5→v1.6 upload regression" narrative behind #143 was a bench artifact; the real per-session ceiling is `maxDrainFramesPerSession × MaxFramePayload / ActiveDrainWindow` ≈ 5.85 MB/s in both versions. The fix is one-line (append instead of prepend); the comment is the expensive part. Test added to catch the regression directly.
This was referenced May 21, 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.
Summary
`EnqueueInitialData` was implemented to prepend to the tx buffer while `synNeeded` was true. The assumption was "called once, before the SYN drains." But the SOCKS5 adapter calls it on every `Write`, and a fast local writer can fit many calls before a poll worker drains the SYN. Each prepend pushed new bytes in front of the existing buffer, so the on-wire byte order ended up reversed.
For any protocol whose leading bytes carry framing — a TLS record header, an HTTP request line, a length prefix — the upstream parses garbage or rejects the stream. The bug has existed since the connect_data optimization landed in v1.4.
Specific consequence: every upload-throughput bench result has been wrong
The bench's `sized` sink (`bench/sink/main.go`) reads the first 8 bytes as a size header. With prepend, those 8 bytes came from the last chunk written before the SYN drained — usually a body chunk full of zeros, decoded as `size=0`. The sink ACKed instantly without reading the body, and the harness measured the time-to-instant-ACK as upload throughput.
That made v1.5 look like 22 MB/s when actual upload is ~5 MB/s. The whole "v1.5→v1.6 upload regression" narrative behind #143 was a bench artifact. With the prepend fixed and `workersPerEndpoint` tested at 3, 4, and 5:
workersPerEndpointThe per-session ceiling is
maxDrainFramesPerSession × MaxFramePayload / ActiveDrainWindow≈ 5.85 MB/s. Worker count doesn't change it. 4 concurrent sessions get 4 × the per-session number, as you'd expect.What does NOT change
What DOES change for real users
What's next (separate work)
This fix means #143's premise (v1.5→v1.6 upload regression caused by
workersPerEndpoint=4) was wrong — the upload numbers were always bench artifacts. A follow-up should:workersPerEndpoint=4) — no upload regression to worry about, get back the v1.6 session-open speedup.ActiveDrainWindowneed design work — out of scope here.Verification
go test -count=1 -timeout 90s ./...— all green; new test catches the bug../bench/bench.sh --baseline v1.6.0 --scenario throughput_up_8MB_1session— 3 consecutive runs all report 4.49 MB/s (vs the previous lying 22.3 MB/s).🤖 Generated with Claude Code