Skip to content

Commit d64e123

Browse files
fix: Prevent panic on context cancellation during agent shutdown
The leader election callback in the agent was previously panicking if the agent encountered an error, including context.Canceled which is expected during graceful shutdown. This commit modifies the callback to check if the error is context.Canceled. If so, it logs an informational message and exits gracefully. Other unexpected errors will still cause a panic. Signed-off-by: davidepasquero <[email protected]>
1 parent b2be0d8 commit d64e123

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

cmd/agent/run.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,19 @@ func run(options *config.AgentOptions) error {
4242

4343
callback := func(ctx context.Context) {
4444
if err := agent.Run(ctx); err != nil {
45-
panic(err)
45+
// Check if the error is context.Canceled, which is expected on graceful shutdown.
46+
if errors.Is(err, context.Canceled) {
47+
logrus.Info("Agent run completed due to context cancellation.")
48+
} else {
49+
// For any other error, it's unexpected, so panic.
50+
logrus.Errorf("Agent run failed with unexpected error: %v", err)
51+
panic(err)
52+
}
4653
}
54+
// Wait for the context to be done, ensuring the leader election logic
55+
// holds the leadership until the context is fully cancelled.
4756
<-ctx.Done()
57+
logrus.Info("Leader election callback completed as context is done.")
4858
}
4959

5060
httpServerOptions := config.HTTPServerOptions{

0 commit comments

Comments
 (0)