fix: avoid deadlock on Read after WriteTo drain#71
Conversation
After WriteTo/io.Copy drained the stream, a subsequent Read blocked forever trying to return a nil buffer to a full blockPool. Short-circuit Read when lastBlock is set and current is empty, and only return buffers with capacity to the pool. Also ensure WriteTo never returns io.EOF so io.Copy does not surface it (klauspost#38). Fixes klauspost#39 Fixes klauspost#38
|
@coderabbitai review |
✅ Action performedReview finished.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
💤 Files with no reviewable changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughReader stream lifecycle handling now resets terminal state, avoids post-completion blocking, safely manages pooled buffers, and makes ChangesReader stream lifecycle
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@gunzip.go`:
- Around line 175-177: Preserve blockPool ownership on both abandonment paths in
gunzip.go: in the reset logic around lines 175-177, return z.current to
blockPool before clearing it; in the write/error handling around lines 570-584,
return read.b to blockPool before every short-write or writer-error return. Use
the existing pool-return mechanism and retain the current control flow
otherwise.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 538afa20-f79f-43d1-b69e-d4608884cc5f
📒 Files selected for processing (2)
gunzip.gogunzip_test.go
|
PTAL at the feedback and the formatting. |
|
Addressed the actionable feedback in 22ce4dd:
I did not add a second Reset return because killReadAhead already returns current and clears it, avoiding a double return. Validation: go test ./... passes. PTAL. |
Summary
Calling
io.Copy(which usesWriteTo) thenReadon the samepgzip.Readerdeadlocked:Root cause: after the final block,
Readstill didz.blockPool <- z.currentwith a nil/empty current while the pool was already full.Fix
lastBlockand no remainingcurrent,Readreturns0, io.EOFimmediatelyblockPoolWriteTonever returnsio.EOF(soio.Copydoes not surface it)Test plan
go test .TestReadAfterWriteToNoDeadlockTestWriteToDoesNotReturnEOFFixes #39
Fixes #38
Summary by CodeRabbit
WriteToend-of-stream behavior so callers don’t receiveio.EOF.WriteTocomplete do not deadlock and returnio.EOFwith 0 bytes.WriteToreturns the correct decompressed byte count and noio.EOFerror.