Skip to content

Commit

Permalink
Issue #400: do not exit on SIGHUP
Browse files Browse the repository at this point in the history
SIGHUP is usually used to trigger a reload of the configuration which
is not supported. However, the process should not exit either.

Fixes #400
  • Loading branch information
magiconair committed Dec 10, 2017
1 parent c3b75f6 commit d948773
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions exit/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,29 @@ var quit = make(chan bool)

// Listen registers an exit handler which is called on
// SIGINT/SIGTERM or when Exit/Fatal/Fatalf is called.
// SIGHUP is ignored since that is usually used for
// triggering a reload of configuration which isn't
// supported but shouldn't kill the process either.
func Listen(fn func(os.Signal)) {
wg.Add(1)
go func() {
defer wg.Done()
// we use buffered to mitigate losing the signal
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt, syscall.SIGTERM)
for {
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)

var sig os.Signal
select {
case sig = <-sigchan:
case <-quit:
}
if fn != nil {
fn(sig)
var sig os.Signal
select {
case sig = <-sigchan:
if sig == syscall.SIGHUP {
continue
}
case <-quit:
}
if fn != nil {
fn(sig)
}
return
}
}()
}
Expand Down

0 comments on commit d948773

Please sign in to comment.