Skip to content

Commit

Permalink
home: fix more
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Aug 25, 2023
1 parent 3872806 commit c07d80e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
3 changes: 0 additions & 3 deletions internal/home/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,6 @@ func closeDNSServer() {
if err != nil {
log.Debug("closing stats: %s", err)
}

// TODO(e.burkov): Find out if it's safe.
Context.stats = nil
}

if Context.queryLog != nil {
Expand Down
24 changes: 20 additions & 4 deletions internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"path/filepath"
"runtime"
"sync"
"sync/atomic"
"syscall"
"time"

Expand Down Expand Up @@ -101,6 +102,19 @@ func Main(clientBuildFS fs.FS) {

signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)

// TODO(s.chzhen): Think of better way to do graceful shutdown.
var cleaningMu sync.Mutex
cleanExit := func() {
cleaningMu.Lock()
defer cleaningMu.Unlock()

cleanup(context.Background())
cleanupAlways()

os.Exit(0)
}

go func() {
for {
sig := <-signals
Expand All @@ -110,15 +124,17 @@ func Main(clientBuildFS fs.FS) {
Context.clients.reloadARP()
Context.tls.reload()
default:
cleanup(context.Background())
cleanupAlways()
os.Exit(0)
cleanExit()
}
}
}()

if opts.serviceControlAction != "" {
handleServiceControlAction(opts, clientBuildFS, signals)
isStopping := new(atomic.Bool)
handleServiceControlAction(opts, clientBuildFS, isStopping)
if isStopping.Load() {
cleanExit()
}

return
}
Expand Down
14 changes: 5 additions & 9 deletions internal/home/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime"
"strconv"
"strings"
"sync/atomic"
"syscall"
"time"

Expand All @@ -33,7 +34,7 @@ const (
// daemon.
type program struct {
clientBuildFS fs.FS
signals chan os.Signal
isStopping *atomic.Bool
opts options
}

Expand All @@ -53,12 +54,7 @@ func (p *program) Start(_ service.Service) (err error) {

// Stop implements service.Interface interface for *program.
func (p *program) Stop(_ service.Service) (err error) {
select {
case p.signals <- syscall.SIGINT:
// Go on.
default:
// Stop should not block.
}
p.isStopping.Store(true)

return nil
}
Expand Down Expand Up @@ -198,7 +194,7 @@ func restartService() (err error) {
// - run: This is a special command that is not supposed to be used directly
// it is specified when we register a service, and it indicates to the app
// that it is being run as a service/daemon.
func handleServiceControlAction(opts options, clientBuildFS fs.FS, signals chan os.Signal) {
func handleServiceControlAction(opts options, clientBuildFS fs.FS, isStopping *atomic.Bool) {
// Call chooseSystem explicitly to introduce OpenBSD support for service
// package. It's a noop for other GOOS values.
chooseSystem()
Expand Down Expand Up @@ -232,7 +228,7 @@ func handleServiceControlAction(opts options, clientBuildFS fs.FS, signals chan

s, err := service.New(&program{
clientBuildFS: clientBuildFS,
signals: signals,
isStopping: isStopping,
opts: runOpts,
}, svcConfig)
if err != nil {
Expand Down

0 comments on commit c07d80e

Please sign in to comment.