When the control pipe drops while a send() is awaiting its ack, rejectControlPending() rejects both the ack and the reply deferreds. The reply is only awaited after the ack, so an ack-first rejection leaves the reply promise rejected with NO awaiter — an unhandled rejection that, under Node's default policy, kills the process. In Docker supervisor restarts it (every session drops for ~1s); on a bare Windows run it just dies, surfacing as "connection pipe closed" with the bot offline after hours of uptime. Attach a no-op .catch() to each deferred at creation so a rejection can never go unhandled (the real await still observes the value/rejection). Add a regression test that drops the pipe mid-send and asserts no unhandled rejection escapes — it fails without the guard. Also install last-resort process guards (unhandledRejection / uncaughtException) in the app entry: log and keep serving instead of exiting, so any other stray rejection cannot take the daemon down and its stack is captured. The bridge's watcher reconnects the dropped pipe on the next tick.
79 lines
3.1 KiB
TypeScript
79 lines
3.1 KiB
TypeScript
import { HookManager } from '@snowluma/bridge';
|
|
import { closeLogger, createLogger } from '@snowluma/common/logger';
|
|
import { loadRuntimeConfig } from '@snowluma/common/runtime';
|
|
import { OneBotManager } from '@snowluma/onebot/manager';
|
|
import { BridgeManager } from './bridge/manager';
|
|
|
|
const runtimeConfig = loadRuntimeConfig();
|
|
const log = createLogger('App');
|
|
|
|
// Last-resort process guards. This is a long-running daemon: a hooked QQ pipe
|
|
// can drop at any moment, and a stray rejection on a detached callback (or a
|
|
// throw that escapes one) must NOT take the whole process down — otherwise
|
|
// Docker restarts it (dropping every session) and a bare Windows run just dies
|
|
// ("connection pipe closed", bot offline). Log and keep serving; the bridge's
|
|
// own watcher reconnects the dropped pipe. Targeted handlers still cover the
|
|
// known paths (e.g. the hook-client send deferreds) — these only catch what
|
|
// slips through, and surface its stack so the real source can be fixed.
|
|
process.on('unhandledRejection', (reason) => {
|
|
log.error('unhandledRejection (kept alive): %s',
|
|
reason instanceof Error ? (reason.stack ?? reason.message) : String(reason));
|
|
});
|
|
process.on('uncaughtException', (error) => {
|
|
log.error('uncaughtException (kept alive): %s', error.stack ?? error.message);
|
|
});
|
|
|
|
async function main() {
|
|
log.info('SnowLuma starting');
|
|
|
|
const bridgeManager = new BridgeManager();
|
|
const oneBotManager = new OneBotManager();
|
|
const autoLoadOnDiscovery = resolveAutoLoad(runtimeConfig.hookAutoLoad);
|
|
const hookManager = new HookManager({ bridgeManager, autoLoadOnDiscovery });
|
|
if (autoLoadOnDiscovery) {
|
|
log.info('hook auto-load enabled: every discovered QQ process will be injected');
|
|
}
|
|
|
|
oneBotManager.bind(bridgeManager);
|
|
|
|
if (
|
|
(typeof __BUILD_WEBUI__ !== 'undefined' && __BUILD_WEBUI__) ||
|
|
process.env.SNOWLUMA_DEV_WEBUI === '1'
|
|
) {
|
|
try {
|
|
const { initWebUI } = await import('./webui/server');
|
|
await initWebUI(runtimeConfig.webuiPort || 5099, oneBotManager, hookManager);
|
|
} catch (err) {
|
|
log.error('Failed to start WebUI: ', err);
|
|
}
|
|
}
|
|
|
|
// Graceful shutdown: dispose managers, await log flush, then exit.
|
|
// SIGINT (Ctrl-C) and SIGTERM (Docker/systemd) take the same path.
|
|
const shutdown = (signal: string) => async () => {
|
|
log.warn(`Shutting down (${signal})...`);
|
|
oneBotManager.dispose();
|
|
hookManager.dispose();
|
|
await closeLogger();
|
|
process.exit(0);
|
|
};
|
|
process.on('SIGINT', shutdown('SIGINT'));
|
|
process.on('SIGTERM', shutdown('SIGTERM'));
|
|
}
|
|
|
|
function resolveAutoLoad(fromConfig: boolean | undefined): boolean {
|
|
const envRaw = process.env.SNOWLUMA_HOOK_AUTOLOAD;
|
|
if (typeof envRaw === 'string' && envRaw.trim()) {
|
|
const v = envRaw.trim().toLowerCase();
|
|
if (v === '1' || v === 'true' || v === 'yes' || v === 'on') return true;
|
|
if (v === '0' || v === 'false' || v === 'no' || v === 'off') return false;
|
|
}
|
|
return fromConfig === true;
|
|
}
|
|
|
|
main().catch(async (error) => {
|
|
log.error(error instanceof Error ? (error.stack ?? error.message) : String(error));
|
|
await closeLogger();
|
|
process.exit(1);
|
|
});
|