Repository navigation
Shut serve-s3 down cleanly on SIGTERM and Ctrl-C - #231
Merged
Merged
Conversation
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.
This was referenced Nov 15, 2026
# Conflicts: # CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
serve-s3had 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, sodocker stopwaited out its 10 s timeout and then SIGKILLed it.Server (
protocol-s3)S3Server::shutdown_on(signal, grace): whensignalcompletes, the servergrace,503 ServiceUnavailable(a newS3Errorvariant), which S3 clients retry, and their connections close, sorun()returns with the store released.The reclaim and scrub tasks stop when
run()returns, as before. A server withoutshutdown_onbehaves exactly as it did.CLI (
spacectl serve-s3)--shutdown-grace <SECONDS>defaults to 5, insidedocker stop's 10.shutting down, a second exits at once (status 130), and a clean stop logsstoppedand exits 0.Tests
crates/protocol-s3/tests/shutdown_test.rs, over real sockets:serve_s3_stops_cleanly_on_sigtermincrates/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 storespacectl listopens.Type
--shutdown-grace,S3Server::shutdown_on)Checklist
cargo fmt --all --checkcargo clippy --workspace --all-targets --locked -- -D warningscargo test --workspace --locked(453 passed on Windows; the SIGTERM test runs on Linux and macOS in CI)tokio-util'sCancellationTokenand tokio'ssignalwere 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 runsserve-s3detached and checks thatdocker stopends it in under 8 s with exit status 0.