diff --git a/cmd/main-server.go b/cmd/main-server.go index 1d3a1caf..d9dfca76 100644 --- a/cmd/main-server.go +++ b/cmd/main-server.go @@ -9,6 +9,7 @@ import ( "log" "net/http" "os" + "os/signal" "runtime/debug" "strconv" "strings" @@ -451,8 +452,10 @@ func stdinReadWatch() { if err != nil { log.Printf("[prompt] stdin closed/error, shutting down: %v\n", err) sendTelemetryWrapper() - time.Sleep(1 * time.Second) + log.Printf("[prompt] closing db connection\n") + sstore.CloseDB() log.Printf("[prompt] *** shutting down local server\n") + time.Sleep(1 * time.Second) syscall.Kill(syscall.Getpid(), syscall.SIGINT) break } @@ -461,6 +464,17 @@ func stdinReadWatch() { syscall.Kill(syscall.Getpid(), syscall.SIGKILL) } +// ignore SIGHUP +func installSignalHandlers() { + sigCh := make(chan os.Signal, 1) + signal.Notify(sigCh, syscall.SIGHUP) + go func() { + for sig := range sigCh { + fmt.Printf("[prompt] got signal %v (ignoring)\n", sig) + } + }() +} + func main() { scbase.BuildTime = BuildTime @@ -537,6 +551,7 @@ func main() { if err != nil { log.Printf("[error] updating activity: %v\n", err) } + installSignalHandlers() go telemetryLoop() go stdinReadWatch() go runWebSocketServer() diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go index 7fa6633f..9593b235 100644 --- a/pkg/sstore/sstore.go +++ b/pkg/sstore/sstore.go @@ -117,6 +117,19 @@ func GetDB(ctx context.Context) (*sqlx.DB, error) { return globalDB, globalDBErr } +func CloseDB() { + globalDBLock.Lock() + defer globalDBLock.Unlock() + if globalDB == nil { + return + } + err := globalDB.Close() + if err != nil { + log.Printf("[db] error closing database: %v\n", err) + } + globalDB = nil +} + type ClientWinSizeType struct { Width int `json:"width"` Height int `json:"height"`