Skip to content

Conversation

ckantcs
Copy link
Contributor

@ckantcs ckantcs commented Aug 19, 2025

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

		if migr.Body != nil {
			m.logVerbosePrintf("Read and execute %v\n", migr.LogString())
			if err := m.databaseDrv.Run(migr.BufferedBody); err != nil {
				return err
			}
		}

This PR introduces change to always close bufferWriter so that BufferedBody readers are not blocked in case Buffer() method returns without closing the bufferWriter.

@ckantcs
Copy link
Contributor Author

ckantcs commented Aug 19, 2025

@dhui could you pls check this small PR

@coveralls
Copy link

coveralls commented Aug 19, 2025

Coverage Status

coverage: 56.309% (-0.005%) from 56.314%
when pulling 7b96ecb on ckantcs:master
into 2788339 on golang-migrate:master.

Copy link
Member

@dhui dhui left a 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 {
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

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 {
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

@ckantcs
Copy link
Contributor Author

ckantcs commented Aug 20, 2025

@dhui thanks a lot for the review. I have addressed the comments. pls check

Copy link
Member

@dhui dhui left a 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
Copy link
Member

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?

Copy link
Contributor Author

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) {
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done. renamed to berr

@ckantcs
Copy link
Contributor Author

ckantcs commented Aug 23, 2025

@dhui addressed the comments. plese check once

Copy link
Member

@dhui dhui left a 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!

@dhui dhui merged commit ed4bdd4 into golang-migrate:master Aug 23, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants