Skip to content

trafficwatch: drop safeCloser, use TrafficWatch.JustClose() - #330

Merged
scott-cotton merged 1 commit into
mainfrom
fix-safe-closer-double-close
May 7, 2026
Merged

scott-cotton merged 1 commit into
mainfrom
fix-safe-closer-double-close

Conversation

@scott-cotton

Copy link
Copy Markdown
Member

Summary

Fixes a panic: close of closed channel from libconnect/common/trafficwatch.WatchTrafficClient.Watch.func1.1 reproducible under e2e + signadot traffic record against staging.

setupTW (in internal/trafficwatch/get.go) wrapped tw.Close in a local safeCloser whose own sync.Once was unrelated to the lib's closeOnce. The wrapper's "is it closed yet?" probe (select { case <-ch: default: close(ch) }) isn't atomic with the close, so two distinct Onces could each reach close(ch):

  1. lib goroutine enters closeCOnce.Do(...), grabs the mutex, starts running f, about to close(closeC).
  2. setupTW's goroutine enters its own once.Do(...); the <-ch non-blocking probe sees the channel still open (the lib hasn't closed it yet — only acquired the mutex), falls through to default, and calls close(ch).
  3. lib goroutine's f runs close(closeC) → panic.

The lib's TrafficWatch.JustClose() is exactly the right primitive: same closeOnce as the lib's five internal close(closeC) call sites, so they all coordinate. The docstring on TrafficWatch.Close in libconnect explicitly warns against close(tw.Close) directly.

This PR drops safeCloser and its dedicated test; setupTW now calls tw.JustClose() on signal / timeout.

Test plan

  • go build ./...
  • go vet ./...

🤖 Generated with Claude Code

setupTW wrapped tw.Close in a local safeCloser whose own sync.Once was
unrelated to the lib's closeOnce. The wrapper's `select { case <-ch:
default: close(ch) }` "is it closed yet?" check isn't atomic with the
close, so two distinct Onces could each reach close(ch):

  1. lib goroutine enters closeCOnce.Do(...), grabs the mutex, starts
     running f, about to close(closeC).
  2. setupTW's goroutine enters its own once.Do(...); the <-ch
     non-blocking probe sees the channel still open (the lib hasn't
     closed it yet — only acquired the mutex), falls through to
     default, and calls close(ch).
  3. lib goroutine's f runs close(closeC) → "close of closed channel".

Reproduces under e2e + traffic record against staging.

The lib's TrafficWatch.JustClose() is exactly the right primitive
here: same closeOnce as the lib's internal close sites, so all
five close(closeC) call sites coordinate. The docstring on
TrafficWatch.Close in libconnect explicitly warns against direct
close.

Drop safeCloser and its dedicated test; setupTW now calls
tw.JustClose() on signal/timeout.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

@daniel-de-vera daniel-de-vera left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@scott-cotton
scott-cotton merged commit 1300998 into main May 7, 2026
@scott-cotton
scott-cotton deleted the fix-safe-closer-double-close branch May 7, 2026 11: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