Browse code

Prevent a goroutine leak when healthcheck gets stopped

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>

Kenfe-Mickael Laventure authored on 2017/06/22 09:36:19
Showing 1 changed files
... ...
@@ -186,7 +186,7 @@ func monitor(d *Daemon, c *container.Container, stop chan struct{}, probe probe)
186 186
 			logrus.Debugf("Running health check for container %s ...", c.ID)
187 187
 			startTime := time.Now()
188 188
 			ctx, cancelProbe := context.WithTimeout(context.Background(), probeTimeout)
189
-			results := make(chan *types.HealthcheckResult)
189
+			results := make(chan *types.HealthcheckResult, 1)
190 190
 			go func() {
191 191
 				healthChecksCounter.Inc()
192 192
 				result, err := probe.run(ctx, d, c)
... ...
@@ -209,8 +209,10 @@ func monitor(d *Daemon, c *container.Container, stop chan struct{}, probe probe)
209 209
 			select {
210 210
 			case <-stop:
211 211
 				logrus.Debugf("Stop healthcheck monitoring for container %s (received while probing)", c.ID)
212
-				// Stop timeout and kill probe, but don't wait for probe to exit.
213 212
 				cancelProbe()
213
+				// Wait for probe to exit (it might take a while to respond to the TERM
214
+				// signal and we don't want dying probes to pile up).
215
+				<-results
214 216
 				return
215 217
 			case result := <-results:
216 218
 				handleProbeResult(d, c, result, stop)