mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 18:41:37 +00:00
* fix: defer gateway restart until all replies are sent Fixes a race condition where gateway config changes (e.g., enabling plugins via iMessage) trigger an immediate SIGUSR1 restart, killing the iMessage RPC connection before replies are delivered. Both restart paths (config watcher and RPC-triggered) now defer until all queued operations, pending replies, and embedded agent runs complete (polling every 500ms, 30s timeout). A shared emitGatewayRestart() guard prevents double SIGUSR1 when both paths fire simultaneously. Key changes: - Dispatcher registry tracks active reply dispatchers globally - markComplete() called in finally block for guaranteed cleanup - Pre-restart deferral hook registered at gateway startup - Centralized extractDeliveryInfo() for session key parsing - Post-restart sentinel messages delivered directly (not via agent) - config-patch distinguished from config-apply in sentinel kind Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: single-source gateway restart authorization --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Peter Steinberger <steipete@gmail.com>
59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
/**
|
|
* Global registry for tracking active reply dispatchers.
|
|
* Used to ensure gateway restart waits for all replies to complete.
|
|
*/
|
|
|
|
type TrackedDispatcher = {
|
|
readonly id: string;
|
|
readonly pending: () => number;
|
|
readonly waitForIdle: () => Promise<void>;
|
|
};
|
|
|
|
const activeDispatchers = new Set<TrackedDispatcher>();
|
|
let nextId = 0;
|
|
|
|
/**
|
|
* Register a reply dispatcher for global tracking.
|
|
* Returns an unregister function to call when the dispatcher is no longer needed.
|
|
*/
|
|
export function registerDispatcher(dispatcher: {
|
|
readonly pending: () => number;
|
|
readonly waitForIdle: () => Promise<void>;
|
|
}): { id: string; unregister: () => void } {
|
|
const id = `dispatcher-${++nextId}`;
|
|
const tracked: TrackedDispatcher = {
|
|
id,
|
|
pending: dispatcher.pending,
|
|
waitForIdle: dispatcher.waitForIdle,
|
|
};
|
|
activeDispatchers.add(tracked);
|
|
|
|
const unregister = () => {
|
|
activeDispatchers.delete(tracked);
|
|
};
|
|
|
|
return { id, unregister };
|
|
}
|
|
|
|
/**
|
|
* Get the total number of pending replies across all dispatchers.
|
|
*/
|
|
export function getTotalPendingReplies(): number {
|
|
let total = 0;
|
|
for (const dispatcher of activeDispatchers) {
|
|
total += dispatcher.pending();
|
|
}
|
|
return total;
|
|
}
|
|
|
|
/**
|
|
* Clear all registered dispatchers (for testing).
|
|
* WARNING: Only use this in test cleanup!
|
|
*/
|
|
export function clearAllDispatchers(): void {
|
|
if (!process.env.VITEST && process.env.NODE_ENV !== "test") {
|
|
throw new Error("clearAllDispatchers() is only available in test environments");
|
|
}
|
|
activeDispatchers.clear();
|
|
}
|