fix shutdown loop -- have to ignore SIGHUP

This commit is contained in:
sawka
2023-03-12 15:16:49 -07:00
parent 34d4962b7a
commit 861cf8d126
2 changed files with 29 additions and 1 deletions
+16 -1
View File
@@ -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()
+13
View File
@@ -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"`