Skip to content

Commit

Permalink
fix systemd restart waiting due to signal handling
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Sep 19, 2024
1 parent 3ed6502 commit 5e54ec6
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions std/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,23 @@ import (
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"

kcp "github.com/xtaci/kcp-go/v5"
)

const (
EXIT_WAIT = 5 // max seconds to wait before exit
)

func init() {
go sigHandler()
}

func sigHandler() {
var exitOnce sync.Once
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGUSR1, syscall.SIGTERM, syscall.SIGINT)
signal.Ignore(syscall.SIGPIPE)
Expand All @@ -51,6 +58,14 @@ func sigHandler() {
postProcess()
signal.Stop(ch)
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)

// wait for max EXIT_WAIT seconds before exit
exitOnce.Do(func() {
go func() {
<-time.After(EXIT_WAIT * time.Second)
os.Exit(0)
}()
})
}
}
}

0 comments on commit 5e54ec6

Please sign in to comment.