Skip to content

Clean up clients after startup failure and propagate startup query errors - #204

Open
melroy89 wants to merge 1 commit into
fastify:mainfrom
melroy89:fix/startup-client-cleanup
Open

melroy89 wants to merge 1 commit into
fastify:mainfrom
melroy89:fix/startup-client-cleanup

Conversation

@melroy89

@melroy89 melroy89 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

A database connection can complete after Fastify's startup timeout and remain usable after fastify.close(), because cleanup is registered only after initialization succeeds. Late completion can also raise an unhandled error while adding the shutdown hook. Separately, the promise-connection startup query is not returned into its promise chain: a query rejection becomes unhandled and Fastify reports a timeout instead of the SQL error.

Install cleanup before initialization, close each client at most once, and await late promise connections during shutdown. Run initialization as a separate plugin step so after() observes its timeout without stalling awaited registration. Await the startup query in both connector APIs and preserve its original error while cleaning up. mysql2's format helper, runtime guards, and result shapes remain intact.

This adapts the lifecycle fix from my MariaDB fork, melroy89/fastify-mariadb#4, where I fixed both registration and startup-cleanup issues. The registration follow-up to #201 is submitted separately; this PR contains only lifecycle changes.

Validation with mysql2 3.24.4, Node.js 24, and isolated MySQL 9.7.2:

  • npm run lint and all 15 TypeScript assertions passed.
  • 63 runtime tests passed with 100% statement, branch, function, and line coverage.
  • Real database tests cover delayed handshakes across callback/promise pools/connections, shutdown before and after late completion, startup-query failures, cleanup errors, and awaited registration followed by more plugins/routes.
  • The original local delayed-startup probes now reject queries after shutdown and report no surviving sockets or unhandled errors in all four modes. Temporary probes remain outside the repository.
  • Both fix branches merge cleanly; their combined result passed 75 runtime tests with 100% coverage, lint, and TypeScript checks.

Pending promise connections must settle before they can be closed, so shutdown may wait for the configured connectTimeout. Local validation is complete.

This branch has not been deployed

No deployments
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.

1 participant