-
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
Conversation
@dhui could you pls check this small PR |
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.
@ckantcs I left some comments
migration.go
Outdated
// 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 { |
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.
Use a named return and errors.Join()
. See this example
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.
addressed
migration.go
Outdated
if err := m.bufferWriter.Close(); err != nil { | ||
return err | ||
} | ||
|
||
// it's safe to close the Body too | ||
if err := m.Body.Close(); err != nil { |
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.
should also defer this Close()
and preserve the close ordering
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.
addressed
@dhui thanks a lot for the review. I have addressed the comments. pls check |
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.
@ckantcs Thanks for addressing my feedback. I have a few more comments to improve readability
migration.go
Outdated
@@ -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 |
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.
Can we use a single defer
instead to avoid this confusion?
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.
done
migration.go
Outdated
@@ -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) { |
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
below
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.
done. renamed to berr
@dhui addressed the comments. plese check once |
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.
@ckantcs Thanks again for the PR!
This PR updates the Migration.Buffer() function to always close the bufferWriter using a deferred call, even if an error occurs during migration buffering.
We have big migration files and once in a while we saw database driver getting stuck on reading the buffered body.
Below is the code snippet in migrate.go where after printing "Read and execute" statement, the migration got stuck and did not proceed further
This PR introduces change to always close bufferWriter so that BufferedBody readers are not blocked in case Buffer() method returns without closing the bufferWriter.