Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ func (m *Migration) Buffer() error {

b := bufio.NewReaderSize(m.Body, int(m.BufferSize))

var bufferWriterCloseErr error
// Always close bufferWriter, even on error, to prevent deadlocks.
// This lets Buffer know that there is no more data coming.
defer func() {
if err := m.bufferWriter.Close(); err != nil && bufferWriterCloseErr == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a named return and errors.Join(). See this example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

bufferWriterCloseErr = err
}
}()

// 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 {
Expand All @@ -145,16 +154,14 @@ func (m *Migration) Buffer() error {
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should also defer this Close() and preserve the close ordering

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

return err
}

if bufferWriterCloseErr != nil {
return bufferWriterCloseErr
}

return nil
}