Skip to content

Shut serve-s3 down cleanly on SIGTERM and Ctrl-C - #231

Merged
saworbit merged 5 commits into
mainfrom
fix/serve-s3-shutdown
Nov 15, 2026
Merged

saworbit merged 5 commits into
mainfrom
fix/serve-s3-shutdown

Conversation

@saworbit

@saworbit saworbit commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

What and why

serve-s3 had no signal handling at all. Outside a container, SIGTERM or Ctrl-C killed it mid-request. As PID 1 in the Docker image it was worse: the kernel ignores a signal that has no handler, so docker stop waited out its 10 s timeout and then SIGKILLed it.

Server (protocol-s3)

S3Server::shutdown_on(signal, grace): when signal completes, the server

  • stops accepting connections and closes idle keep-alive ones,
  • lets requests in flight finish for up to grace,
  • cuts off requests still running after that by dropping their handlers. They get 503 ServiceUnavailable (a new S3Error variant), which S3 clients retry, and their connections close, so run() returns with the store released.

The reclaim and scrub tasks stop when run() returns, as before. A server without shutdown_on behaves exactly as it did.

CLI (spacectl serve-s3)

  • SIGTERM and SIGINT stop it, and on Windows so do Ctrl-C, Ctrl-Break, the console closing and system shutdown. The handlers are registered before serving, so failing to register them is a startup error.
  • --shutdown-grace <SECONDS> defaults to 5, inside docker stop's 10.
  • The first signal logs shutting down, a second exits at once (status 130), and a clean stop logs stopped and exits 0.

Tests

  • crates/protocol-s3/tests/shutdown_test.rs, over real sockets:
    • an idle keep-alive connection is closed and doesn't delay the stop;
    • a PUT whose body is still arriving at the signal finishes with 200 and is in the store when it reopens;
    • a PUT still unfinished after the grace period gets 503, nothing of it is stored, and the store reopens.
  • serve_s3_stops_cleanly_on_sigterm in crates/spacectl/tests/cli_test.rs (Unix): sends the real binary SIGTERM and checks it exits 0 within 8 s, logs both lines, and leaves a store spacectl list opens.
  • The cut-off test fails without the handler cancellation: the cut-off connection's task kept the store's lock.

Type

  • Bug fix
  • New feature (--shutdown-grace, S3Server::shutdown_on)

Checklist

  • cargo fmt --all --check
  • cargo clippy --workspace --all-targets --locked -- -D warnings
  • cargo test --workspace --locked (453 passed on Windows; the SIGTERM test runs on Linux and macOS in CI)
  • CHANGELOG.md entry, and the S3 quick start notes Ctrl-C
  • No new dependencies (tokio-util's CancellationToken and tokio's signal were already enabled)

Docker

With #228 merged in, the Dockerfile header no longer tells people to run with --init. The Docker workflow's smoke test now runs serve-s3 detached and checks that docker stop ends it in under 8 s with exit status 0.

serve-s3 had no signal handling. Outside a container the default action
killed it mid-request; as PID 1 in the Docker image the kernel ignores
an unhandled SIGTERM, so docker stop waited out its 10 s and then
killed it.

S3Server::shutdown_on takes a signal and a grace period. On the signal
the server stops accepting connections, closes idle ones and lets the
requests in flight finish. Those still running when the grace period
runs out have their handlers dropped and get 503 ServiceUnavailable,
which S3 clients retry, so run() can return with the store released.

spacectl registers SIGTERM and SIGINT (on Windows Ctrl-C, Ctrl-Break,
console close and system shutdown) before it serves, so a failure to
listen for them is a startup error. --shutdown-grace defaults to 5 s,
inside docker stop's 10. The first signal logs "shutting down", a
second exits at once, and a clean stop logs "stopped" and exits 0.
The Dockerfile header from #228 told people to run the image with
--init, since spacectl ignored signals as PID 1. It handles them now.
The Docker workflow checks it: docker stop must end serve-s3 in under
8 s with exit status 0.
A reclaim pass compacts on a blocking thread, which stopping the server
can't cancel, and dropping the runtime at the end of main waited for it
however long it took. On a large store that held the exit past docker
stop's timeout. spacectl now builds its runtime by hand and gives
leftover blocking work 2 s at exit. Compaction writes the next
generation beside the live file and the next open discards one it
didn't finish, so abandoning it is safe.

ARCHITECTURE.md, the S3 quick start and capsule.md say how the server
stops, and the CI doc lists the docker stop check.
@saworbit
saworbit merged commit c62c206 into main Nov 15, 2026
17 checks passed
@saworbit
saworbit deleted the fix/serve-s3-shutdown branch November 15, 2026 04:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant