Prevent a goroutine leak when healthcheck gets stopped
Aaron Lehmann authored on 2017/06/27 07:34:43... | ... |
@@ -193,7 +193,7 @@ func monitor(d *Daemon, c *container.Container, stop chan struct{}, probe probe) |
193 | 193 |
logrus.Debugf("Running health check for container %s ...", c.ID) |
194 | 194 |
startTime := time.Now() |
195 | 195 |
ctx, cancelProbe := context.WithTimeout(context.Background(), probeTimeout) |
196 |
- results := make(chan *types.HealthcheckResult) |
|
196 |
+ results := make(chan *types.HealthcheckResult, 1) |
|
197 | 197 |
go func() { |
198 | 198 |
healthChecksCounter.Inc() |
199 | 199 |
result, err := probe.run(ctx, d, c) |
... | ... |
@@ -216,8 +216,10 @@ func monitor(d *Daemon, c *container.Container, stop chan struct{}, probe probe) |
216 | 216 |
select { |
217 | 217 |
case <-stop: |
218 | 218 |
logrus.Debugf("Stop healthcheck monitoring for container %s (received while probing)", c.ID) |
219 |
- // Stop timeout and kill probe, but don't wait for probe to exit. |
|
220 | 219 |
cancelProbe() |
220 |
+ // Wait for probe to exit (it might take a while to respond to the TERM |
|
221 |
+ // signal and we don't want dying probes to pile up). |
|
222 |
+ <-results |
|
221 | 223 |
return |
222 | 224 |
case result := <-results: |
223 | 225 |
handleProbeResult(d, c, result, stop) |