fix(api): cleaner exits before respawn (#66135)

This commit is contained in:
Mrugesh Mohapatra
2026-03-03 20:43:13 +05:30
committed by GitHub
parent b5e3a94693
commit a391dc0664
2 changed files with 17 additions and 5 deletions
+6 -1
View File
@@ -41,6 +41,7 @@ import {
FCC_ENABLE_SWAGGER_UI,
FCC_ENABLE_SHADOW_CAPTURE,
FCC_ENABLE_SENTRY_ROUTES,
FREECODECAMP_NODE_ENV,
GROWTHBOOK_FASTIFY_API_HOST,
GROWTHBOOK_FASTIFY_CLIENT_KEY
} from './utils/env.js';
@@ -86,7 +87,11 @@ export const buildOptions: FastifyHttpOptions<
loggerInstance: getLogger(),
genReqId: () => randomBytes(8).toString('hex'),
// disabled so we can customise the request/response logging
disableRequestLogging: true
disableRequestLogging: true,
// destroy all connections on close to avoid EADDRINUSE
// on restart, in development. Leave default in production.
forceCloseConnections:
FREECODECAMP_NODE_ENV === 'production' ? ('idle' as const) : true
};
/**
+11 -4
View File
@@ -8,8 +8,17 @@ const start = async () => {
const stop = async (signal: NodeJS.Signals) => {
fastify.log.info(`Received ${signal}, shutting down.`);
fastify.server.closeAllConnections();
await new Promise<void>(resolve => {
fastify.server.close(() => resolve());
});
// Yield one tick so libuv can finalize uv_close() on the TCP handle
// before pino's autoEnd blocks the event loop via Atomics.wait().
await new Promise<void>(resolve => setImmediate(resolve));
await fastify.close();
fastify.log.info('Shutdown complete');
process.exit(0);
};
@@ -17,9 +26,7 @@ const start = async () => {
process.on('SIGTERM', signal => void stop(signal));
try {
const port = Number(PORT);
fastify.log.info(`Starting server on port ${port}`);
await fastify.listen({ port, host: HOST });
await fastify.listen({ port: Number(PORT), host: HOST });
} catch (err) {
fastify.log.error(err);
process.exit(1);