Skip to content

Commit

Permalink
Fix race condition on daemon.ex
Browse files Browse the repository at this point in the history
If daemon.Restart() got called twice in quick succession, the second
call panicked with d.ex being nil.
  • Loading branch information
neelance committed Dec 2, 2018
1 parent 6ff68a1 commit a30bd97
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,14 @@ type daemon struct {
conf conf.Daemon
indir string

running bool
ex *shell.Executor
log termlog.Stream
shell string
stop bool
ex *shell.Executor
log termlog.Stream
shell string
stop bool
sync.Mutex
}

func (d *daemon) Run() {
ex, err := shell.NewExecutor(d.shell, d.conf.Command, d.indir)
if err != nil {
d.log.Shout("Could not create executor: %s", err)
}
d.ex = ex

var lastStart time.Time
delay := MinRestart
for d.stop != true {
Expand All @@ -52,7 +45,7 @@ func (d *daemon) Run() {
}
d.log.Notice(">> starting...")
lastStart = time.Now()
err, pstate := ex.Run(d.log, false)
err, pstate := d.ex.Run(d.log, false)

if err != nil {
d.log.Shout("execution error: %s", err)
Expand Down Expand Up @@ -83,8 +76,12 @@ func (d *daemon) Run() {
func (d *daemon) Restart() {
d.Lock()
defer d.Unlock()
if !d.running {
d.running = true
if d.ex == nil {
ex, err := shell.NewExecutor(d.shell, d.conf.Command, d.indir)
if err != nil {
d.log.Shout("Could not create executor: %s", err)
}
d.ex = ex
go d.Run()
} else {
d.log.Notice(">> sending signal %s", d.conf.RestartSignal)
Expand Down

0 comments on commit a30bd97

Please sign in to comment.