Skip to content

Commit 0ec2800

Browse files
fix(core): address review feedback on graceful shutdown
- Extract hard-coded timeout into FORCE_SHUTDOWN_TIMEOUT_MS constant - Add JSDoc for shutdown function with @param and @returns - Log error in catch block instead of swallowing silently
1 parent f73a00e commit 0ec2800

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

lib/app.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,22 @@ const start = async () => {
127127
}
128128
};
129129

130-
// Shut down the server
130+
const FORCE_SHUTDOWN_TIMEOUT_MS = 5000;
131+
132+
/**
133+
* Gracefully shut down the server, disconnecting services before exiting.
134+
* If the server promise rejects (e.g. failed to start), logs the error and exits.
135+
* A forced exit is triggered after {@link FORCE_SHUTDOWN_TIMEOUT_MS} ms if shutdown hangs.
136+
*
137+
* @param {Promise<{http: import('http').Server}>} server - Promise returned by {@link start}
138+
* @returns {Promise<void>} Resolves when shutdown is initiated (process exits via callback)
139+
*/
131140
const shutdown = async (server) => {
132-
// Force exit after 5s if graceful shutdown hangs
141+
// Force exit if graceful shutdown hangs
133142
const forceTimeout = setTimeout(() => {
134143
console.error(chalk.red('Forced shutdown (timeout)'));
135144
process.exit(1);
136-
}, 5000);
145+
}, FORCE_SHUTDOWN_TIMEOUT_MS);
137146
forceTimeout.unref();
138147

139148
try {
@@ -147,8 +156,8 @@ const shutdown = async (server) => {
147156
}
148157
process.exit();
149158
});
150-
} catch {
151-
// Server never started — just exit
159+
} catch (err) {
160+
console.error(chalk.red('Shutdown error: server never started or shutdown failed'), err);
152161
process.exit(1);
153162
}
154163
};

0 commit comments

Comments
 (0)