Repository navigation
fix(coderd/httpapi): stop writing websocket frames to ResponseRecorder in test - #25284
Conversation
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.
|
/coder-agents-review |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.PipeandResponseRecorder. - 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.
The
mockEventSenderWritefunction innewOneWayWriter()wrote WebSocket frame data to both thenet.Pipeand thehttptest.ResponseRecorder. Afterwebsocket.Accept()callsWriteHeader(101), the recorder rejects body writes with"response status code does not allow body". WhenHeartbeatClosesends a ping, the control frame flush routes through the recorder, producing an ERROR-level log thatslogtestcatches 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 thenet.Pipe.Closes coder/internal#1521
Analysis
The flake reproduced 8+ times across different commits on main, always with the same error signature. The
httptest.ResponseRecorderenforces HTTP semantics (no body after 101 Switching Protocols), but WebSocket frames were being written to it after the upgrade handshake. The timing ofbufio.Writerflushes during ping control frames made this intermittent. Verified fix with 20 consecutive runs of the heartbeat test with zero failures.