Skip to content

Commit b4aa643

Browse files
authored
fix(agent/agentcontainers): ensure proper channel closure for updateTrigger (#18631)
1 parent 6d305df commit b4aa643

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

agent/agentcontainers/api.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,17 @@ func (api *API) updaterLoop() {
421421
// advancing the clock.
422422
ticker := api.clock.TickerFunc(api.ctx, api.updateInterval, func() error {
423423
done := make(chan error, 1)
424-
defer close(done)
425-
424+
var sent bool
425+
defer func() {
426+
if !sent {
427+
close(done)
428+
}
429+
}()
426430
select {
427431
case <-api.ctx.Done():
428432
return api.ctx.Err()
429433
case api.updateTrigger <- done:
434+
sent = true
430435
err := <-done
431436
if err != nil {
432437
if errors.Is(err, context.Canceled) {
@@ -455,6 +460,7 @@ func (api *API) updaterLoop() {
455460
// Note that although we pass api.ctx here, updateContainers
456461
// has an internal timeout to prevent long blocking calls.
457462
done <- api.updateContainers(api.ctx)
463+
close(done)
458464
}
459465
}
460466
}
@@ -798,12 +804,19 @@ func (api *API) RefreshContainers(ctx context.Context) (err error) {
798804
}()
799805

800806
done := make(chan error, 1)
807+
var sent bool
808+
defer func() {
809+
if !sent {
810+
close(done)
811+
}
812+
}()
801813
select {
802814
case <-api.ctx.Done():
803815
return xerrors.Errorf("API closed: %w", api.ctx.Err())
804816
case <-ctx.Done():
805817
return ctx.Err()
806818
case api.updateTrigger <- done:
819+
sent = true
807820
select {
808821
case <-api.ctx.Done():
809822
return xerrors.Errorf("API closed: %w", api.ctx.Err())

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy