From c56bfdf10a4874701fe53e82d209ec7e5d36d497 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 9 Oct 2019 14:45:37 +0200 Subject: [PATCH] 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 --- testutil/daemon/daemon.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/testutil/daemon/daemon.go b/testutil/daemon/daemon.go index 0c1ecc19b2..943e6b7cb9 100644 --- a/testutil/daemon/daemon.go +++ b/testutil/daemon/daemon.go @@ -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.