fix(cli): dev/dev:offline Ctrl-C 退出防重入并支持二次强制退出

This commit is contained in:
rbetree
2026-01-16 20:51:10 +08:00
parent 87d1f0244c
commit 0e154bc43e
3 changed files with 102 additions and 17 deletions

View File

@@ -170,13 +170,33 @@ async function main() {
log.ok('就绪', { url: `http://localhost:${actualPort}` });
const shutdown = () => {
log.info('正在关闭...');
server.close(() => process.exit(0));
let shuttingDown = false;
const shutdown = (signal) => {
if (shuttingDown) return;
shuttingDown = true;
process.stdout.write('\n');
log.info('正在关闭...', { signal });
try {
if (typeof server.closeIdleConnections === 'function') server.closeIdleConnections();
if (typeof server.closeAllConnections === 'function') server.closeAllConnections();
} catch {
// ignore
}
const exit = signal === 'SIGINT' ? 130 : 0;
const forceTimer = setTimeout(() => process.exit(exit), 2000);
if (typeof forceTimer.unref === 'function') forceTimer.unref();
server.close(() => {
clearTimeout(forceTimer);
process.exit(exit);
});
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
process.once('SIGINT', () => shutdown('SIGINT'));
process.once('SIGTERM', () => shutdown('SIGTERM'));
}
if (require.main === module) {