lock.go: fix race condition

Fix a race condition between startChild() and killChild() that could
lead to an orphaned managed process.

Fixes #1155
This commit is contained in:
Michael S. Fischer 2015-08-05 09:06:51 -07:00
parent 9a9cc9341b
commit 6875e8d6b4

View File

@ -47,7 +47,7 @@ Usage: consul lock [options] prefix child...
disrupted the child process will be sent a SIGTERM signal and given disrupted the child process will be sent a SIGTERM signal and given
time to gracefully exit. After the grace period expires the process time to gracefully exit. After the grace period expires the process
will be hard terminated. will be hard terminated.
For Consul agents on Windows, the child process is always hard For Consul agents on Windows, the child process is always hard
terminated with a SIGKILL, since Windows has no POSIX compatible terminated with a SIGKILL, since Windows has no POSIX compatible
notion for SIGTERM. notion for SIGTERM.
@ -268,13 +268,14 @@ func (c *LockCommand) startChild(script string, doneCh chan struct{}) error {
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
// Start the child process // Start the child process
c.childLock.Lock()
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
c.Ui.Error(fmt.Sprintf("Error starting handler: %s", err)) c.Ui.Error(fmt.Sprintf("Error starting handler: %s", err))
c.childLock.Unlock()
return err return err
} }
// Setup the child info // Setup the child info
c.childLock.Lock()
c.child = cmd.Process c.child = cmd.Process
c.childLock.Unlock() c.childLock.Unlock()