From 4a6ab5e9fdbd6b6da38d8efc7379e38e17898135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Fri, 25 Nov 2022 18:16:55 +0800 Subject: [PATCH] Fix cancel on start --- cmd/sing-box/cmd_run.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cmd/sing-box/cmd_run.go b/cmd/sing-box/cmd_run.go index 77572ff6..40d03df6 100644 --- a/cmd/sing-box/cmd_run.go +++ b/cmd/sing-box/cmd_run.go @@ -69,6 +69,20 @@ func create() (*box.Box, context.CancelFunc, error) { cancel() return nil, nil, E.Cause(err, "create service") } + + osSignals := make(chan os.Signal, 1) + signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP) + defer func() { + signal.Stop(osSignals) + close(osSignals) + }() + + go func() { + _, loaded := <-osSignals + if loaded { + cancel() + } + }() err = instance.Start() if err != nil { cancel() @@ -80,6 +94,7 @@ func create() (*box.Box, context.CancelFunc, error) { func run() error { osSignals := make(chan os.Signal, 1) signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP) + defer signal.Stop(osSignals) for { instance, cancel, err := create() if err != nil {