Skip to content

conntrack: fix races in InstrumentedConn.Error, Idle and Stats - #389

Merged
shubh-stripe merged 2 commits into
masterfrom
shubh/conn-error-lock
Nov 25, 2026
Merged

shubh-stripe merged 2 commits into
masterfrom
shubh/conn-error-lock

Conversation

@shubh-stripe

@shubh-stripe shubh-stripe commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

This fixes the races from #369 and #370 on master. The same fixes for the v1 branch are in #390.

@rohith-sierra found the Error race 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.

Error takes the connection lock

InstrumentedConn.Close reads ConnError while it holds the connection's lock. Error wrote ConnError without 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. Error now takes the lock. Close doesn't wait for other goroutines while it holds the lock, and Error never runs from inside Close, so the change can't deadlock.

Idle and Stats read the counters atomically

Read and Write update LastActivity, BytesIn and BytesOut with atomic operations and without the lock. Idle and Stats read the same fields with plain loads, so they raced with an active tunnel. Idle runs from Close at shutdown, and Stats runs for the stats socket. They now use atomic.LoadInt64 and atomic.LoadUint64, as Close already does.

Test plan

I tested this with race tests that this PR leaves out. They have no ordering between the two goroutines.

  • Error against Close: on master without the fix it reports DATA RACE, and with the fix it passes 20 runs in a row under -race.
  • Idle and Stats against Write: each reports DATA RACE on master without the fix, and with the fix they pass 20 runs in a row under -race.

🤖 Generated with Claude Code

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
@shubh-stripe shubh-stripe changed the title conntrack: take the connection lock in InstrumentedConn.Error conntrack: fix races in InstrumentedConn.Error, Idle and Stats Nov 25, 2026
@shubh-stripe
shubh-stripe merged commit a1f3239 into master Nov 25, 2026
21 checks passed
@shubh-stripe
shubh-stripe deleted the shubh/conn-error-lock branch November 25, 2026 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants