fix data race issue with calling wg.Add(1) after wg.Wait() #28
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.
In certain situations, it is possible that the
Close()
method could be called at the same time as a callback function likeReadTimeoutHandler
. This callback function could then call theSend
method, which would attempt to increment the counter of aWaitGroup
that has already hadWait
called on it (insideClose
). This scenario can lead to a data race, according to the data race detector.To fix this issue, we should ensure that
wg.Add(1)
is not called after or duringwg.Wait()
. This is achieved by puttingwg.Add(1)
into the mutex that controls the closing state of the connection.Some more context:
From the go documentation:
Explanation from Rob Pike: