Fix asyncWriter data race and send-on-closed-channel panic#345
Merged
Conversation
The closed flag was read in Write (logging goroutine) and written in Close (shutdown) with no synchronization: a data race, plus a send-on-closed-channel panic (Write passes the closed check, Close closes aw.out, Write then sends). An atomic.Bool fixes the race but not the panic — the check and the send are separate ops on separate objects. Add a sync.Mutex guarding the closed-check and the channel send in Write, and the close in Close, so a Write can never send on an already-closed channel. Reproduced under -race first (data race + panic), green after the mutex. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #345 +/- ##
==========================================
+ Coverage 66.44% 66.46% +0.02%
==========================================
Files 51 51
Lines 8177 8183 +6
==========================================
+ Hits 5433 5439 +6
Misses 2369 2369
Partials 375 375 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
The
closedflag was read inWrite(logging goroutine) and written inClose(shutdown) with no synchronization — a data race, plus a
send on closed channelpanic (TOCTOU:Writepasses theclosedcheck,Closeclosesaw.out,Writethen sends).An
atomic.Boolfixes the race but not the panic (check and send areseparate ops on separate objects). Added a
sync.Mutexguarding theclosed-check + channel send inWriteand the close inClose.Test plan
TestAsyncWriter_ConcurrentWriteClosewritten first; under-raceitfailed with both a data-race report and the panic (confirming atomic-alone
is insufficient), then green after the mutex
make test-all+go test -race ./cmd/...greenNo README trigger files touched.
🤖 Generated with Claude Code