fix(server): close conns channel only after accept loops exit - #3755
Open
m-u-xyz wants to merge 1 commit into
Open
fix(server): close conns channel only after accept loops exit#3755m-u-xyz wants to merge 1 commit into
m-u-xyz wants to merge 1 commit into
Conversation
Listener.Close raced with its own accept loops: it closed the conns channel (inside the sync.Once, before eg.Wait) while the loops could still be selected to send into it. Go's select chooses randomly among ready cases, so a loop that had just accepted a connection could pick the `conns <-` arm over the `<-shutdown` arm and send into a channel being closed — panicking (`send on closed channel`) or tripping the race detector under -race. This manifested in downstream dolt's sql-server tests as a flaky `runtime.closechan` vs `runtime.chansend` race on server/listener.go during rapid start/stop. Fix: split the single sync.Once into shutdownOnce and connsOnce so Close can sequence the two closings around eg.Wait(): 1. close shutdown (loops observe it and return) 2. close the underlying net.Listeners (unblocks Accept -> loops exit) 3. eg.Wait() — all senders are now guaranteed gone 4. close conns — safe, no sender remains Accept()'s contract is preserved: it still unblocks with net.ErrClosed because conns is still closed (just later, after the loops drain). Adds listener_race_test.go with a deterministic reproduction that fails (WARNING: DATA RACE) on the original code and passes on the fix, covering both the TCP and unix accept loops plus the post-Close Accept contract.
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.
Problem
Listener.Closeraced with its own accept loops, causing intermittentsend on closed channelpanics and-racefailures.The accept loops send accepted connections into
l.connsvia aselectthat also watches
l.shutdown:Go's
selectpicks randomly among ready cases.Closedid:So a loop that had just accepted a connection could win the
conns <-arm and send into a channel
Closewas closing concurrently →runtime.closechanvsruntime.chansendrace (panics or trips thedetector). This showed up downstream in dolt's
sql-servertests as aflaky race on
server/listener.go:101vs:149during rapidstart/stop.
Fix
Split the single
sync.OnceintoshutdownOnceandconnsOncesoClosecan sequence the two channel closings aroundeg.Wait():close(shutdown)— loops observe it and returnnet.Listeners — unblocksAccept(), loops exiteg.Wait()— all senders are now guaranteed goneclose(conns)— safe; no sender remainsAccept()'s contract is preserved: it still unblocks withnet.ErrClosedbecauseconnsis still closed, just after the loopsdrain rather than before.
Reproduction / test
server/listener_race_test.goadds a deterministic repro that:Listenerbut withholds theAccept()consumer so theloop's
conns <-send stalls inside the select,Closewhile that send is in flight,On the original code this fails under
-race:On the patched code it passes (
-race -count=5clean). Covers both theTCP and unix accept loops (they share
conns) plus the post-CloseAccept→net.ErrClosedcontract.Checklist
go build ./...go vet ./server/go test -race -count=5 -run TestListenerClose ./server/serverpackage tests still pass