testutil/daemon: always remove pidfile after daemon is stopped

If the daemon was stopped successfully in one of the retry-loops,
the function would return early;

```go
for {
	select {
	case err := <-d.Wait:
---> the function returns here, both on "success" and on "fail"
		return err
	case <-time.After(20 * time.Second):
...
```

In that case, the pidfile would not be cleaned up. This patch changes
the function to clean-up the pidfile in a defer, so that it will
always be removed after succesfully stopping the daemon.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2019-10-09 14:45:37 +02:00
parent f6842327b0
commit c56bfdf10a
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 7 additions and 7 deletions

View File

@ -465,8 +465,13 @@ func (d *Daemon) StopWithError() (err error) {
d.log.Logf("[%s] error while stopping daemon: %v", d.id, err)
} else {
d.log.Logf("[%s] daemon stopped", d.id)
if d.pidFile != "" {
_ = os.Remove(d.pidFile)
}
}
if err := d.logFile.Close(); err != nil {
d.log.Logf("[%s] failed to close daemon logfile: %v", d.id, err)
}
d.logFile.Close()
d.cmd = nil
}()
@ -519,12 +524,7 @@ out2:
return err
}
d.cmd.Wait()
if d.pidFile != "" {
_ = os.Remove(d.pidFile)
}
return nil
return d.cmd.Wait()
}
// Restart will restart the daemon by first stopping it and the starting it.