Skip to content

Fix two data races in the websocket exec path - #61

Closed
Mohith26 wants to merge 2 commits into
superfly:mainfrom
Mohith26:fix/exec-data-races
Closed

Mohith26 wants to merge 2 commits into
superfly:mainfrom
Mohith26:fix/exec-data-races

Conversation

@Mohith26

@Mohith26 Mohith26 commented Nov 3, 2026 •

Copy link
Copy Markdown

Fixes #48

Both races from the issue, reproduced in this repo with go test -race:

  1. wsCmd.Close vs the write loop. Close called SetWriteDeadline on the connection while wsAdapter.writeLoop could still be inside WriteMessage. gorilla's WriteControl already takes a deadline argument and is documented as safe to call concurrently with other methods, so I dropped the SetWriteDeadline call and kept the WriteControl + Close.

  2. TTY resize goroutine reading os.Stdin. The goroutine read the os.Stdin global 10ms after start, after Start had returned to the caller. It now captures the stdin descriptor in start() before startChan is signaled, and usually returns early if doneChan closes before the 10ms timer fires (if both are ready, select may still proceed, which is harmless).

  3. proxyConn.Close vs proxyConn.Write (from review). Same pattern in proxy.go: Close called SetWriteDeadline outside writeMu while a Write could be inside WriteMessage. Same fix.

Tests: added websocket_race_test.go with one test per race, each against an httptest websocket server. The TTY test delays the exit frame past the 10ms timer so the goroutine actually reads stdin. Before the change both tests failed under -race with the stacks from the issue (websocket.go:690 vs :754, and :272 vs the test restoring os.Stdin). Each test fails under -race with its fix reverted (including reverting only the stdin capture). After the change, go test -race -count=10 on the three tests and go test -race ./... pass, and golangci-lint run (v2.11.3, repo config) reports 0 issues. I didn't run the integration tests since they need a Sprites token.

wsCmd.Close called SetWriteDeadline on the connection while the adapter's
write loop could still be inside WriteMessage. gorilla's WriteControl takes
its own deadline and is safe to call alongside another writer, so the
SetWriteDeadline call is dropped.

The TTY resize goroutine read the os.Stdin global 10ms after start, after
Start had already returned. It now captures os.Stdin before Start returns
and exits early if the command finishes first.

Both are covered by tests that fail under -race without the change.

Fixes #48
@Mohith26
Mohith26 requested a review from a team as a code owner November 3, 2026 00:34
@aezell

aezell commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Reviewed this against a local checkout of the branch. The change itself looks correct and the reasoning in the description holds up.

I confirmed both tests are real regression tests: reverting websocket.go to HEAD~1 makes each fail with WARNING: DATA RACE on the first run. go vet, go test -race -count=1 ./..., and go test -race -count=5 on the two new tests are all clean on the branch.

Two things worth considering, neither blocking:

1. proxyConn.Close has the same race, unfixed

proxy.go:101 is the identical pattern:

func (conn *proxyConn) Close() error {
	var closeErr error
	conn.closeOnce.Do(func() {
		deadline := time.Now().Add(proxyCloseDeadline)
		conn.conn.SetWriteDeadline(deadline)   // <-- not under writeMu
		conn.conn.WriteControl(...)

proxyConn.Write takes writeMu before calling WriteMessage, but Close calls SetWriteDeadline outside it. proxyConn is returned as a net.Conn from Client.ProxySocket / Sprite.ProxySocket, and net.Conn's contract allows Close concurrently with Write — e.g. a port-forwarder running io.Copy(proxyConn, src) in one goroutine while another calls Close() on peer EOF. That's the same writeDeadline write racing the flushFrame read as in #48.

The same one-line fix applies, since the WriteControl on the next line already takes the deadline. (proxyConn.SetWriteDeadline at proxy.go:134 is exposed the same way and also sits outside writeMu, though that one is arguably the caller's problem.)

2. TestTTYResizeDoesNotReadStdinAfterCommandReturns doesn't guard the stdin capture

The test server writes its exit frame immediately on upgrade, so doneChan closes ~1–2ms after start() — well before the goroutine's 10ms timer. The select therefore always takes the doneChan case and never reaches the stdin read, meaning the test only covers the new early-return, not the capture.

Verified empirically: keeping the select but reverting the capture back to int(os.Stdin.Fd()) inside the goroutine still gives ok under go test -race -count=10, whereas the full pre-PR code fails on the first run. So a later refactor that "simplifies" the capture away would reintroduce race #2 with the suite green.

Delaying the server's exit frame past ~10ms (the way TestCloseDoesNotRaceWriteLoop already does with its 5ms sleep) would let the goroutine reach the stdin read and cover both halves of the fix.

Minor

  • The description says the goroutine "no longer outlives a command that finishes inside the 10ms window" — slightly stronger than what the code does, since select picks randomly when both cases are ready, and a doneChan close just after 10ms lets the goroutine proceed regardless. Harmless in practice (Resize on a closed adapter returns a discarded error, and (*os.File).Fd() is nil- and close-safe), so no change needed; just noting the claim is a little broad.
  • Test 2 restores os.Stdin explicitly on each t.Fatalf path rather than via defer/t.Cleanup. Every current exit path is covered, so it's fine, but a t.Cleanup would be harder to break later given it's a process-wide global.

🤖 Generated with help from Claude Code

proxyConn.Close called SetWriteDeadline outside writeMu, racing with a
concurrent Write inside WriteMessage, the same pattern as wsCmd.Close.
net.Conn allows Close while Write is in progress, so drop the call and
rely on the deadline WriteControl already takes.

The TTY resize test's server exited before the goroutine's 10ms delay,
so it only covered the early return, not the stdin capture. Delay the
exit frame so the goroutine reads stdin while the command runs. That
also showed the goroutine calling Fd() on an *os.File the caller may
close afterwards, so capture the descriptor before Start returns.
Restore os.Stdin with t.Cleanup.

Each new or changed test fails under -race with its fix reverted.
@Mohith26

Mohith26 commented Nov 7, 2026

Copy link
Copy Markdown
Author

Thanks for the careful review. Both points are in 81018e3:

  • proxyConn.Close: dropped the SetWriteDeadline call, same as wsCmd.Close. Added TestProxyConnCloseDoesNotRaceWrite (a writer looping on Write while another goroutine calls Close); it fails under -race at proxy.go:101 vs the WriteMessage path with the fix reverted. I left proxyConn.SetWriteDeadline alone: taking writeMu there would block behind a stuck Write, which defeats the point of setting a deadline to unblock it.
  • TTY test: the server now sends its exit frame after 50ms, so the goroutine gets past the timer and reads stdin. Doing that surfaced a second issue: the goroutine called Fd() on the captured *os.File after the test closed the pipe, which -race flags too. So it now captures int(os.Stdin.Fd()) before Start returns. With only the capture reverted (select kept), the test fails under -race on every run (3/3).
  • Also switched the os.Stdin restore to t.Cleanup and softened the "no longer outlives" wording in the description.

go vet ./..., go test -race ./..., -count=10 on the three race tests, and golangci-lint v2.11.3 are clean.

@Mohith26 Mohith26 closed this by deleting the head repository Nov 17, 2026
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.

Two data races in the websocket exec path: Close vs writeLoop, and the TTY resize goroutine reading os.Stdin

2 participants