Fix data race on Connection logger field - #59
Open
dimetron wants to merge 1 commit into
Open
Conversation
NewConnection spawns the receive and processNotifications goroutines in its constructor, so a caller can only reach SetLogger after those goroutines are already running. SetLogger wrote the logger field with no synchronization while they read it through loggerOrDefault, so any call to SetLogger raced the connection's own logging. Under `go test -race` this aborts every test that connects an ACP subprocess. Store the logger in an atomic.Pointer instead. A single lock-free atomic load covers the hot loggerOrDefault read path with no mutex contention, and no lock-reentrancy risk against the connection's other mutexes; the pointer is replaced wholesale, so an atomic is sufficient. Add a regression test that feeds the connection unparseable input (which makes receive log) while calling SetLogger concurrently. It fails under -race without this change and passes with it. Verified: go test -race ./... and go build ./example/... pass. Fixes coder#57 Signed-off-by: Dmytro Rashko <dmitriy.rashko@amdocs.com>
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.
Fixes #57.
The race
NewConnectionspawnsreceiveandprocessNotificationsin its constructor, so a caller can only reachSetLoggerafter those goroutines are already running.SetLoggerwrote theloggerfield with no synchronization while they read it throughloggerOrDefault, so installing a logger races the connection's own logging.Under
go test -racethis aborts every test that connects an ACP subprocess — which is how it surfaced downstream (pi-go pins a patched copy of v0.13.5 behind ago.mod replacetoday; this PR is that patch, upstreamed).The change
loggerbecomes anatomic.Pointer[slog.Logger]:A single lock-free atomic load covers the hot
loggerOrDefaultread path: no mutex contention, and no lock-reentrancy risk against the connection's other mutexes. The pointer is replaced wholesale rather than mutated, so an atomic is sufficient — async.RWMutexwould buy nothing here.No API change:
SetLogger's signature and semantics are unchanged, andsync/atomicwas already imported.A constructor-level
WithLogger(...)option would also close the hole and would let the very first log line carry the caller's logger, but that is an API addition; this keeps the fix to the minimum and leaves that option open.Test
TestConnectionSetLogger_ConcurrentWithReceivefeeds the connection unparseable lines — which drivereceiveintologgerOrDefault— while callingSetLoggerconcurrently. It fails under-racewithout the change:and passes with it.
Verified
go test -race ./...— passgo build ./example/...— passgofmt -l .— clean