Browse code

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>

Sebastiaan van Stijn authored on 2019/10/09 21:45:37
Showing 1 changed files
... ...
@@ -465,8 +465,13 @@ func (d *Daemon) StopWithError() (err error) {
465 465
 			d.log.Logf("[%s] error while stopping daemon: %v", d.id, err)
466 466
 		} else {
467 467
 			d.log.Logf("[%s] daemon stopped", d.id)
468
+			if d.pidFile != "" {
469
+				_ = os.Remove(d.pidFile)
470
+			}
471
+		}
472
+		if err := d.logFile.Close(); err != nil {
473
+			d.log.Logf("[%s] failed to close daemon logfile: %v", d.id, err)
468 474
 		}
469
-		d.logFile.Close()
470 475
 		d.cmd = nil
471 476
 	}()
472 477
 
... ...
@@ -519,12 +524,7 @@ out2:
519 519
 		return err
520 520
 	}
521 521
 
522
-	d.cmd.Wait()
523
-
524
-	if d.pidFile != "" {
525
-		_ = os.Remove(d.pidFile)
526
-	}
527
-	return nil
522
+	return d.cmd.Wait()
528 523
 }
529 524
 
530 525
 // Restart will restart the daemon by first stopping it and the starting it.