fix: prevent 'closed pipe' errors with thread-safe segment writer - #156
Merged
Conversation
Fixes race condition where downloads write to closed segment pipes, causing "bufpipe: read/write on closed pipe" errors. Changes: - Add closed flag to track segment state - Implement safeWriter wrapper for thread-safe writes - Return io.ErrClosedPipe instead of panicking on closed writes - Make Close() idempotent to prevent multiple close issues - Add comprehensive tests for concurrent access patterns Tests include: - Write after close behavior - Concurrent write and close race conditions - Idempotent close operations - Multiple concurrent writers with close - Race detector validation (8 test cases, all passing) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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
Fixes the
"bufpipe: read/write on closed pipe"error that occurs when background download goroutines attempt to write to segment pipes that have been closed due to reader cancellation or context cancellation.Problem
Race condition in
internal/usenet/segment.go:cp.Body()which writes to segment writerClose()is called (from reader close or context cancellation)segment.Close()closes the writer pipeBody()write operation fails with "closed pipe" panicSolution
Implemented thread-safe writer wrapper:
closedflag to track segment statesafeWriterthat checks closed status before writesio.ErrClosedPipeinstead of causing panicClose()idempotent to handle multiple calls safelyChanges
Modified Files
internal/usenet/segment.go- Thread-safe writer implementationNew Files
internal/usenet/segment_test.go- Comprehensive test coverage (8 tests)Testing
✅ All 16 tests pass (8 new + 8 existing)
✅ No race conditions detected with
-raceflag✅ Existing functionality preserved
Test Coverage
Impact
🤖 Generated with Claude Code