Skip to content

conntrack: fix races in InstrumentedConn.Error, Idle and Stats (v1 branch) - #390

Merged
shubh-stripe merged 1 commit into
v1from
shubh/v1-conn-error-lock
Nov 25, 2026
Merged

shubh-stripe merged 1 commit into
v1from
shubh/v1-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 the v1 branch. The same fixes for master are in #389.

@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 v1 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 v1 without the fix, and with the fix they pass 20 runs in a row under -race.

🤖 Generated with Claude Code

@shubh-stripe shubh-stripe changed the title conntrack: take the connection lock in InstrumentedConn.Error (v1 branch) conntrack: fix races in InstrumentedConn.Error, Idle and Stats (v1 branch) 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
shubh-stripe force-pushed the shubh/v1-conn-error-lock branch from 5498f9b to 3a6406d Compare November 25, 2026 14:43
@shubh-stripe
shubh-stripe merged commit a622ac3 into v1 Nov 25, 2026
29 checks passed
@shubh-stripe
shubh-stripe deleted the shubh/v1-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