File tree 1 file changed +17
-3
lines changed
1 file changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -300,6 +300,9 @@ func newLogFollower(
300
300
}
301
301
302
302
func (f * logFollower ) follow () {
303
+ var cancel context.CancelFunc
304
+ f .ctx , cancel = context .WithCancel (f .ctx )
305
+ defer cancel ()
303
306
// note that we only need to subscribe to updates if the job is not yet
304
307
// complete.
305
308
if ! f .complete {
@@ -405,17 +408,28 @@ func (f *logFollower) follow() {
405
408
}
406
409
407
410
func (f * logFollower ) listener (_ context.Context , message []byte , err error ) {
411
+ // in this function we always pair writes to channels with a select on the context
412
+ // otherwise we could block a goroutine if the follow() method exits.
408
413
if err != nil {
409
- f .errors <- err
414
+ select {
415
+ case <- f .ctx .Done ():
416
+ case f .errors <- err :
417
+ }
410
418
return
411
419
}
412
420
var n provisionersdk.ProvisionerJobLogsNotifyMessage
413
421
err = json .Unmarshal (message , & n )
414
422
if err != nil {
415
- f .errors <- err
423
+ select {
424
+ case <- f .ctx .Done ():
425
+ case f .errors <- err :
426
+ }
416
427
return
417
428
}
418
- f .notifications <- n
429
+ select {
430
+ case <- f .ctx .Done ():
431
+ case f .notifications <- n :
432
+ }
419
433
}
420
434
421
435
// query fetches the latest job logs from the database and writes them to the
You can’t perform that action at this time.
0 commit comments