server: add missing db.Close calls before returning (#541)

It's better to always close the db before exit, so we will be noticed
when any error happens. Otherwise, serious errors can silently happen,
e.g, corrupted db, pending write missing ...

Fixes #538
This commit is contained in:
Cuong Manh Le 2021-09-09 15:05:26 +07:00 committed by GitHub
parent 2e45a0665e
commit 397e862a1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -164,6 +164,11 @@ func startStandAlone(ctx *server.Context, appCreator types.AppCreator) error {
if err != nil { if err != nil {
return err return err
} }
defer func() {
if err := db.Close(); err != nil {
ctx.Logger.With("error", err).Error("error closing db")
}
}()
traceWriterFile := ctx.Viper.GetString(srvflags.TraceStore) traceWriterFile := ctx.Viper.GetString(srvflags.TraceStore)
traceWriter, err := openTraceWriter(traceWriterFile) traceWriter, err := openTraceWriter(traceWriterFile)
@ -226,6 +231,11 @@ func startInProcess(ctx *server.Context, clientCtx client.Context, appCreator ty
logger.Error("failed to open DB", "error", err.Error()) logger.Error("failed to open DB", "error", err.Error())
return err return err
} }
defer func() {
if err := db.Close(); err != nil {
ctx.Logger.With("error", err).Error("error closing db")
}
}()
traceWriter, err := openTraceWriter(traceWriterFile) traceWriter, err := openTraceWriter(traceWriterFile)
if err != nil { if err != nil {