Skip to content

Context DB txn timeout, and interface to avoid connection leak during migration#211

Open
peterbroadhurst wants to merge 1 commit intoexternalize-tag-handlerfrom
migration-conn-leak
Open

Context DB txn timeout, and interface to avoid connection leak during migration#211
peterbroadhurst wants to merge 1 commit intoexternalize-tag-handlerfrom
migration-conn-leak

Conversation

@peterbroadhurst
Copy link
Contributor

This addresses two issues with the DB driver:

  1. When we Begin we're not supplying the context to allow the DB to automatically terminate the transaction when the context expires
  2. When using PostgreSQL with auto-migrate, we're leaking a single connection for the life of the process, because the WithInstance function would close the whole DB if we used driver.Close()
    • Solution is to use WithConnection() in the migration driver for PostgreSQL
    • Have to make this optional, as SQLite does not have a WithConnection()

Signed-off-by: Peter Broadhurst <peter.broadhurst@kaleido.io>
@peterbroadhurst peterbroadhurst requested a review from a team as a code owner March 17, 2026 15:41
before := time.Now()
l.Tracef("SQL-> begin")
sqlTX, err := s.db.Begin()
sqlTX, err := s.db.BeginTx(ctx /* transaction should auto-rollback on context cancel */, nil /* default options */)
Copy link
Contributor

Choose a reason for hiding this comment

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

👍🏻 - this is what Opus was whispering in my ear about when analyzing this previously. Thanks makes perfect sense

Comment on lines +144 to +153
var driver database.Driver
providerClosable, isClosable := provider.(ProviderCloseableMigrationDriver)
if isClosable {
driver, err = providerClosable.GetMigrationDriverClosable(s.db)
defer func() {
_ = driver.Close()
}()
} else {
driver, err = provider.GetMigrationDriver(s.db)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting - think this explains behavior I saw in some of our tests where when closing the migrations, it closed the whole driver, and caused other obscure failures in the tests 😵

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.

2 participants