Repository navigation
trafficwatch: drop safeCloser, use TrafficWatch.JustClose() - #330
Merged
Merged
Conversation
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]>
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.
Summary
Fixes a
panic: close of closed channelfromlibconnect/common/trafficwatch.WatchTrafficClient.Watch.func1.1reproducible under e2e +signadot traffic recordagainst staging.setupTW(ininternal/trafficwatch/get.go) wrappedtw.Closein a localsafeCloserwhose ownsync.Oncewas unrelated to the lib'scloseOnce. 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 reachclose(ch):closeCOnce.Do(...), grabs the mutex, starts runningf, about toclose(closeC).setupTW's goroutine enters its ownonce.Do(...); the<-chnon-blocking probe sees the channel still open (the lib hasn't closed it yet — only acquired the mutex), falls through todefault, and callsclose(ch).frunsclose(closeC)→ panic.The lib's
TrafficWatch.JustClose()is exactly the right primitive: samecloseOnceas the lib's five internalclose(closeC)call sites, so they all coordinate. The docstring onTrafficWatch.Closein libconnect explicitly warns againstclose(tw.Close)directly.This PR drops
safeCloserand its dedicated test;setupTWnow callstw.JustClose()on signal / timeout.Test plan
go build ./...go vet ./...🤖 Generated with Claude Code