Skip to content

Fix data race on chunks shared by WriteDedupQueue#373

Merged
folbricht merged 1 commit into
masterfrom
writededupqueue-chunk-race
Jul 10, 2026
Merged

Fix data race on chunks shared by WriteDedupQueue#373
folbricht merged 1 commit into
masterfrom
writededupqueue-chunk-race

Conversation

@folbricht

Copy link
Copy Markdown
Owner

Follow-up to #372, which fixed the same defect in DedupQueue.GetChunk but missed the sibling path: WriteDedupQueue.GetChunk returns the chunk of an in-flight StoreChunk request for the same ID to every waiting reader. That chunk is the exact *Chunk the storing caller passed in and continues to use, and Chunk.Data()/ID() memoize internal fields without synchronization — so concurrent readers race with each other and with the writer.

Same fix shape as #372: StoreChunk publishes a shallow copy detached from the caller's chunk via markDone, and each waiting reader receives its own copy of that master. The master is never handed out or mutated, markDone's channel close provides the happens-before edge, and the copies share the underlying read-only data so nothing is duplicated.

The added test stores a chunk while ten readers request the same ID (deterministically interleaved with testing/synctest): without the fix it fails under -race and on the shared-pointer assertion; with the fix the full library suite passes under -race.

WriteDedupQueue.GetChunk hands the chunk of an in-flight StoreChunk
request for the same ID to every waiting reader. The chunk published
in the request is the one the storing caller passed in and keeps
using, and Chunk lazily materializes its plain data without locking,
so concurrent readers race with each other and with the writer on the
chunk's internal fields.

Apply the same fix as for DedupQueue.GetChunk in #372: StoreChunk
publishes a shallow copy detached from the caller's chunk, and every
waiting reader receives its own copy of that. The copies share the
underlying read-only data, so no chunk data is duplicated.

The new test fails with -race (and on the shared-pointer assertion)
without this fix.
@folbricht folbricht merged commit 80e9967 into master Jul 10, 2026
3 checks passed
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.

1 participant