Fix race condition by copying chunk data in Subscriber's read method #8
  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.
  
    
  
    
Motivation / Background
This PR addresses a concurrency-related bug in
Subscriber.read()where slices returned bybufio.Reader.ReadSlicewere shared between iterations, leading to race conditions when the consumer of the channel retained or mutated the chunk after it was reused by the reader.This issue has been documented in #581. The race occurs because
ReadSlicereuses an internal buffer, so passing the slice directly through the channel can lead to unsafe access if it is used concurrently elsewhere.Fixes #581
Details
To resolve the race condition, the read loop now copies the chunk into a new slice before sending it over the channel. This ensures that each message chunk sent to downstream consumers is a safe, isolated copy that won't be invalidated by future reads.
Benchmarking showed a negligible decrease in throughput in exchange for guaranteed safety in concurrent environments.
Alternative approaches considered
Checklist
make up.make test_shortfor a quick check.make test.