Skip to content

Commit 1b998c0

Browse files
fix: /restart restarts the gateway over and over instead of once (#127179)
The /restart command is dispatched from a durable channel ingress row. The gateway begins its restart drain before that dispatch returns, so the row is still claimed when root-work admission closes. The drain sees GatewayDrainingError and releases the row without spending its retry budget (#125918), which is correct for an ordinary message and wrong for a command that caused the drain: the successor gateway claims the same row and runs /restart again, forever. Every boot is clean, so the gateway looks healthy throughout. Adopt the ingress claim in the restart command handler before scheduling the restart. The command is not idempotent, so losing the acknowledgement is better than an unbounded restart loop. The drain's deferral branch is unchanged. Co-authored-by: Marvinthebored <[email protected]>
1 parent 5afc6fb commit 1b998c0

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎src/auto-reply/reply/commands-session-restart.test.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,79 @@ describe("handleRestartCommand", () => {
186186
}
187187
});
188188

189+
it("adopts the durable ingress claim before scheduling the restart", async () => {
190+
const order: string[] = [];
191+
mocks.scheduleGatewaySigusr1Restart.mockImplementationOnce((_opts) => {
192+
order.push("schedule");
193+
return { scheduled: true };
194+
});
195+
const handler = () => {};
196+
process.on("SIGUSR1", handler);
197+
try {
198+
await handleRestartCommand(
199+
restartCommandParams({
200+
opts: {
201+
turnAdoptionLifecycle: {
202+
onAdopted: () => {
203+
order.push("adopt");
204+
},
205+
},
206+
} as HandleCommandsParams["opts"],
207+
}),
208+
true,
209+
);
210+
} finally {
211+
process.removeListener("SIGUSR1", handler);
212+
}
213+
214+
// Unadopted at restart => drain releases with recordAttempt:false and the
215+
// successor replays /restart, restarting again forever.
216+
expect(order).toEqual(["adopt", "schedule"]);
217+
});
218+
219+
it("adopts the durable ingress claim before the fallback restart path", async () => {
220+
const order: string[] = [];
221+
mocks.triggerOpenClawRestart.mockImplementationOnce(() => {
222+
order.push("trigger");
223+
return { ok: true, method: "launchctl" };
224+
});
225+
226+
await handleRestartCommand(
227+
restartCommandParams({
228+
opts: {
229+
turnAdoptionLifecycle: {
230+
onAdopted: () => {
231+
order.push("adopt");
232+
},
233+
},
234+
} as HandleCommandsParams["opts"],
235+
}),
236+
true,
237+
);
238+
239+
expect(order).toEqual(["adopt", "trigger"]);
240+
});
241+
242+
it("does not restart when ingress adoption was lost to another owner", async () => {
243+
await expect(
244+
handleRestartCommand(
245+
restartCommandParams({
246+
opts: {
247+
turnAdoptionLifecycle: {
248+
onAdopted: () => {
249+
throw new Error("ingress adoption lost: guillotined");
250+
},
251+
},
252+
} as HandleCommandsParams["opts"],
253+
}),
254+
true,
255+
),
256+
).rejects.toThrow("ingress adoption lost");
257+
258+
expect(mocks.triggerOpenClawRestart).not.toHaveBeenCalled();
259+
expect(mocks.scheduleGatewaySigusr1Restart).not.toHaveBeenCalled();
260+
});
261+
189262
it("rejects authorized non-owner restart commands", async () => {
190263
const result = await handleRestartCommand(
191264
restartCommandParams({

‎src/auto-reply/reply/commands-session.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,13 @@ export const handleRestartCommand: CommandHandler = async (params, allowTextComm
611611
if (!isRestartEnabled(params.cfg)) {
612612
return sessionCommandReply("⚠️ /restart is disabled (commands.restart=false).");
613613
}
614+
// Restart tears this process down before the dispatch that carries /restart can
615+
// return, so the durable ingress claim would still be held when admission closes.
616+
// The drain then releases it without spending retry budget and the successor
617+
// replays /restart forever. Adopt here: the command is not idempotent, so losing
618+
// the acknowledgement beats an unbounded restart loop. Adoption loss throws,
619+
// which correctly aborts the restart because another owner holds the event.
620+
await params.opts?.turnAdoptionLifecycle?.onAdopted();
614621
const hasSigusr1Listener = process.listenerCount("SIGUSR1") > 0;
615622
const sentinelPayload = buildRestartCommandSentinel(params);
616623
if (hasSigusr1Listener) {

0 commit comments

Comments
 (0)