Skip to content

Commit 5b532a7

Browse files
authored
fix(gateway): avoid shutdown stalls with paired workers (#132877)
* fix(gateway): avoid shutdown stalls with paired workers * test(gateway): use consistent shutdown fixture returns * test(ui): drain clipboard focus restoration before teardown
1 parent 45dcabc commit 5b532a7

8 files changed

Lines changed: 622 additions & 146 deletions

‎docs/gateway/restart-recovery.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ gateway stops accepting new work, then waits for active agent turns and
5454
background tasks to finish, up to a drain budget (5 minutes by default). Most
5555
restarts therefore interrupt nothing at all.
5656

57+
Replies to pending node commands remain accepted during the drain, including
58+
worker cleanup started by shutdown. Each reply must still match its live
59+
invocation, node connection, pairing generation, and owning lifecycle. This
60+
lets cleanup finish without waiting for a command timeout; it does not reopen
61+
admission for new requests.
62+
5763
Only work that cannot finish inside the drain budget (or any run interrupted
5864
by a forced restart or a crash) is aborted — and before that happens, each
5965
affected session is marked for recovery.

‎src/gateway/node-registry-private.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ async function invokeNodeRegistryCore(
384384
progressChunks: new Map(),
385385
nextInputSeq: 0,
386386
...(params.onProgress ? { onProgress: params.onProgress } : {}),
387+
// Private transport retains its exact lifecycle owner through reply settlement.
388+
...(allowPrivateCommand ? { isCompletionAuthorized: params.isDispatchAuthorized } : {}),
387389
};
388390
const generationController = params.expectedPairingGeneration
389391
? new AbortController()

‎src/gateway/node-registry.invoke-stream.ts‎

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
captureGatewayRootWorkAdmissionContinuationScope,
3+
isGatewayRestartDraining,
34
type GatewayRootWorkAdmissionContinuationScope,
45
} from "../process/gateway-work-admission.js";
56
import { NODE_INVOKE_PAIRING_CHANGED_ABORT } from "./node-registry-private-token.js";
@@ -36,6 +37,7 @@ export type PendingInvoke = {
3637
nextInputSeq: number;
3738
removeAbortListener?: () => void;
3839
admissionContinuation?: GatewayRootWorkAdmissionContinuationScope;
40+
isCompletionAuthorized?: () => boolean;
3941
};
4042

