fix: don't drop items when close races with the final put!#6
Merged
Conversation
The consumer-side drain checks in take!, take!(ch, output), wait and iterate read the head/tail indices, observed an empty buffer, and *then* separately read the closed flag. Because close() is ordered after every put! but the empty-snapshot and closed reads are distinct operations, a producer's final put! could become visible to the consumer only after its close(): the consumer would observe the channel as closed-and-empty and throw / end iteration, silently dropping the last item(s). Fix the drain checks to re-read the indices after observing closed: since close() is ordered after every put!, a re-read then observes all published items, so throwing only happens when the channel is genuinely drained. Also simplify iterate to always attempt the (now race-free) take! instead of guarding it with the racy `isopen(ch) || isready(ch)` snapshot; take! already drains before throwing and propagates bound-task exceptions. Add a threaded regression test that produces N items then closes and asserts the consumer iterates all N, over many trials. 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 #6 +/- ##
==========================================
+ Coverage 94.48% 99.27% +4.78%
==========================================
Files 1 1
Lines 145 137 -8
==========================================
- Hits 137 136 -1
+ Misses 8 1 -7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Benchmark Results (Julia v1)Time benchmarks
Memory benchmarks
|
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.
Problem
The consumer-side drain checks (
take!,take!(ch, output),wait, anditerate) read thehead/tailindices, observed an empty buffer, and then separately read theclosedflag.Although
close()is ordered after everyput!, the empty-snapshot read and theclosedread are distinct atomic operations. So a producer's finalput!can become visible to the consumer only after itsclose():head→ sees empty (old value)head(publishes item), then setsclosed = trueclosed→ seestrueThis surfaces as a rare (~0.1%) data-loss flake under load — e.g. a
tee/pipeline consumer receiving fewer items than were produced.Fix
Re-read the indices after observing
closed. Becauseclose()is ordered after everyput!, a re-read at that point observes all published items, so a throw only happens when the channel is genuinely drained:Applied to
take!,take!(ch, output), andwait. Also simplifiediterateto always attempt the (now race-free)take!rather than guarding it with the racyisopen(ch) || isready(ch)snapshot —take!already drains before throwing andcheck_closed_and_throwpropagates bound-task exceptions.Testing
Added a threaded regression test: produce N items,
close(), then iterate and assert all N are received, over many trials. It fails reliably on the old code and passes on the fixed code. Verified 0 failures over 60,000 additional stress trials (iterate + take! paths) locally with--threads=4.🤖 Generated with Claude Code