Repository navigation
conntrack: fix races in InstrumentedConn.Error, Idle and Stats (v1 branch) - #390
Merged
Merged
Conversation
gauthamw-stripe
approved these changes
Nov 25, 2026
Close reads ConnError while it holds the connection's lock, and the tracker can close a connection from another goroutine at shutdown. Error wrote ConnError without the lock, so an error report and a close at the same time raced. Error now takes the lock. Read and Write update LastActivity, BytesIn and BytesOut with atomic operations and without the lock, but Idle and Stats read them with plain loads. They now use atomic loads, as Close already does. rohith-sierra reported the Error race in #369, and gabriela-cw reported the counter race in #370. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
shubh-stripe
force-pushed
the
shubh/v1-conn-error-lock
branch
from
November 25, 2026 14:43
5498f9b to
3a6406d
Compare
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.
This fixes the races from #369 and #370 on the
v1branch. The same fixes formasterare in #389.@rohith-sierra found the
Errorrace and reported it in #369. @gabriela-cw found the counter race and reported it in #370. Thank you both for the clear reports and reproductions. This PR fixes both with a separate implementation, so we can merge it while the CLAs on #369 and #370 are open.Errortakes the connection lockInstrumentedConn.ClosereadsConnErrorwhile it holds the connection's lock.ErrorwroteConnErrorwithout the lock. The tracker can close a connection from another goroutine, for example at shutdown, so an error report and a close at the same moment raced.Errornow takes the lock.Closedoesn't wait for other goroutines while it holds the lock, andErrornever runs from insideClose, so the change can't deadlock.IdleandStatsread the counters atomicallyReadandWriteupdateLastActivity,BytesInandBytesOutwith atomic operations and without the lock.IdleandStatsread the same fields with plain loads, so they raced with an active tunnel.Idleruns fromCloseat shutdown, andStatsruns for the stats socket. They now useatomic.LoadInt64andatomic.LoadUint64, asClosealready does.Test plan
I tested this with race tests that this PR leaves out. They have no ordering between the two goroutines.
ErroragainstClose: onv1without the fix it reportsDATA RACE, and with the fix it passes 20 runs in a row under-race.IdleandStatsagainstWrite: each reportsDATA RACEonv1without the fix, and with the fix they pass 20 runs in a row under-race.🤖 Generated with Claude Code