Skip to content

Add graceful shutdown handler for SIGTERM and SIGINT#16

Open
fantaJinMode wants to merge 1 commit intoLinkForty:mainfrom
fantaJinMode:graceful_shutdown_handler
Open

Add graceful shutdown handler for SIGTERM and SIGINT#16
fantaJinMode wants to merge 1 commit intoLinkForty:mainfrom
fantaJinMode:graceful_shutdown_handler

Conversation

@fantaJinMode
Copy link
Contributor

@fantaJinMode fantaJinMode commented Mar 12, 2026

Description

Introduces a registerGracefulShutdown utility that cleanly closes the Fastify server and database pool on process termination signals, preventing in-flight requests and connections from being dropped abruptly.

Benefits

  • Clean teardown prevents leaked database connections in containerized environments
  • Logs shutdown progress and errors for easier observability
  • Simple one-call API makes it easy to adopt in any server setup

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📝 Documentation update
  • ♻️ Code refactoring
  • ⚡ Performance improvement
  • ✅ Test addition or update

Changes Made

  • Added registerGracefulShutdown(fastify) function to src/index.ts that handles SIGTERM and SIGINT
  • Shutdown sequence closes the Fastify server first, then ends the database pool, then exits with code 0 (or 1 on error)
  • Exported db from database.ts to allow pool teardown during shutdown
  • Updated examples/basic-server.ts to demonstrate usage of the new function

Testing

  • Tests pass locally (npm test)
  • New tests added for new functionality
  • Existing tests updated (if needed)
  • Type checking passes (npm run typecheck)

Checklist

  • My code follows the project's code style
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings or errors
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published
  • I have used conventional commit messages (e.g., feat:, fix:)
  • I have signed the Contributor License Agreement (the CLA bot will prompt you)

Breaking Changes

None

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codecov
Copy link

codecov bot commented Mar 12, 2026

Codecov Report

❌ Patch coverage is 0% with 19 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/index.ts 0.00% 19 Missing ⚠️

📢 Thoughts on this report? Let us know!

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