Skip to content

feat: improve graceful shutdown with configurable timeout#23

Open
bianbiandashen wants to merge 1 commit into
runta-dev:mainfrom
bianbiandashen:feat/graceful-shutdown
Open

feat: improve graceful shutdown with configurable timeout#23
bianbiandashen wants to merge 1 commit into
runta-dev:mainfrom
bianbiandashen:feat/graceful-shutdown

Conversation

@bianbiandashen

Copy link
Copy Markdown
Contributor

Summary

Enhance the graceful shutdown mechanism with better logging and a configurable timeout setting.

Problem

The current shutdown implementation:

  • Doesn't clearly log which signal triggered the shutdown
  • Has no configurable timeout for waiting on in-flight requests
  • Uses generic log messages

In Kubernetes deployments, pods receive SIGTERM during rolling updates and need predictable drain behavior.

Solution

Configurable Shutdown Timeout

Add shutdown_timeout_secs to the [server] config section:

[server]
host = "127.0.0.1"
port = 18790
shutdown_timeout_secs = 30  # Wait up to 30s for requests to complete

Default: 30 seconds

Improved Logging

The shutdown flow now logs:

  1. Which signal was received (SIGTERM vs SIGINT/Ctrl+C)
  2. The configured timeout duration
  3. Clear indication of graceful completion

Example output:

INFO Received SIGTERM, initiating graceful shutdown...
INFO timeout_secs=30 Waiting for in-flight requests to complete...
INFO ClawShell shut down gracefully

Shutdown Behavior

  1. Receive SIGTERM or SIGINT (Ctrl+C)
  2. Stop accepting new connections immediately
  3. Wait for in-flight requests to complete (up to timeout)
  4. Remove PID file
  5. Exit cleanly

Changes

  • src/config.rs: Add shutdown_timeout_secs to ServerConfig
  • src/main.rs: Improve shutdown signal handling and logging
  • clawshell.example.toml: Document new configuration option

Test Plan

  • Build succeeds
  • Verified SIGINT (Ctrl+C) triggers graceful shutdown with proper logging
  • Verified configuration parsing with default value
  • All existing tests pass

Enhance the graceful shutdown mechanism with better logging and a
configurable timeout setting.

Changes:
- Add shutdown_timeout_secs to [server] config (default: 30 seconds)
- Improve shutdown logging to show which signal was received
- Log the configured timeout when shutdown begins
- Change final log message to indicate graceful completion

The shutdown flow now:
1. Receives SIGTERM or SIGINT (Ctrl+C)
2. Logs which signal was received
3. Stops accepting new connections
4. Logs the timeout and waits for in-flight requests
5. Completes when all requests finish (or timeout expires)
6. Removes PID file and logs graceful completion

Configuration example:
```toml
[server]
shutdown_timeout_secs = 30  # Wait up to 30s for requests to complete
```

This is important for Kubernetes deployments where pods receive SIGTERM
during rolling updates and need time to drain connections.
Comment thread src/config.rs
/// requests to complete before forcefully terminating.
/// Default: 30 seconds.
#[serde(default = "default_shutdown_timeout_secs")]
pub shutdown_timeout_secs: u64,

@ADD-SP ADD-SP Feb 15, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to use std::time::Duration here?

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.

2 participants