




















@@ -50,8 +50,6 @@ type CiaoModule = {
5050type BonjourCycle = {
5151responder: BonjourResponder;
5252services: Array<{ label: string; svc: BonjourService }>;
53-cleanupUncaughtException?: () => void;
54-cleanupUnhandledRejection?: () => void;
5553};
56545755type ServiceStateTracker = {
@@ -179,6 +177,18 @@ export async function startGatewayBonjourAdvertiser(
179177const { getResponder, Protocol } = await loadCiaoModule();
180178const restoreConsoleLog = installCiaoConsoleNoiseFilter();
181179let requestCiaoRecovery: ((classification: CiaoProcessErrorClassification) => void) | undefined;
180+let cleanupUnhandledRejection: (() => void) | undefined;
181+let cleanupUncaughtException: (() => void) | undefined;
182+let processHandlersCleaned = false;
183+184+function cleanupProcessHandlers() {
185+if (processHandlersCleaned) {
186+return;
187+}
188+processHandlersCleaned = true;
189+cleanupUncaughtException?.();
190+cleanupUnhandledRejection?.();
191+}
182192183193const handleCiaoProcessError = (reason: unknown): boolean => {
184194const classification = classifyCiaoProcessError(reason);
@@ -196,6 +206,8 @@ export async function startGatewayBonjourAdvertiser(
196206}
197207return true;
198208};
209+cleanupUnhandledRejection = deps.registerUnhandledRejectionHandler?.(handleCiaoProcessError);
210+cleanupUncaughtException = deps.registerUncaughtExceptionHandler?.(handleCiaoProcessError);
199211200212try {
201213const hostnameRaw = process.env.OPENCLAW_MDNS_HOSTNAME?.trim() || "openclaw";
@@ -259,16 +271,7 @@ export async function startGatewayBonjourAdvertiser(
259271svc: gateway as unknown as BonjourService,
260272});
261273262-const cleanupUnhandledRejection =
263-services.length > 0 && deps.registerUnhandledRejectionHandler
264- ? deps.registerUnhandledRejectionHandler(handleCiaoProcessError)
265- : undefined;
266-const cleanupUncaughtException =
267-services.length > 0 && deps.registerUncaughtExceptionHandler
268- ? deps.registerUncaughtExceptionHandler(handleCiaoProcessError)
269- : undefined;
270-271-return { responder, services, cleanupUncaughtException, cleanupUnhandledRejection };
274+return { responder, services };
272275}
273276274277async function stopCycle(cycle: BonjourCycle | null, opts?: { shutdownResponder?: boolean }) {
@@ -288,9 +291,6 @@ export async function startGatewayBonjourAdvertiser(
288291}
289292} catch {
290293/* ignore */
291-} finally {
292-cycle.cleanupUncaughtException?.();
293-cycle.cleanupUnhandledRejection?.();
294294}
295295}
296296@@ -483,10 +483,12 @@ export async function startGatewayBonjourAdvertiser(
483483}
484484await stopCycle(cycle, { shutdownResponder: true });
485485restoreConsoleLog();
486+cleanupProcessHandlers();
486487},
487488};
488489} catch (err) {
489490restoreConsoleLog();
491+cleanupProcessHandlers();
490492throw err;
491493}
492494}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。