Skip to content

Fix asyncWriter data race and send-on-closed-channel panic#345

Merged
clcollins merged 2 commits into
mainfrom
srepd/asyncwriter-race
Jul 9, 2026
Merged

Fix asyncWriter data race and send-on-closed-channel panic#345
clcollins merged 2 commits into
mainfrom
srepd/asyncwriter-race

Conversation

@clcollins

Copy link
Copy Markdown
Owner

Summary

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 (TOCTOU: Write passes the closed check, Close closes
aw.out, Write then sends).

An atomic.Bool fixes the race but not the panic (check and send are
separate ops on separate objects). Added a sync.Mutex guarding the
closed-check + channel send in Write and the close in Close.

Test plan

  • TDD: TestAsyncWriter_ConcurrentWriteClose written first; under -race it
    failed 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/... green

No README trigger files touched.

🤖 Generated with Claude Code

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-commenter

codecov-commenter commented Jul 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.46%. Comparing base (792c98a) to head (54dcd89).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@clcollins clcollins merged commit 0815e90 into main Jul 9, 2026
11 checks passed
@clcollins clcollins deleted the srepd/asyncwriter-race branch July 9, 2026 19:27
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