4143
export type NodeInvokeProgressParams = {
@@ -119,19 +121,8 @@ export class NodeInvokeStreamController {
119121
}
120122

121123
handleResult(params: NodeInvokeResultParams): boolean {
122-
const pending = this.options.pendingInvokes.get(params.id);
123-
if (
124-
!pending ||
125-
pending.nodeId !== params.nodeId ||
126-
pending.connId !== params.connId ||
127-
!this.options.isConnectionActive(pending)
128-
) {
129-
return false;
130-
}
131-
if (this.settleIfExpired(params.id, pending)) {
132-
return false;
133-
}
134-
if (!this.takePending(params.id, pending)) {
124+
const pending = this.getPending(params.id, params.nodeId, params.connId);
125+
if (!pending || !this.takePending(params.id, pending)) {
135126
return false;
136127
}
137128
if (!params.ok) {
@@ -203,17 +194,8 @@ export class NodeInvokeStreamController {
203194
}
204195

205196
handleProgress(params: NodeInvokeProgressParams): boolean {
206-
const pending = this.options.pendingInvokes.get(params.invokeId);
207-
if (
208-
!pending ||
209-
pending.nodeId !== params.nodeId ||
210-
pending.connId !== params.connId ||
211-
!this.options.isConnectionActive(pending) ||
212-
params.seq < pending.nextProgressSeq
213-
) {
214-
return false;
215-
}
216-
if (this.settleIfExpired(params.invokeId, pending)) {
197+
const pending = this.getPending(params.invokeId, params.nodeId, params.connId);
198+
if (!pending || params.seq < pending.nextProgressSeq) {
217199
return false;
218200
}
219201
// Receipt proves execution even without a stream consumer. Keep ignored
@@ -243,7 +225,7 @@ export class NodeInvokeStreamController {
243225
if (chunk === undefined) {
244226
break;
245227
}
246-
if (this.settleIfExpired(params.invokeId, pending)) {
228+
if (!this.getPending(params.invokeId, params.nodeId, params.connId)) {
247229
break;
248230
}
249231
pending.progressChunks.delete(pending.nextProgressSeq);
@@ -263,14 +245,52 @@ export class NodeInvokeStreamController {
263245
pending.progressChunks.clear();
264246
break;
265247
}
266-
if (this.settleIfExpired(params.invokeId, pending)) {
248+
if (!this.getPending(params.invokeId, params.nodeId, params.connId)) {
267249
break;
268250
}
269251
this.resetIdleTimer(params.invokeId, pending);
270252
}
271253
return true;
272254
}
273255

256+
runPendingContinuation<T>(params: {
257+
invokeId: string;
258+
nodeId: string;
259+
connId: string | undefined;
260+
run: () => Promise<T>;
261+
}): Promise<T> | null {
262+
const pending = this.getPending(params.invokeId, params.nodeId, params.connId);
263+
if (!pending) {
264+
return null;
265+
}
266+
if (pending.admissionContinuation) {
267+
return pending.admissionContinuation.run(params.run);
268+
}
269+
// Shutdown cleanup has no request root. Its live private owner grants only
270+
// settlement; do not mint admission or revive a released captured root.
271+
return isGatewayRestartDraining() && pending.isCompletionAuthorized ? params.run() : null;
272+
}
273+
274+
private getPending(id: string, nodeId: string, connId: string | undefined) {
275+
const pending = this.options.pendingInvokes.get(id);
276+
if (
277+
!pending ||
278+
pending.nodeId !== nodeId ||
279+
pending.connId !== connId ||
280+
!this.options.isConnectionActive(pending) ||
281+
this.settleIfExpired(id, pending)
282+
) {
283+
return undefined;
284+
}
285+
// Recheck at settlement as handler loading may await after router admission.
286+
// Some lifecycle owners assert by throwing; either form must fail closed.
287+
try {
288+
return pending.isCompletionAuthorized?.() === false ? undefined : pending;
289+
} catch {
290+
return undefined;
291+
}
292+
}
293+
274294
clearTimers(pending: PendingInvoke): void {
275295
if (pending.hardTimer) {
276296
clearTimeout(pending.hardTimer);

‎src/gateway/node-registry.ts‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,27 +1132,14 @@ export class NodeRegistry {
11321132
return this.invokeStreams.handleProgress(params);
11331133
}
11341134

1135-
/** Re-enters only the root that owns this exact live node invocation. */
1135+
/** Continues only the exact live owner of a pending node invocation. */
11361136
runPendingInvokeContinuation<T>(params: {
11371137
invokeId: string;
11381138
nodeId: string;
11391139
connId: string | undefined;
11401140
run: () => Promise<T>;
11411141
}): Promise<T> | null {
1142-
const pending = this.pendingInvokes.get(params.invokeId);
1143-
if (
1144-
!pending?.admissionContinuation ||
1145-
pending.nodeId !== params.nodeId ||
1146-
pending.connId !== params.connId ||
1147-
!isNodeRegistryPendingInvokeConnectionActive({
1148-
registry: this,
1149-
pending,
1150-
currentNode: this.nodesById.get(params.nodeId),
1151-
})
1152-
) {
1153-
return null;
1154-
}
1155-
return pending.admissionContinuation.run(params.run);
1142+
return this.invokeStreams.runPendingContinuation(params);
11561143
}
11571144

11581145
/** Authorize an inbound system.run event against a recently issued node invoke. */

0 commit comments

Comments
 (0)