fix(agent/agentcontainers): ensure proper channel closure for updateTrigger (#18631)

This commit is contained in:
Mathias Fredriksson
2025-06-27 18:05:48 +03:00
committed by GitHub
parent 6d305df67d
commit b4aa643dfa

View File

@ -421,12 +421,17 @@ func (api *API) updaterLoop() {
// advancing the clock.
ticker := api.clock.TickerFunc(api.ctx, api.updateInterval, func() error {
done := make(chan error, 1)
defer close(done)
var sent bool
defer func() {
if !sent {
close(done)
}
}()
select {
case <-api.ctx.Done():
return api.ctx.Err()
case api.updateTrigger <- done:
sent = true
err := <-done
if err != nil {
if errors.Is(err, context.Canceled) {
@ -455,6 +460,7 @@ func (api *API) updaterLoop() {
// Note that although we pass api.ctx here, updateContainers
// has an internal timeout to prevent long blocking calls.
done <- api.updateContainers(api.ctx)
close(done)
}
}
}
@ -798,12 +804,19 @@ func (api *API) RefreshContainers(ctx context.Context) (err error) {
}()
done := make(chan error, 1)
var sent bool
defer func() {
if !sent {
close(done)
}
}()
select {
case <-api.ctx.Done():
return xerrors.Errorf("API closed: %w", api.ctx.Err())
case <-ctx.Done():
return ctx.Err()
case api.updateTrigger <- done:
sent = true
select {
case <-api.ctx.Done():
return xerrors.Errorf("API closed: %w", api.ctx.Err())