Skip to content

fix: implement graceful shutdown to prevent db connection leaks#344

Merged
gboigwe merged 1 commit intoThinkLikeAFounder:mainfrom
Dopezapha:fix/308-db-connection-leak
Mar 28, 2026
Merged

fix: implement graceful shutdown to prevent db connection leaks#344
gboigwe merged 1 commit intoThinkLikeAFounder:mainfrom
Dopezapha:fix/308-db-connection-leak

Conversation

@Dopezapha
Copy link
Copy Markdown
Contributor

Closes #308

Overview

This PR addresses a critical connection leak where process.exit(1) was terminating the production backend during DB connection faults or orchestrator restarts without systematically closing active databases. During crash loops, this was preventing dead connections from being returned and rapidly exhausting the Postgres pool threshold limit.

Changes Implemented

Centralized Teardown Routine

Created an async shutdown(code) mechanism that systematically disconnects all persistent endpoints properly inside a safe try/catch.

Fixed exit(1) Leaks

Replaced abrupt process.exit(1) calls across index.ts (specifically inside DB/Prisma health checks and start() catch loops) with await shutdown(1) inside production environments to intercept and close connections before faulting.

Corrected Issue Misconceptions (Missing Handlers)

The originating issue asserted that process.on('SIGTERM') handlers already existed natively and only selectively missed the error paths. This was incorrect — no graceful signal handlers were bound. Fully functioning SIGTERM and SIGINT application handlers have now been globally established.

Added HTTP Server & Redis Drain

The teardown safely waits for server.close() to stop fetching in-flight requests, and gracefully disconnects redisClient.quit() alongside Prisma and the Postgres pg pool via await pool.end().

Testing

  • Validated clean disconnect mechanisms during simulated database disconnects on startup.
  • Safe O(1) scope-limited teardown routines ensure no cross-contamination inside or outside of production.

@drips-wave
Copy link
Copy Markdown

drips-wave bot commented Mar 27, 2026

@Dopezapha 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! 🚀

Learn more about application limits

@gboigwe gboigwe merged commit 7c6faf5 into ThinkLikeAFounder:main Mar 28, 2026
1 of 3 checks passed
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.

[BUG] Backend: process.exit(1) without disconnecting database — connection leak on crash loops

2 participants