From fe7d9f764343c5da691f09654709cd2c1a307f46 Mon Sep 17 00:00:00 2001 From: Gabe Jackson Date: Thu, 3 Dec 2020 10:09:31 -0500 Subject: [PATCH] Additional Config Store Cleanup (#16440) This change addresses a few issues where config stores were not properly closed when an error was encountered on server startup. This could result in leaked database connections when dealing with a database config store. --- cmd/mattermost/commands/channel.go | 1 - cmd/mattermost/commands/init.go | 1 - cmd/mattermost/commands/permissions.go | 1 + cmd/mattermost/commands/server.go | 1 + cmd/mattermost/commands/version.go | 1 + config/database.go | 9 ++++++++- config/store.go | 8 +++++++- 7 files changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd/mattermost/commands/channel.go b/cmd/mattermost/commands/channel.go index 3348880292..5ea6a205c4 100644 --- a/cmd/mattermost/commands/channel.go +++ b/cmd/mattermost/commands/channel.go @@ -611,7 +611,6 @@ func renameChannelCmdF(command *cobra.Command, args []string) error { } func searchChannelCmdF(command *cobra.Command, args []string) error { - a, err := InitDBCommandContextCobra(command) if err != nil { return errors.Wrap(err, "failed to InitDBCommandContextCobra") diff --git a/cmd/mattermost/commands/init.go b/cmd/mattermost/commands/init.go index 93d2661ece..ba468aeef0 100644 --- a/cmd/mattermost/commands/init.go +++ b/cmd/mattermost/commands/init.go @@ -13,7 +13,6 @@ import ( func InitDBCommandContextCobra(command *cobra.Command) (*app.App, error) { a, err := InitDBCommandContext(getConfigDSN(command, config.GetEnvironment())) - if err != nil { // Returning an error just prints the usage message, so actually panic panic(err) diff --git a/cmd/mattermost/commands/permissions.go b/cmd/mattermost/commands/permissions.go index 6a3e7e687a..7e78e944bb 100644 --- a/cmd/mattermost/commands/permissions.go +++ b/cmd/mattermost/commands/permissions.go @@ -62,6 +62,7 @@ func resetPermissionsCmdF(command *cobra.Command, args []string) error { if err != nil { return err } + defer a.Srv().Shutdown() confirmFlag, _ := command.Flags().GetBool("confirm") if !confirmFlag { diff --git a/cmd/mattermost/commands/server.go b/cmd/mattermost/commands/server.go index d14da0c02a..ac988ef5fd 100644 --- a/cmd/mattermost/commands/server.go +++ b/cmd/mattermost/commands/server.go @@ -78,6 +78,7 @@ func serverCmdF(command *cobra.Command, args []string) error { if err != nil { return errors.Wrap(err, "failed to load configuration") } + defer configStore.Close() return runServer(configStore, usedPlatform, interruptChan) } diff --git a/cmd/mattermost/commands/version.go b/cmd/mattermost/commands/version.go index e32ee362b7..9649a4842f 100644 --- a/cmd/mattermost/commands/version.go +++ b/cmd/mattermost/commands/version.go @@ -32,6 +32,7 @@ func versionCmdF(command *cobra.Command, args []string) error { if err != nil { return err } + defer a.Srv().Shutdown() printVersion(a) diff --git a/config/database.go b/config/database.go index ba288b1386..98218c4700 100644 --- a/config/database.go +++ b/config/database.go @@ -48,6 +48,12 @@ func NewDatabaseStore(dsn string) (ds *DatabaseStore, err error) { return nil, errors.Wrapf(err, "failed to connect to %s database", driverName) } + defer func() { + if err != nil { + db.Close() + } + }() + ds = &DatabaseStore{ driverName: driverName, originalDsn: dsn, @@ -55,7 +61,8 @@ func NewDatabaseStore(dsn string) (ds *DatabaseStore, err error) { db: db, } if err = initializeConfigurationsTable(ds.db); err != nil { - return nil, errors.Wrap(err, "failed to initialize") + err = errors.Wrap(err, "failed to initialize") + return nil, err } return ds, nil diff --git a/config/store.go b/config/store.go index a3638d8c6b..709d7057ea 100644 --- a/config/store.go +++ b/config/store.go @@ -54,7 +54,13 @@ func NewStore(dsn string, watch bool, customDefaults *model.Config) (*Store, err return nil, err } - return NewStoreFromBacking(backingStore, customDefaults) + store, err := NewStoreFromBacking(backingStore, customDefaults) + if err != nil { + backingStore.Close() + return nil, errors.Wrap(err, "failed to create store") + } + + return store, nil } func NewStoreFromBacking(backingStore BackingStore, customDefaults *model.Config) (*Store, error) {