Skip to content

fix(coderd/httpapi): stop writing websocket frames to ResponseRecorder in test - #25284

Merged
johnstcn merged 3 commits into
mainfrom
fix/plat-208-heartbeat-ping-flake
May 14, 2026
Merged

johnstcn merged 3 commits into
mainfrom
fix/plat-208-heartbeat-ping-flake

Conversation

@johnstcn

Copy link
Copy Markdown
Member

The mockEventSenderWrite function in newOneWayWriter() wrote WebSocket frame data to both the net.Pipe and the httptest.ResponseRecorder. After websocket.Accept() calls WriteHeader(101), the recorder rejects body writes with "response status code does not allow body". When HeartbeatClose sends a ping, the control frame flush routes through the recorder, producing an ERROR-level log that slogtest catches as a test failure.

Removed the recorder.Write(b) call from the write function. The recorder is only needed for header/status inspection; WebSocket frame data should only go through the net.Pipe.

Closes coder/internal#1521

🤖 Generated by Coder Agents

Analysis

The flake reproduced 8+ times across different commits on main, always with the same error signature. The httptest.ResponseRecorder enforces HTTP semantics (no body after 101 Switching Protocols), but WebSocket frames were being written to it after the upgrade handshake. The timing of bufio.Writer flushes during ping control frames made this intermittent. Verified fix with 20 consecutive runs of the heartbeat test with zero failures.

The mockEventSenderWrite function in newOneWayWriter() wrote WebSocket
frame data to both the net.Pipe and the httptest.ResponseRecorder. After
websocket.Accept() calls WriteHeader(101), the recorder rejects body
writes with "response status code does not allow body". This caused
intermittent ping failures logged at ERROR level, which slogtest
caught as test failures.

The recorder is only needed for header and status code inspection;
WebSocket frame data should only go to the net.Pipe.
@johnstcn johnstcn self-assigned this May 13, 2026
@johnstcn

Copy link
Copy Markdown
Member Author

/coder-agents-review

@coder-agents-review coder-agents-review Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean, well-diagnosed fix. The root cause analysis is precise: httptest.ResponseRecorder rejects body writes after WriteHeader(101), and the bufio.Writer flush timing during WebSocket ping control frames made the failure intermittent. The fix is at the right causal level, removing the recorder from the write path while preserving its legitimate role in header/status inspection.

One nit from the panel (5/13 reviewers converged on it independently). No correctness issues found.

Pariston captured the differential diagnosis well: four possible framings of the problem, and the author chose the right one. Error classification in HeartbeatClose would have silenced the symptom without fixing the structural mistake. The deeper cleanup (separate writer chains per lifecycle phase) is unnecessary for a test helper.

As Hisoka put it: "The original bug was a fine Texture Surprise: mockEventSenderWrite presented itself as a faithful dual-write mirror, but recorder.Write(b) was a poison pill after WriteHeader(101)." ♠

PS. The mockServerSentWriter sibling was checked by multiple reviewers; it writes to a bytes.Buffer, not the recorder. No sibling issue exists.

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/httpapi/httpapi_test.go Resolved
@johnstcn
johnstcn marked this pull request as ready for review May 13, 2026 17:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a flaky WebSocket test in coderd/httpapi by preventing WebSocket frame bytes from being written to an httptest.ResponseRecorder, which rejects body writes after a 101 Switching Protocols response.

Changes:

  • Removed the custom writer wrapper that duplicated WebSocket writes to both net.Pipe and ResponseRecorder.
  • Updated the test WebSocket writer to send frame data only through the pipe-backed writer.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@johnstcn
johnstcn merged commit 581f3bd into main May 14, 2026
35 checks passed
@johnstcn
johnstcn deleted the fix/plat-208-heartbeat-ping-flake branch May 14, 2026 08:15
@github-actions github-actions Bot locked and limited conversation to collaborators May 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

flake: TestOneWayWebSocketEventSender (heartbeat ping)

3 participants