Merge: - service stop: fix race

Close #799

* commit '131aa4c93ccd6e2603e8025dfd4d3693aa9dd561':
  - service stop: fix race
This commit is contained in:
Simon Zolin 2019-07-02 14:45:21 +03:00
commit ceac4cbdd5
3 changed files with 9 additions and 6 deletions

View File

@ -51,6 +51,7 @@ type configuration struct {
// runningAsService flag is set to true when options are passed from the service runner // runningAsService flag is set to true when options are passed from the service runner
runningAsService bool runningAsService bool
disableUpdate bool // If set, don't check for updates disableUpdate bool // If set, don't check for updates
appSignalChannel chan os.Signal
BindHost string `yaml:"bind_host"` // BindHost is the IP address of the HTTP server to bind to BindHost string `yaml:"bind_host"` // BindHost is the IP address of the HTTP server to bind to
BindPort int `yaml:"bind_port"` // BindPort is the port the HTTP server BindPort int `yaml:"bind_port"` // BindPort is the port the HTTP server

View File

@ -99,10 +99,10 @@ func run(args options) {
requireAdminRights() requireAdminRights()
} }
signalChannel := make(chan os.Signal) config.appSignalChannel = make(chan os.Signal)
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT) signal.Notify(config.appSignalChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
go func() { go func() {
<-signalChannel <-config.appSignalChannel
cleanup() cleanup()
cleanupAlways() cleanupAlways()
os.Exit(0) os.Exit(0)

View File

@ -3,6 +3,7 @@ package home
import ( import (
"os" "os"
"runtime" "runtime"
"syscall"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"github.com/kardianos/service" "github.com/kardianos/service"
@ -31,9 +32,10 @@ func (p *program) Start(s service.Service) error {
// Stop stops the program // Stop stops the program
func (p *program) Stop(s service.Service) error { func (p *program) Stop(s service.Service) error {
// Stop should not block. Return with a few seconds. // Stop should not block. Return with a few seconds.
cleanup() if config.appSignalChannel == nil {
cleanupAlways() os.Exit(0)
os.Exit(0) }
config.appSignalChannel <- syscall.SIGINT
return nil return nil
} }