From 7f588e6dbc68da0be29503b59122914382268319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 7 Jul 2023 15:48:08 +0200 Subject: [PATCH] Manager: close database connection on startup errors When there is an error detected at startup, close the database connection. Before, the connection could be kept open even when an error was returned, causing the write-ahead log files to be kept around. These are now properly integrated into the main database file before exiting. --- internal/manager/persistence/db.go | 34 ++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/internal/manager/persistence/db.go b/internal/manager/persistence/db.go index 9c407678..b5b56abc 100644 --- a/internal/manager/persistence/db.go +++ b/internal/manager/persistence/db.go @@ -40,6 +40,18 @@ func OpenDB(ctx context.Context, dsn string) (*DB, error) { return nil, err } + // Close the database connection if there was some error. This prevents + // leaking database connections & should remove any write-ahead-log files. + closeConnOnReturn := true + defer func() { + if !closeConnOnReturn { + return + } + if err := db.Close(); err != nil { + log.Debug().AnErr("cause", err).Msg("cannot close database connection") + } + }() + if err := setBusyTimeout(db.gormDB, 5*time.Second); err != nil { return nil, err } @@ -52,6 +64,7 @@ func OpenDB(ctx context.Context, dsn string) (*DB, error) { } log.Debug().Msg("database automigration succesful") + closeConnOnReturn = false return db, nil } @@ -74,6 +87,22 @@ func openDBWithConfig(dsn string, config *gorm.Config) (*DB, error) { return nil, err } + db := DB{ + gormDB: gormDB, + } + + // Close the database connection if there was some error. This prevents + // leaking database connections & should remove any write-ahead-log files. + closeConnOnReturn := true + defer func() { + if !closeConnOnReturn { + return + } + if err := db.Close(); err != nil { + log.Debug().AnErr("cause", err).Msg("cannot close database connection") + } + }() + // Use the generic sql.DB interface to set some connection pool options. sqlDB, err := gormDB.DB() if err != nil { @@ -108,10 +137,7 @@ func openDBWithConfig(dsn string, config *gorm.Config) (*DB, error) { return nil, fmt.Errorf("enabling SQLite 'normal' sync mode: %w", tx.Error) } - db := DB{ - gormDB: gormDB, - } - + closeConnOnReturn = false return &db, nil }