Skip to content

fix(serve): graceful shutdown on SIGTERM/SIGINT (exit 0)#177

Open
sebglon wants to merge 1 commit intoaws:mainfrom
sebglon:main
Open

fix(serve): graceful shutdown on SIGTERM/SIGINT (exit 0)#177
sebglon wants to merge 1 commit intoaws:mainfrom
sebglon:main

Conversation

@sebglon
Copy link
Copy Markdown

@sebglon sebglon commented Mar 18, 2026

Problem

The serve command has no signal handler. When Kubernetes sends SIGTERM
to the container (e.g. during pod shutdown, rolling update, or job completion
with native sidecars), Go's default signal handling terminates the process
with exit code 2.

This causes Kubernetes to report the sidecar container as reason: Error
even when the pod/job completed successfully. See issue #176.

Fix

Add a signal handler in Serve() that:

  1. Listens for SIGTERM and SIGINT on a buffered channel
  2. Calls server.Shutdown() with a 5-second context to allow in-flight requests to complete
  3. Returns naturally from Serve() via http.ErrServerClosedexit code 0

Testing

# Start server in background, send SIGTERM, verify exit 0
./aws_signing_helper serve --certificate cert.pem --private-key key.pem ... --port 19911 &
PID=$!
sleep 1 && kill -TERM $PID && wait $PID
echo "Exit code: $?"  # → 0

Fixes #176

The serve command previously called os.Exit(2) when receiving SIGTERM,
which caused Kubernetes to report the sidecar container as errored even
when the pod completed successfully.

This change adds a signal handler that:
- Catches SIGTERM and SIGINT
- Calls server.Shutdown() with a 5s context for in-flight requests
- Lets Serve() return naturally via http.ErrServerClosed → exit 0

Fixes: aws#176

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SigTerm not handled for Kube pod shutdown

1 participant