Skip to content

fix: prevent 'closed pipe' errors with thread-safe segment writer - #156

Merged
javi11 merged 1 commit into
mainfrom
fix/thread-safe-segment-writer
Dec 22, 2025
Merged

fix: prevent 'closed pipe' errors with thread-safe segment writer#156
javi11 merged 1 commit into
mainfrom
fix/thread-safe-segment-writer

Conversation

@javi11

@javi11 javi11 commented Dec 21, 2025

Copy link
Copy Markdown
Collaborator

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:

  1. Download goroutine calls cp.Body() which writes to segment writer
  2. Meanwhile, Close() is called (from reader close or context cancellation)
  3. segment.Close() closes the writer pipe
  4. In-flight Body() write operation fails with "closed pipe" panic

Solution

Implemented thread-safe writer wrapper:

  • Added closed flag to track segment state
  • Created safeWriter that checks closed status before writes
  • Returns io.ErrClosedPipe instead of causing panic
  • Made Close() idempotent to handle multiple calls safely

Changes

Modified Files

  • internal/usenet/segment.go - Thread-safe writer implementation

New 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 -race flag
✅ Existing functionality preserved

Test Coverage

  • Write after close behavior
  • Concurrent write and close race conditions
  • Idempotent close operations
  • Multiple concurrent writers with close
  • Nil writer/segment edge cases
  • Race detector validation (20 iterations)

Impact

  • No more "closed pipe" panics during downloads
  • Graceful error handling when segments close during download
  • Downloads can be safely cancelled without crashes
  • Error is handled by existing retry logic

🤖 Generated with Claude Code

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>
@javi11
javi11 merged commit 8050a64 into main Dec 22, 2025
1 check passed
@javi11
javi11 deleted the fix/thread-safe-segment-writer branch February 4, 2026 13:41
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