From d74610c345101f6416125f8c8ae3cbb54b67ae53 Mon Sep 17 00:00:00 2001 From: Mechiel Lukkien Date: Mon, 8 Apr 2024 20:22:07 +0200 Subject: [PATCH] bugfix: missing account close in queue direct send found while writing new tests for upcoming functionality. the test had an embarrassing workaround for the symptoms... --- queue/direct.go | 4 ++++ queue/queue_test.go | 13 ++----------- store/account.go | 9 +++++++++ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/queue/direct.go b/queue/direct.go index affd50b..fa1df5d 100644 --- a/queue/direct.go +++ b/queue/direct.go @@ -728,6 +728,10 @@ func updateRecipientDomainTLS(ctx context.Context, log mlog.Log, senderAccount s if err != nil { return fmt.Errorf("open account: %w", err) } + defer func() { + err := acc.Close() + log.Check(err, "closing account") + }() err = acc.DB.Write(ctx, func(tx *bstore.Tx) error { // First delete any existing record. if err := tx.Delete(&store.RecipientDomainTLS{Domain: rdt.Domain}); err != nil && err != bstore.ErrAbsent { diff --git a/queue/queue_test.go b/queue/queue_test.go index 40cb623..5bd6899 100644 --- a/queue/queue_test.go +++ b/queue/queue_test.go @@ -49,20 +49,10 @@ func tcompare(t *testing.T, got, exp any) { } } -var keepAccount bool - func setup(t *testing.T) (*store.Account, func()) { // Prepare config so email can be delivered to mjl@mox.example. - // Don't trigger the account consistency checks. Only remove account files on first - // (of randomized) runs. - if !keepAccount { - os.RemoveAll("../testdata/queue/data") - keepAccount = true - } else { - os.RemoveAll("../testdata/queue/data/queue") - } - + os.RemoveAll("../testdata/queue/data") log := mlog.New("queue", nil) mox.Context = ctxbg mox.ConfigStaticPath = filepath.FromSlash("../testdata/queue/mox.conf") @@ -75,6 +65,7 @@ func setup(t *testing.T) (*store.Account, func()) { mox.Shutdown, mox.ShutdownCancel = context.WithCancel(ctxbg) return acc, func() { acc.Close() + acc.CheckClosed() mox.ShutdownCancel() mox.Shutdown, mox.ShutdownCancel = context.WithCancel(ctxbg) Shutdown() diff --git a/store/account.go b/store/account.go index 78cb4b3..f52c5a3 100644 --- a/store/account.go +++ b/store/account.go @@ -1059,6 +1059,15 @@ func initAccount(db *bstore.DB) error { }) } +// CheckClosed asserts that the account has a zero reference count. For use in tests. +func (a *Account) CheckClosed() { + openAccounts.Lock() + defer openAccounts.Unlock() + if a.nused != 0 { + panic(fmt.Sprintf("account still in use, %d refs", a.nused)) + } +} + // Close reduces the reference count, and closes the database connection when // it was the last user. func (a *Account) Close() error {