[Issue 1483] Fix sync: WaitGroup is reused panic in connection close path#1489
Open
SAY-5 wants to merge 1 commit intoapache:masterfrom
Open
[Issue 1483] Fix sync: WaitGroup is reused panic in connection close path#1489SAY-5 wants to merge 1 commit intoapache:masterfrom
sync: WaitGroup is reused panic in connection close path#1489SAY-5 wants to merge 1 commit intoapache:masterfrom
Conversation
failLeftRequestsWhenClose previously called incomingRequestsWG.Wait() without first stopping new SendRequest / SendRequestNoWait callers. Both callers did Add(1) before checking the connection state, so a caller that started concurrently with the close path could Add(1) at the moment Wait() was draining to zero, tripping Go's panic: sync: WaitGroup is reused before previous Wait has returned (reported in apache#1483 and seen in production close paths). Add a sync.RWMutex (incomingRequestsLock) that: * SendRequest / SendRequestNoWait take RLock, check state, Add(1), RUnlock — so multiple senders still proceed in parallel; and * failLeftRequestsWhenClose takes Lock, sets state=closed, Unlocks, then Wait()s. The exclusive section guarantees no in-flight Add(1) is straddling the Wait() and that any new caller arriving after the close sees state=closed and returns ErrConnectionClosed without touching the WaitGroup. Closes apache#1483. Signed-off-by: SAY-5 <say.apm35@gmail.com>
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.
Fixes #1483.
Motivation
failLeftRequestsWhenClosecallsc.incomingRequestsWG.Wait()to drain in-flightSendRequest/SendRequestNoWaitcallers, but those callers doincomingRequestsWG.Add(1)before checkingc.getState() == connectionClosed. A caller that started concurrently with the close path can therefore executeAdd(1)at the exact momentWait()is finishing draining to zero, which Go treats as a reuse violation:This is the production crash captured in #1483.
Modifications
Add a
sync.RWMutex(incomingRequestsLock) that gates theAdd(1)so it cannot interleave withWait():SendRequest/SendRequestNoWaittake RLock, check state,Add(1), RUnlock. Multiple senders still proceed in parallel.failLeftRequestsWhenClosetakes Lock, sets state=closed, Unlocks, thenWait(). The exclusive section guarantees:Add(1)straddles theWait(), andstate == connectionClosedand returnsErrConnectionClosedwithout touching the WaitGroup.Inline comments in both call sites document why the lock is necessary so future contributors don't re-introduce the race.
Verifying this change
go build ./pulsar/internal/...passesgo vet ./pulsar/internal/...passesgo test -count=1 -short ./pulsar/internal/— all internal tests still passDocumentation
doc-not-needed(bug fix in internal package; no public API change)