Skip to content

Commit

Permalink
pg: avoid hang on orphaned transcation panic (#1123)
Browse files Browse the repository at this point in the history
When a test or an application panics due to an orphaned transaction
when trying to close the DB.  This change avoids the hang by
automatically rolling back the orphaned transaction and closing the DB
before the explicit panic.
  • Loading branch information
jchappelow authored Dec 6, 2024
1 parent 2d7128d commit 2d88790
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions node/pg/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ func (db *DB) EnsureFullReplicaIdentityDatasets(ctx context.Context) error {
func (db *DB) Close() error {
db.cancel(nil)
db.repl.stop()
if db.tx != nil {
// This is a bug, so we are going to panic so the issue is not easily
// ignored, but we will rollback the tx so we don't hang or leak
// postgresql resources.
db.tx.Rollback(context.Background())
db.tx = nil
db.txid = ""
db.pool.Close()
panic("Closed the DB with an active transaction!")
}
return db.pool.Close()
}

Expand Down

0 comments on commit 2d88790

Please sign in to comment.