P1: Graceful shutdown — drain, flush traces, uncaught handlers (#170)#188
Open
dkijania wants to merge 1 commit into
Open
P1: Graceful shutdown — drain, flush traces, uncaught handlers (#170)#188dkijania wants to merge 1 commit into
dkijania wants to merge 1 commit into
Conversation
Shutdown previously called `server.close()` then `process.exit(0)` from the close event. It didn't bound how long draining could take, never flushed OpenTelemetry spans (losing the tail of traces on deploy), and had no handlers for uncaughtException / unhandledRejection. Add a small, unit-tested `createGracefulShutdown` orchestrator and wire it into the entry point: - Drain in-flight requests via `server.close()`, then run teardown steps (flush the tracer provider, close the Postgres pool) in order. - A hard `SHUTDOWN_TIMEOUT_MS` deadline (default 10s) forces exit if draining or teardown hangs; the process exits at most once. - The handler is idempotent, so a second signal is ignored. - SIGINT/SIGTERM/SIGQUIT plus uncaughtException/unhandledRejection all route through it. `buildPlugins` now returns the tracer provider so the entry point can flush it. Unit tests cover ordering, idempotency, a failing teardown step, and the timeout-forces-exit path with an injected exit hook. Closes #170. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QSuak9smCHbp4N17xjjLF6
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 & why
Part of the production-readiness epic (#163). Closes #170.
Shutdown previously called
server.close()thenprocess.exit(0)from the close event. It didn't bound how long draining could take, never flushed OpenTelemetry spans (losing the tail of traces on every deploy), and had no handlers foruncaughtException/unhandledRejection.Changes
src/server/graceful-shutdown.ts— a small, unit-testedcreateGracefulShutdownorchestrator.server.close(), then run teardown steps (flush the tracer provider, close the Postgres pool) in order.SHUTDOWN_TIMEOUT_MSdeadline (default 10s) forces exit if draining/teardown hangs; the process exits at most once.SIGINT/SIGTERM/SIGQUITplusuncaughtException/unhandledRejectionall route through it.buildPluginsnow returns the tracerproviderso the entry point can flush it.SHUTDOWN_TIMEOUT_MS10000Testing
npm run build— cleannpm run test:unit— all pass; new tests cover ordering, idempotency, a failing teardown step (logged, doesn't abort the rest), and the timeout-forces-exit path (injected exit hook, asserts exit code1fires exactly once)npm run lint— cleannpx prettier --debug-check .— exit 0🤖 Generated with Claude Code