

























@@ -69,6 +69,7 @@ type ServiceStateTracker = {
6969type ConsoleLogFn = (...args: unknown[]) => void;
7070type UncaughtExceptionHandler = (error: unknown) => boolean;
7171type UnhandledRejectionHandler = (reason: unknown) => boolean;
72+type ProcessUnhandledRejectionListener = (reason: unknown, promise: Promise<unknown>) => void;
7273type ExecBridge = (command: string, options?: unknown, callback?: unknown) => ChildProcess;
7374type ExecOptionsRecord = Record<string, unknown> & { windowsHide?: boolean };
7475@@ -324,6 +325,25 @@ function installCiaoWindowsExecHidePatch(): () => void {
324325};
325326}
326327328+function installCiaoUnhandledRejectionListener(handler: UnhandledRejectionHandler): () => void {
329+const hadOtherListeners = process.listenerCount("unhandledRejection") > 0;
330+const listener: ProcessUnhandledRejectionListener = (reason) => {
331+if (handler(reason)) {
332+return;
333+}
334+if (hadOtherListeners) {
335+return;
336+}
337+queueMicrotask(() => {
338+throw reason instanceof Error ? reason : new Error(String(reason));
339+});
340+};
341+process.on("unhandledRejection", listener);
342+return () => {
343+process.off("unhandledRejection", listener);
344+};
345+}
346+327347export async function startGatewayBonjourAdvertiser(
328348opts: GatewayBonjourAdvertiseOpts,
329349deps: BonjourAdvertiserDeps = {},
@@ -341,6 +361,7 @@ export async function startGatewayBonjourAdvertiser(
341361let restoreConsoleLog: () => void = () => {};
342362let requestCiaoRecovery: ((classification: CiaoProcessErrorClassification) => void) | undefined;
343363let cleanupUnhandledRejection: (() => void) | undefined;
364+let cleanupDirectUnhandledRejection: (() => void) | undefined;
344365let cleanupUncaughtException: (() => void) | undefined;
345366let processHandlersCleaned = false;
346367@@ -349,6 +370,7 @@ export async function startGatewayBonjourAdvertiser(
349370return;
350371}
351372processHandlersCleaned = true;
373+cleanupDirectUnhandledRejection?.();
352374cleanupUncaughtException?.();
353375cleanupUnhandledRejection?.();
354376}
@@ -380,6 +402,7 @@ export async function startGatewayBonjourAdvertiser(
380402}
381403return true;
382404};
405+cleanupDirectUnhandledRejection = installCiaoUnhandledRejectionListener(handleCiaoProcessError);
383406cleanupUnhandledRejection = deps.registerUnhandledRejectionHandler?.(handleCiaoProcessError);
384407cleanupUncaughtException = deps.registerUncaughtExceptionHandler?.(handleCiaoProcessError);
385408此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。