-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Ensure bufferWriter is always closed in Migration.Buffer and propagate close errors #1308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
89e4f3e
Ensure bufferWriter is always closed and error is returned in Buffer()
chandrakant-cohesity 849fb88
Ensure bufferWriter is always closed and error is returned in Buffer()
chandrakant-cohesity 0701543
addressed review comments. used named error and joined error to retur…
chandrakant-cohesity 7b96ecb
Fixed defer block
chandrakant-cohesity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package migrate | |
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
@@ -118,7 +119,7 @@ func (m *Migration) LogString() string { | |
|
||
// Buffer buffers Body up to BufferSize. | ||
// Calling this function blocks. Call with goroutine. | ||
func (m *Migration) Buffer() error { | ||
func (m *Migration) Buffer() (err error) { | ||
if m.Body == nil { | ||
return nil | ||
} | ||
|
@@ -127,34 +128,41 @@ func (m *Migration) Buffer() error { | |
|
||
b := bufio.NewReaderSize(m.Body, int(m.BufferSize)) | ||
|
||
// defer closing buffer writer and body. | ||
// defer blocks run in reverse order | ||
|
||
|
||
// close the Body. | ||
defer func() { | ||
if cerr := m.Body.Close(); cerr != nil { | ||
err = errors.Join(err, cerr) | ||
} | ||
}() | ||
|
||
// always close bufferWriter, even on error, to prevent deadlocks. | ||
// this lets Buffer know that there is no more data coming. | ||
defer func() { | ||
if cerr := m.bufferWriter.Close(); cerr != nil { | ||
err = errors.Join(err, cerr) | ||
} | ||
}() | ||
|
||
// start reading from body, peek won't move the read pointer though | ||
// poor man's solution? | ||
if _, err := b.Peek(int(m.BufferSize)); err != nil && err != io.EOF { | ||
return err | ||
if _, perr := b.Peek(int(m.BufferSize)); perr != nil && perr != io.EOF { | ||
return perr | ||
} | ||
|
||
m.FinishedBuffering = time.Now() | ||
|
||
// write to bufferWriter, this will block until | ||
// something starts reading from m.Buffer | ||
n, err := b.WriteTo(m.bufferWriter) | ||
if err != nil { | ||
return err | ||
n, werr := b.WriteTo(m.bufferWriter) | ||
if werr != nil { | ||
return werr | ||
} | ||
|
||
m.FinishedReading = time.Now() | ||
m.BytesRead = n | ||
|
||
// close bufferWriter so Buffer knows that there is no | ||
// more data coming | ||
if err := m.bufferWriter.Close(); err != nil { | ||
return err | ||
} | ||
|
||
// it's safe to close the Body too | ||
if err := m.Body.Close(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename this so we can continue to use
err
belowThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done. renamed to berr