fix: prevent double-close in partsReader by advancing index after Close#60
Merged
Conversation
In partsReader.Read, when Close() returns an error after EOF, r.index was not incremented — causing the same already-closed reader to be closed again by the outer Close() method. In partsReader.WriteTo, none of the three error-return paths incremented r.index after closing the reader, leading to the same double-close bug. Fix: increment r.index before returning in both error paths, so that a closed reader is always marked as consumed regardless of Close/io.Copy outcome. Closes #57 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a resource-lifecycle bug in pkg/iobuf’s composite partsReader where failures during per-part Close() could leave r.index unchanged, causing later Close() calls to double-close the same underlying reader (issue #57). It also adds unit tests covering the regression scenarios.
Changes:
- Advance
r.indexwhenRead()encountersClose()errors afterio.EOF, preventing subsequent double-close. - Advance
r.indexonWriteTo()error paths for the same double-close prevention. - Add 3 unit tests to cover the fixed scenarios.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
pkg/iobuf/part_reader.go |
Advances internal index on specific error paths in Read/WriteTo to avoid double-closing parts. |
pkg/iobuf/part_reader_test.go |
Adds regression tests for index advancement on Close()/copy errors and successful WriteTo() behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+68
to
+72
| @@ -68,6 +69,7 @@ func (r *partsReader) WriteTo(w io.Writer) (n int64, err error) { | |||
| nn, err = io.Copy(w, reader) | |||
| n += nn | |||
| if err != nil { | |||
| r.index++ | |||
Comment on lines
+29
to
+47
| // eofCloseErrReader returns data + io.EOF in a single Read, and returns the given error on Close. | ||
| type eofCloseErrReader struct { | ||
| data string | ||
| readOnce bool | ||
| closeErr error | ||
| closeCount *int | ||
| } | ||
|
|
||
| func (r *eofCloseErrReader) Read(p []byte) (int, error) { | ||
| if r.readOnce { | ||
| return 0, io.EOF | ||
| } | ||
| r.readOnce = true | ||
| n := copy(p, r.data) | ||
| if n == 0 { | ||
| return 0, io.EOF | ||
| } | ||
| return n, io.EOF | ||
| } |
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
partsReader.Read中Close()返回错误时r.index未递增,导致后续Close()重复关闭已关闭的 readerpartsReader.WriteTo中所有错误退出路径均未递增r.index的同源问题根因
见 #57
Test plan
TestPartsReader_Read_CloseErrorAdvancesIndex— 验证 Read 中 Close 失败后索引前进TestPartsReader_WriteTo_ErrorAdvancesIndex— 验证 WriteTo 中 io.Copy 错误后索引前进TestPartsReader_WriteTo_AllSuccess— 验证正常 WriteTo 全路径pkg/iobuf测试通过Closes #57
🤖 Generated with Claude Code