fix(agent): handle context cancellation (#5323)

Signed-off-by: ivaltryek <meet.vasani86@gmail.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
This commit is contained in:
Meet Vasani 2025-07-21 17:54:04 +05:30 committed by GitHub
parent f209256bd9
commit 5c9fc61619
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -267,6 +267,11 @@ func run(ctx context.Context, c *cli.Command, backends []types.Backend) error {
err := client.ReportHealth(grpcCtx)
if err != nil {
log.Err(err).Msg("failed to report health")
// Check if the error is due to context cancellation
if grpcCtx.Err() != nil || agentCtx.Err() != nil {
log.Debug().Msg("terminating health reporting due to context cancellation")
return nil
}
}
select {
@ -291,8 +296,18 @@ func run(ctx context.Context, c *cli.Command, backends []types.Backend) error {
log.Debug().Msg("polling new steps")
if err := runner.Run(agentCtx, shutdownCtx); err != nil {
log.Error().Err(err).Msg("runner done with error")
return err
log.Error().Err(err).Msg("runner error, retrying...")
// Check if context is canceled
if agentCtx.Err() != nil {
return nil
}
// Wait a bit before retrying to avoid hammering the server
select {
case <-agentCtx.Done():
return nil
case <-time.After(time.Second * 5):
// Continue to next iteration
}
}
}
})