Skip to content
Merged
Changes from all 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
36 changes: 31 additions & 5 deletions db/test_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,52 @@ func NewTestDBHandleFromPath(t testing.TB, dbPath string) *PostgresStore {
log := btclog.Disabled

testPgFixtureMtx.Lock()
defer testPgFixtureMtx.Unlock()

sqlFixture, ok := testPgFixturesByPath[dbPath]
testPgFixtureMtx.Unlock()

if !ok {
// First handle for this path: stand up the database, run
// migrations via the default store path, and register teardown.
// This must happen outside testPgFixtureMtx: creating the
// fixture can block on the global Postgres fixture semaphore,
// and cleanup needs the same mutex to evict finished fixtures.
sqlFixture = NewTestPgFixture(
t, DefaultPostgresFixtureLifetime, true,
Comment on lines +70 to 74
)

store, err := NewPostgresStore(sqlFixture.GetConfig(), log)
require.NoError(t, err)

testPgFixtureMtx.Lock()
existingFixture, alreadyCreated := testPgFixturesByPath[dbPath]
if alreadyCreated {
testPgFixtureMtx.Unlock()

_ = store.DB.Close()
sqlFixture.TearDown(t)

storeCfg := existingFixture.GetConfig()
storeCfg.SkipMigrations = true
store, err := NewPostgresStore(storeCfg, log)
require.NoError(t, err)

t.Cleanup(func() {
_ = store.DB.Close()
})

return store
}

testPgFixturesByPath[dbPath] = sqlFixture
testPgFixtureMtx.Unlock()

t.Cleanup(func() {
sqlFixture.TearDown(t)

testPgFixtureMtx.Lock()
delete(testPgFixturesByPath, dbPath)
if testPgFixturesByPath[dbPath] == sqlFixture {
delete(testPgFixturesByPath, dbPath)
}
testPgFixtureMtx.Unlock()
})
Comment on lines 103 to 111

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

There is a race condition in the cleanup function: sqlFixture.TearDown(t) is called before the fixture is evicted from the testPgFixturesByPath map under the lock. Since TearDown stops and purges the Docker container (which can take several seconds), there is a window of time where a concurrent or subsequent call to NewTestDBHandleFromPath for the same dbPath will find the fixture in the map (ok == true), but the underlying container is already stopped or being stopped. This will result in connection failures.

To prevent this, we should lock the mutex and delete the fixture from the map before calling TearDown(t). This ensures that any subsequent caller will not see the stale fixture in the map and will instead spin up a new one, while the teardown of the old fixture can safely run outside the lock.

Suggested change
t.Cleanup(func() {
sqlFixture.TearDown(t)
testPgFixtureMtx.Lock()
delete(testPgFixturesByPath, dbPath)
if testPgFixturesByPath[dbPath] == sqlFixture {
delete(testPgFixturesByPath, dbPath)
}
testPgFixtureMtx.Unlock()
})
t.Cleanup(func() {
testPgFixtureMtx.Lock()
if testPgFixturesByPath[dbPath] == sqlFixture {
delete(testPgFixturesByPath, dbPath)
}
testPgFixtureMtx.Unlock()
sqlFixture.TearDown(t)
})


Expand All @@ -90,8 +118,6 @@ func NewTestDBHandleFromPath(t testing.TB, dbPath string) *PostgresStore {
_ = store.DB.Close()
})

testPgFixturesByPath[dbPath] = sqlFixture

return store
}

Expand Down
Loading