Skip to content

Commit 4b4eb96

Browse files
committed
fix(update): hand over only after the proxy confirmed the deferral (#7423)
Review r3: when /api/stop failed before honoring the receipt, the stop fell back to a signal and a standalone proxy's own exit cleanup restored the pristine catalog, yet the parent still exited 81. ocx stop now records whether the stopped proxy answered 'deferred' and hands over only then; a manager-stopped service proxy keeps its own OCX_SERVICE preservation.
1 parent 4717400 commit 4b4eb96

4 files changed

Lines changed: 29 additions & 7 deletions

File tree

‎src/cli/index.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,7 @@ async function handleStopUnlocked(snapshot?: GuardedStopSnapshot) {
11361136
let teardownNonce: string | undefined;
11371137
// #7423: an update that restarts the proxy asks this stop to leave Codex routing for the replacement.
11381138
const handoverRequested = updateTeardownHandoverRequested() && !stoppingSibling && !connectedClientSkip;
1139+
let deferralHonoredByProxy = false;
11391140
const claimTeardown = (endpoint: { hostname: string; port: number }, endpointSource: "exact" | "guessed") => {
11401141
if (teardownNonce || stoppingSibling) return;
11411142
if (connectedClientSkip) return;
@@ -1167,7 +1168,8 @@ async function handleStopUnlocked(snapshot?: GuardedStopSnapshot) {
11671168
// A valid receipt means the proxy deferred shared teardown to this process. If the
11681169
// receipt could not be written, the proxy restores it itself and the caller must not
11691170
// attempt a second restore after a graceful stop. A hard-kill always leaves restore
1170-
// to this process.
1171+
// to this process. #7423: only a confirmed deferral kept the proxy's own exit cleanup off.
1172+
if (graceful && teardownNonce) deferralHonoredByProxy = true;
11711173
return graceful && !teardownNonce;
11721174
};
11731175
const approvedEndpoint = snapshot
@@ -1435,7 +1437,7 @@ async function handleStopUnlocked(snapshot?: GuardedStopSnapshot) {
14351437
if (connectedClientSkip) console.log(connectedClientSkip);
14361438
const restoreBlocked = ownershipBlocked || inheritedBlocks || nativeRestoreHandledByProxy || stoppingSibling || connectedClientSkip !== null;
14371439
const handedOver = stopHandsTeardownOver({ requested: handoverRequested, restoreBlocked, nonce: teardownNonce,
1438-
stopFailed, recoveredCount: recoveredNonces.length, proxy: record.proxy, service: record.service });
1440+
stopFailed, recoveredCount: recoveredNonces.length, proxy: record.proxy, service: record.service, deferralHonored: deferralHonoredByProxy });
14391441
if (handedOver) announceStopHandover();
14401442
else if (!restoreBlocked) {
14411443
if (recoveredNonces.length > 0) {

‎src/update/teardown-handover.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ export type StopHandoverFacts = {
6161
proxy: string;
6262
/** What the service-manager stop returned; a manager can take the proxy down itself. */
6363
service?: string;
64+
/**
65+
* The proxy this run stopped answered `deferred` before exiting. Any fallback (a failed
66+
* or unconfirmed /api/stop, then a signal) lets a standalone proxy's exit cleanup restore
67+
* native Codex and the pristine catalog, so nothing is left to hand over.
68+
*/
69+
deferralHonored?: boolean;
6470
};
6571

6672
/**
@@ -76,7 +82,7 @@ export type StopHandoverFacts = {
7682
*/
7783
export function stopHandsTeardownOver(facts: StopHandoverFacts): boolean {
7884
const managerStopped = facts.service === "stopped" || facts.service === "stopped-respawnable";
79-
const proxyDown = facts.proxy === "stopped" || facts.proxy === "stopped-orphan"
85+
const proxyDown = ((facts.proxy === "stopped" || facts.proxy === "stopped-orphan") && facts.deferralHonored === true)
8086
|| (facts.proxy === "not-running" && managerStopped);
8187
return facts.requested && !facts.restoreBlocked && facts.nonce !== undefined && !facts.stopFailed
8288
&& facts.recoveredCount === 0 && proxyDown && pendingTeardownsAreExactly([facts.nonce])

‎tests/fixtures/parent-stop-runner.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const options = JSON.parse(readFileSync(0, "utf8")) as {
2929
handover?: boolean;
3030
/** The runtime-port record is missing, so the stop can only record a guessed endpoint. */
3131
noRuntimeRecord?: boolean;
32+
/** Like the real readPid, report no PID once the process is gone (a manager stop exited it). */
33+
deadPidReadsNull?: boolean;
3234
};
3335
if (options.proxyProbePort) {
3436
// Deliberately bypass every fixture mock for the real proxy-environment regression.
@@ -104,7 +106,7 @@ mock.module("../../src/config/pending-teardown", () => ({
104106
const state = await import("../../src/config/process-state");
105107
mock.module("../../src/config/process-state", () => ({
106108
...state,
107-
readPid: () => options.orphan || options.alreadyStopped ? null : fakePid,
109+
readPid: () => options.orphan || options.alreadyStopped || (options.deadPidReadsNull && !alive) ? null : fakePid,
108110
readRuntimePort: () => options.alreadyStopped || options.noRuntimeRecord ? null : endpoint,
109111
removePid() {},
110112
removeRuntimePort() {},

‎tests/update/update-teardown-handover.test.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,16 @@ describe("handover exit code and update decision", () => {
8080

8181
describe("stop handover predicate", () => {
8282
const facts = (nonce: string | undefined) => ({
83-
requested: true, restoreBlocked: false, nonce, stopFailed: false, recoveredCount: 0, proxy: "stopped",
83+
requested: true, restoreBlocked: false, nonce, stopFailed: false, recoveredCount: 0, proxy: "stopped", deferralHonored: true,
8484
});
8585

8686
test("hands over only this run's own receipt, alone, after a clean stop", () => {
8787
const own = claimPendingTeardown(ENDPOINT, "exact", process.pid, "update-restart").nonce;
8888
expect(stopHandsTeardownOver(facts(own))).toBe(true);
8989
expect(stopHandsTeardownOver({ ...facts(own), proxy: "stopped-orphan" })).toBe(true);
90+
// A signal fallback ran the proxy's own exit cleanup, which already restored the pristine catalog.
91+
expect(stopHandsTeardownOver({ ...facts(own), deferralHonored: false })).toBe(false);
92+
expect(stopHandsTeardownOver({ ...facts(own), proxy: "stopped-orphan", deferralHonored: false })).toBe(false);
9093
expect(stopHandsTeardownOver({ ...facts(own), requested: false })).toBe(false);
9194
expect(stopHandsTeardownOver({ ...facts(own), restoreBlocked: true })).toBe(false);
9295
expect(stopHandsTeardownOver({ ...facts(own), stopFailed: true })).toBe(false);
@@ -210,14 +213,14 @@ describe("ocx stop under an update handover", () => {
210213
});
211214

212215
test("hands over when the service manager stops the proxy itself", () => {
213-
const run = runStop(true, { managerCascades: true });
216+
const run = runStop(true, { managerCascades: true, deadPidReadsNull: true });
214217
expect(run.exitCode, run.stderr).toBe(STOP_TEARDOWN_HANDED_OVER_EXIT_CODE);
215218
expect(run.calls).toMatchObject({ native: 0, grok: 0, cleared: 0, service: 1 });
216219
expect(isUpdateHandoverReceipt(run.nonce ?? null)).toBe(true);
217220
});
218221

219222
test("a manager-stopped ordinary stop still restores", () => {
220-
const run = runStop(false, { managerCascades: true });
223+
const run = runStop(false, { managerCascades: true, deadPidReadsNull: true });
221224
expect(run.exitCode, run.stderr).toBe(0);
222225
expect(run.calls).toMatchObject({ native: 1, grok: 1 });
223226
expect(receiptNames()).toEqual([]);
@@ -229,6 +232,15 @@ describe("ocx stop under an update handover", () => {
229232
expect(run.calls).toMatchObject({ native: 1, grok: 1 });
230233
expect(receiptNames()).toEqual([]);
231234
});
235+
236+
test("a failed /api/stop that falls back to a signal restores instead of claiming a handover", () => {
237+
// Exact runtime endpoint, but the proxy answers 503 before honoring the deferral, so the
238+
// stop falls back to a signal and the proxy's own exit cleanup is what ran.
239+
const run = runStop(true, { apiFailure: true, status: 503, response: { error: "unavailable" }, nativeSignal: true });
240+
expect(run.exitCode, run.stderr).toBe(0);
241+
expect(run.calls).toMatchObject({ native: 1, grok: 1 });
242+
expect(receiptNames()).toEqual([]);
243+
});
232244
});
233245

234246
describe("startup publication", () => {

0 commit comments

Comments
 (0)