prevented sending SIGTERM multiple times upon signal handler termination

This commit is contained in:
2025-12-05 13:57:30 +09:00
parent 42800a9c7f
commit ce47cdb69e

View File

@@ -13,6 +13,7 @@ import "path/filepath"
import "regexp" import "regexp"
import "strings" import "strings"
import "sync" import "sync"
import "sync/atomic"
import "syscall" import "syscall"
// Don't change these items to 'const' as they can be overridden externally with a linker option // Don't change these items to 'const' as they can be overridden externally with a linker option
@@ -29,6 +30,7 @@ var hodu_rsa_key_text []byte
// -------------------------------------------------------------------- // --------------------------------------------------------------------
type signal_handler struct { type signal_handler struct {
svc hodu.Service svc hodu.Service
stop_req atomic.Bool
} }
func (sh *signal_handler) RunTask(wg *sync.WaitGroup) { func (sh *signal_handler) RunTask(wg *sync.WaitGroup) {
@@ -53,8 +55,9 @@ chan_loop:
sh.svc.FixServices() sh.svc.FixServices()
case sig = <-sigterm_chan: case sig = <-sigterm_chan:
sh.svc.StopServices()
sh.svc.WriteLog ("", hodu.LOG_INFO, "Received %s signal", sig) sh.svc.WriteLog ("", hodu.LOG_INFO, "Received %s signal", sig)
sh.stop_req.Store(true)
sh.svc.StopServices()
break chan_loop break chan_loop
} }
} }
@@ -76,7 +79,9 @@ func (sh *signal_handler) StartService(data interface{}) {
} }
func (sh *signal_handler) StopServices() { func (sh *signal_handler) StopServices() {
if sh.stop_req.CompareAndSwap(false, true) {
syscall.Kill(syscall.Getpid(), syscall.SIGTERM) // TODO: find a better to terminate the signal handler... syscall.Kill(syscall.Getpid(), syscall.SIGTERM) // TODO: find a better to terminate the signal handler...
}
} }
func (sh *signal_handler) FixServices() { func (sh *signal_handler) FixServices() {