From 168b62937da905afdf14e62b2e9ab98e8a8ed75b Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 8 Jun 2022 09:21:16 +0200 Subject: [PATCH] Rework signal handling --- commands/local_proxy_start.go | 2 +- commands/local_server_start.go | 2 +- local/process/process.go | 4 +--- local/process/process_linux.go | 16 ++++++++++------ local/process/process_other.go | 13 ++++++++----- local/process/process_windows.go | 8 ++++++-- local/runner.go | 2 +- 7 files changed, 28 insertions(+), 19 deletions(-) diff --git a/commands/local_proxy_start.go b/commands/local_proxy_start.go index 50182da9..b6ccfffa 100644 --- a/commands/local_proxy_start.go +++ b/commands/local_proxy_start.go @@ -167,7 +167,7 @@ var localProxyStartCmd = &console.Command{ shutdownCh := make(chan bool, 1) go func() { sigsCh := make(chan os.Signal, 1) - signal.Notify(sigsCh, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM) + signal.Notify(sigsCh, os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM) <-sigsCh signal.Stop(sigsCh) shutdownCh <- true diff --git a/commands/local_server_start.go b/commands/local_server_start.go index 222e7a1f..73c65257 100644 --- a/commands/local_server_start.go +++ b/commands/local_server_start.go @@ -89,7 +89,7 @@ var localServerStartCmd = &console.Command{ shutdownCh := make(chan bool, 1) go func() { sigsCh := make(chan os.Signal, 1) - signal.Notify(sigsCh, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM) + signal.Notify(sigsCh, os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM) <-sigsCh signal.Stop(sigsCh) shutdownCh <- true diff --git a/local/process/process.go b/local/process/process.go index 6f1faac3..ffc2d112 100644 --- a/local/process/process.go +++ b/local/process/process.go @@ -24,7 +24,6 @@ import ( "context" "os" "os/exec" - "syscall" "github.com/pkg/errors" "github.com/rs/zerolog" @@ -72,8 +71,7 @@ func (p *Process) Run(ctx context.Context) (*exec.Cmd, error) { cmd.Env = os.Environ() cmd.Env = append(cmd.Env, p.Env...) - cmd.SysProcAttr = &syscall.SysProcAttr{} - deathsig(cmd.SysProcAttr) + cmd.SysProcAttr = createSysProcAttr() if err := cmd.Start(); err != nil { return nil, errors.WithStack(err) } diff --git a/local/process/process_linux.go b/local/process/process_linux.go index 4437de40..3d9c3564 100644 --- a/local/process/process_linux.go +++ b/local/process/process_linux.go @@ -22,15 +22,19 @@ package process import ( "os/exec" "syscall" + + "golang.org/x/sys/unix" ) -func deathsig(sysProcAttr *syscall.SysProcAttr) { - // the following helps with killing the main process and its children - // see https://medium.com/@felixge/killing-a-child-process-and-all-of-its-children-in-go-54079af94773 - sysProcAttr.Setpgid = true - sysProcAttr.Pdeathsig = syscall.SIGKILL +func createSysProcAttr() *syscall.SysProcAttr { + return &unix.SysProcAttr{ + // the following helps with killing the main process and its children + // see https://medium.com/@felixge/killing-a-child-process-and-all-of-its-children-in-go-54079af94773 + Setpgid: true, + Pdeathsig: unix.SIGKILL, + } } func kill(cmd *exec.Cmd) error { - return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) + return unix.Kill(-cmd.Process.Pid, unix.SIGKILL) } diff --git a/local/process/process_other.go b/local/process/process_other.go index 953ef82e..dc195f18 100644 --- a/local/process/process_other.go +++ b/local/process/process_other.go @@ -23,16 +23,19 @@ package process import ( + "os" "os/exec" "syscall" ) -func deathsig(sysProcAttr *syscall.SysProcAttr) { - // the following helps with killing the main process and its children - // see https://medium.com/@felixge/killing-a-child-process-and-all-of-its-children-in-go-54079af94773 - sysProcAttr.Setpgid = true +func createSysProcAttr() *syscall.SysProcAttr { + return &syscall.SysProcAttr{ + // the following helps with killing the main process and its children + // see https://medium.com/@felixge/killing-a-child-process-and-all-of-its-children-in-go-54079af94773 + Setpgid: true, + } } func kill(cmd *exec.Cmd) error { - return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) + return syscall.Kill(-cmd.Process.Pid, os.SIGKILL) } diff --git a/local/process/process_windows.go b/local/process/process_windows.go index e062c1ca..9312bf9f 100644 --- a/local/process/process_windows.go +++ b/local/process/process_windows.go @@ -20,12 +20,16 @@ package process import ( + "os" "os/exec" "strconv" "syscall" + + "golang.org/x/sys/windows" ) -func deathsig(sysProcAttr *syscall.SysProcAttr) { +func createSysProcAttr() *syscall.SysProcAttr { + return &windows.SysProcAttr{} } func kill(cmd *exec.Cmd) error { @@ -33,5 +37,5 @@ func kill(cmd *exec.Cmd) error { if err := c.Run(); err == nil { return nil } - return cmd.Process.Signal(syscall.SIGKILL) + return cmd.Process.Signal(windows.SIGKILL) } diff --git a/local/runner.go b/local/runner.go index b128c91d..fd5b9c6a 100644 --- a/local/runner.go +++ b/local/runner.go @@ -107,7 +107,7 @@ func (r *Runner) Run() error { cmdExitChan := make(chan error) // receives command exit status, allow to cmd.Wait() in non-blocking way restartChan := make(chan bool) // receives requests to restart the command sigChan := make(chan os.Signal, 1) - signal.Notify(sigChan, os.Kill, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + signal.Notify(sigChan, os.Kill, os.Interrupt, syscall.SIGTERM) defer signal.Stop(sigChan) if len(r.pidFile.Watched) > 0 { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy