Skip to content

Commit

Permalink
bugfix: make the daemon exit happy
Browse files Browse the repository at this point in the history
When the daemon server fails, the process will be hang because the
no-one closes the channel. We should exit.

Signed-off-by: Wei Fu <[email protected]>
  • Loading branch information
Wei Fu committed May 14, 2018
1 parent 5a7236d commit 6cd65b2
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,37 +181,40 @@ func runDaemon() error {

// initialize signal and handle method.
var (
waitExit = make(chan struct{})
signals = make(chan os.Signal, 1)
errCh = make(chan error, 1)
signalCh = make(chan os.Signal, 1)
)
signal.Notify(signals, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
sig := <-signals
logrus.Warnf("received signal: %s", sig)

for _, handle := range sigHandles {
if err := handle(); err != nil {
logrus.Errorf("failed to handle signal: %v", err)
}
}

close(waitExit)
os.Exit(1)
}()

// new daemon instance, this is core.
d := daemon.NewDaemon(cfg)
if d == nil {
return fmt.Errorf("failed to new daemon")
}

signal.Notify(signalCh, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGHUP)
sigHandles = append(sigHandles, d.ShutdownPlugin, d.Shutdown)

err := d.Run()
go func() {
// FIXME: I think the Run() should always return error.
errCh <- d.Run()
}()

<-waitExit
select {
case sig := <-signalCh:
logrus.Warnf("received signal: %s", sig)

return err
for _, handle := range sigHandles {
if err := handle(); err != nil {
logrus.Errorf("failed to handle signal: %v", err)
}
}

os.Exit(1)
case err := <-errCh:
// FIXME: should we do the cleanup like signal handle?
return err
}
return nil
}

// initLog initializes log Level and log format of daemon.
Expand Down

0 comments on commit 6cd65b2

Please sign in to comment.