Repository navigation
conntrack: fix races in InstrumentedConn.Error, Idle and Stats - #389
Merged
Merged
Conversation
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. rohith-sierra found this race and reported it in #369. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Committed-By-Agent: claude
Read and Write update LastActivity, BytesIn and BytesOut with atomic operations and without the connection's lock. Idle and Stats read the same fields with plain loads, so they raced with an active tunnel. Close already reads the byte counters atomically, and Idle and Stats now do the same. gabriela-cw found this race and reported it in #370. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Committed-By-Agent: claude
gauthamw-stripe
approved these changes
Nov 25, 2026
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
master. The same fixes for thev1branch are in #390.@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: onmasterwithout the fix it reportsDATA RACE, and with the fix it passes 20 runs in a row under-race.IdleandStatsagainstWrite: each reportsDATA RACEonmasterwithout the fix, and with the fix they pass 20 runs in a row under-race.🤖 Generated with Claude Code