Feat/graceful shutdown handler#669
Open
Muaysh02 wants to merge 4 commits into
Open
Conversation
added 4 commits
June 30, 2026 09:51
|
@Muaysh02 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
Here is a comprehensive summary of what was implemented:
1. The Core Issue
In production (
process.env.NODE_ENV !== "test"), the application registered duplicate signal handlers forSIGTERMandSIGINTinside src/index.ts.While the system initialized the
GracefulShutdownManagerand calledinstallShutdownHandlers(), it immediately followed with an inlineshutdownhandler registered viaprocess.on(...). This overriding handler immediately calledserver.close(() => process.exit(0))upon receiving a signal. As a result, the process would exit as soon as the HTTP server closed, bypassing the remaining phases of theGracefulShutdownManagersequence (which was responsible for closing database pools, stopping WebSockets, pausing background schedulers, stopping the outbox publisher, and disconnecting from Redis).2. The Solution (Code Changes)
shutdownfunction and its duplicateprocess.on('SIGTERM')andprocess.on('SIGINT')registrations in src/index.ts. This leavesinstallShutdownHandlers()as the single production registration point.GracefulShutdownManager.shutdown(). This executes the following clean, ordered sequence before exiting:server_close: Express HTTP server stops accepting new connections, allowing in-flight HTTP requests to complete.ws_drain: Open WebSocket clients are closed gracefully (code 1000) with a 5-second hard termination fallback.listener_stop: Event consumers are paused at their current offsets.scheduler_drain: Schedulers are stopped and current running jobs are allowed to finish.outbox_stop: Outbox publisher is safely shut down.invalidation_bus_stop: Cross-replica cache invalidation listener connection is closed.pool_close: All three database connection pools (pool,workerPool,replicaPool) are cleanly closed viapool.end().redis_close: Redis client is cleanly disconnected viadisconnect().0(or1if any phase throws an error or the grace period expires).3. Unit Test Integration
We added a sequence verification test in src/tests/gracefulShutdown.test.ts to guarantee order execution:
server.close(),pool.end(),redis.disconnect(), andprocess.exit().process.exit(0)is only triggered after all resources are closed.4. Documentation Updates
## Graceful Shutdownsection documenting how the backend handles SIGTERM/SIGINT signals, and linking to the primary documentation.## Signal Handling in Productionsection detailing the entrypoint signal hooks.Closes #<this-issue>tag.closes #562