From 7cd7cef6c6830220ace2a7c25abc8b5e81247996 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Thu, 6 Jan 2022 13:23:41 +0100 Subject: [PATCH] htlcswitch: fix duplicate close The decayed log database opening and closing is managed at a higher level in config_builder.go. --- docs/release-notes/release-notes-0.14.2.md | 3 ++ go.mod | 2 +- htlcswitch/decayedlog.go | 3 -- htlcswitch/decayedlog_test.go | 37 ++++++++++++---------- kvdb/postgres/db.go | 2 ++ 5 files changed, 27 insertions(+), 20 deletions(-) diff --git a/docs/release-notes/release-notes-0.14.2.md b/docs/release-notes/release-notes-0.14.2.md index 775e5edcc..69b3dbbfa 100644 --- a/docs/release-notes/release-notes-0.14.2.md +++ b/docs/release-notes/release-notes-0.14.2.md @@ -53,6 +53,8 @@ * [Fix Postgres itests max connections](https://github.com/lightningnetwork/lnd/pull/6116) +* [Fix duplicate db connection close](https://github.com/lightningnetwork/lnd/pull/6140) + ## RPC Server * [ChanStatusFlags is now @@ -68,6 +70,7 @@ * Bjarne Magnussen * Elle Mouton * Harsha Goli +* Joost Jager * Martin Habovštiak * Naveen Srinivasan * Oliver Gugger diff --git a/go.mod b/go.mod index 0af0bdadf..3641c3579 100644 --- a/go.mod +++ b/go.mod @@ -46,7 +46,7 @@ require ( github.com/lightningnetwork/lnd/cert v1.1.0 github.com/lightningnetwork/lnd/clock v1.1.0 github.com/lightningnetwork/lnd/healthcheck v1.2.0 - github.com/lightningnetwork/lnd/kvdb v1.2.4 + github.com/lightningnetwork/lnd/kvdb v1.2.5 github.com/lightningnetwork/lnd/queue v1.1.0 github.com/lightningnetwork/lnd/ticker v1.1.0 github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796 diff --git a/htlcswitch/decayedlog.go b/htlcswitch/decayedlog.go index 5a39f37c3..44e4d1c51 100644 --- a/htlcswitch/decayedlog.go +++ b/htlcswitch/decayedlog.go @@ -158,9 +158,6 @@ func (d *DecayedLog) Stop() error { d.wg.Wait() - // Close boltdb. - d.db.Close() - return nil } diff --git a/htlcswitch/decayedlog_test.go b/htlcswitch/decayedlog_test.go index 42e636536..e7b7e6e58 100644 --- a/htlcswitch/decayedlog_test.go +++ b/htlcswitch/decayedlog_test.go @@ -31,14 +31,14 @@ func tempDecayedLogPath(t *testing.T) string { // startup sets up the DecayedLog and possibly the garbage collector. func startup(dbPath string, notifier bool) (sphinx.ReplayLog, - *mock.ChainNotifier, *sphinx.HashPrefix, error) { + *mock.ChainNotifier, *sphinx.HashPrefix, func(), error) { cfg := &kvdb.BoltConfig{ DBTimeout: time.Second, } backend, err := NewBoltBackendCreator(dbPath, "sphinxreplay.db")(cfg) if err != nil { - return nil, nil, nil, fmt.Errorf("unable to create temporary "+ + return nil, nil, nil, nil, fmt.Errorf("unable to create temporary "+ "decayed log db: %v", err) } @@ -63,7 +63,7 @@ func startup(dbPath string, notifier bool) (sphinx.ReplayLog, // Open the channeldb (start the garbage collector) err = log.Start() if err != nil { - return nil, nil, nil, err + return nil, nil, nil, nil, err } // Create a HashPrefix identifier for a packet. Instead of actually @@ -72,10 +72,15 @@ func startup(dbPath string, notifier bool) (sphinx.ReplayLog, var hashedSecret sphinx.HashPrefix _, err = rand.Read(hashedSecret[:]) if err != nil { - return nil, nil, nil, err + return nil, nil, nil, nil, err } - return log, chainNotifier, &hashedSecret, nil + stop := func() { + _ = log.Stop() + backend.Close() + } + + return log, chainNotifier, &hashedSecret, stop, nil } // shutdown deletes the temporary directory that the test database uses @@ -93,7 +98,7 @@ func TestDecayedLogGarbageCollector(t *testing.T) { dbPath := tempDecayedLogPath(t) - d, notifier, hashedSecret, err := startup(dbPath, true) + d, notifier, hashedSecret, _, err := startup(dbPath, true) if err != nil { t.Fatalf("Unable to start up DecayedLog: %v", err) } @@ -154,7 +159,7 @@ func TestDecayedLogPersistentGarbageCollector(t *testing.T) { dbPath := tempDecayedLogPath(t) - d, _, hashedSecret, err := startup(dbPath, true) + d, _, hashedSecret, stop, err := startup(dbPath, true) if err != nil { t.Fatalf("Unable to start up DecayedLog: %v", err) } @@ -172,9 +177,9 @@ func TestDecayedLogPersistentGarbageCollector(t *testing.T) { } // Shut down DecayedLog and the garbage collector along with it. - d.Stop() + stop() - d2, notifier2, _, err := startup(dbPath, true) + d2, notifier2, _, _, err := startup(dbPath, true) if err != nil { t.Fatalf("Unable to restart DecayedLog: %v", err) } @@ -210,7 +215,7 @@ func TestDecayedLogInsertionAndDeletion(t *testing.T) { dbPath := tempDecayedLogPath(t) - d, _, hashedSecret, err := startup(dbPath, false) + d, _, hashedSecret, _, err := startup(dbPath, false) if err != nil { t.Fatalf("Unable to start up DecayedLog: %v", err) } @@ -248,7 +253,7 @@ func TestDecayedLogStartAndStop(t *testing.T) { dbPath := tempDecayedLogPath(t) - d, _, hashedSecret, err := startup(dbPath, false) + d, _, hashedSecret, stop, err := startup(dbPath, false) if err != nil { t.Fatalf("Unable to start up DecayedLog: %v", err) } @@ -261,9 +266,9 @@ func TestDecayedLogStartAndStop(t *testing.T) { } // Shutdown the DecayedLog's channeldb - d.Stop() + stop() - d2, _, hashedSecret2, err := startup(dbPath, false) + d2, _, hashedSecret2, stop, err := startup(dbPath, false) if err != nil { t.Fatalf("Unable to restart DecayedLog: %v", err) } @@ -288,9 +293,9 @@ func TestDecayedLogStartAndStop(t *testing.T) { } // Shutdown the DecayedLog's channeldb - d2.Stop() + stop() - d3, _, hashedSecret3, err := startup(dbPath, false) + d3, _, hashedSecret3, _, err := startup(dbPath, false) if err != nil { t.Fatalf("Unable to restart DecayedLog: %v", err) } @@ -314,7 +319,7 @@ func TestDecayedLogStorageAndRetrieval(t *testing.T) { dbPath := tempDecayedLogPath(t) - d, _, hashedSecret, err := startup(dbPath, false) + d, _, hashedSecret, _, err := startup(dbPath, false) if err != nil { t.Fatalf("Unable to start up DecayedLog: %v", err) } diff --git a/kvdb/postgres/db.go b/kvdb/postgres/db.go index d123f734a..7badd1433 100644 --- a/kvdb/postgres/db.go +++ b/kvdb/postgres/db.go @@ -256,5 +256,7 @@ func (db *db) Copy(w io.Writer) error { // Close cleanly shuts down the database and syncs all data. // This function is part of the walletdb.Db interface implementation. func (db *db) Close() error { + log.Infof("Closing database %v", db.prefix) + return dbConns.Close(db.cfg.Dsn) }