From 65e6070e5f32e9f102bbb6fd132afe31848ca701 Mon Sep 17 00:00:00 2001 From: Masahiro Fujimura Date: Tue, 2 Jul 2019 10:11:36 +0900 Subject: [PATCH] Fix race condition in server mode (#857) --- commands/server.go | 21 +-------------------- server/server.go | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/commands/server.go b/commands/server.go index 24921cde..e21907d3 100644 --- a/commands/server.go +++ b/commands/server.go @@ -208,26 +208,7 @@ func (p *ServerCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{} } } - dbclient, locked, err := report.NewDBClient(report.DBClientConf{ - CveDictCnf: c.Conf.CveDict, - OvalDictCnf: c.Conf.OvalDict, - GostCnf: c.Conf.Gost, - ExploitCnf: c.Conf.Exploit, - DebugSQL: c.Conf.DebugSQL, - }) - if locked { - util.Log.Errorf("SQLite3 is locked. Close other DB connections and try again: %+v", err) - return subcommands.ExitFailure - } - - if err != nil { - util.Log.Errorf("Failed to init DB Clients. err: %+v", err) - return subcommands.ExitFailure - } - - defer dbclient.CloseDB() - - http.Handle("/vuls", server.VulsHandler{DBclient: *dbclient}) + http.Handle("/vuls", server.VulsHandler{}) http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "ok") }) diff --git a/server/server.go b/server/server.go index 22708550..37175ebf 100644 --- a/server/server.go +++ b/server/server.go @@ -35,7 +35,6 @@ import ( // VulsHandler is used for vuls server mode type VulsHandler struct { - DBclient report.DBClient } func (h VulsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -70,7 +69,26 @@ func (h VulsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - if err := report.FillCveInfo(h.DBclient, &result, []string{}); err != nil { + dbclient, locked, err := report.NewDBClient(report.DBClientConf{ + CveDictCnf: c.Conf.CveDict, + OvalDictCnf: c.Conf.OvalDict, + GostCnf: c.Conf.Gost, + ExploitCnf: c.Conf.Exploit, + DebugSQL: c.Conf.DebugSQL, + }) + if locked { + util.Log.Errorf("SQLite3 is locked. Close other DB connections and try again: %+v", err) + return + } + + if err != nil { + util.Log.Errorf("Failed to init DB Clients. err: %+v", err) + return + } + + defer dbclient.CloseDB() + + if err := report.FillCveInfo(*dbclient, &result, []string{}); err != nil { util.Log.Error(err) http.Error(w, err.Error(), http.StatusServiceUnavailable) return