Skip to content

Commit 7fb8e2f

Browse files
committed
fix(update): require correlated identity after npm GUI restart
A no-op or failed explicit restart could leave the pre-update proxy healthy and still report success. Re-check PID/version evidence after npm restart and fail when the old process remains.
1 parent 5a29b47 commit 7fb8e2f

2 files changed

Lines changed: 160 additions & 4 deletions

File tree

‎src/update/job.ts‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,8 @@ export function npmSelfUpdateRestartEvidence(
582582
* only prints `ocx start` and never brings the proxy back, so waiting would always burn
583583
* the full health timeout. Skipping also requires update-correlated evidence (PID change
584584
* and/or target version) so a surviving pre-update process cannot look like success.
585+
* After an explicit npm restart the same evidence is required again — health alone is
586+
* not enough when a no-op restart or failed port reclaim leaves the old proxy up.
585587
*/
586588
export async function finishGuiUpdateRestart(
587589
job: UpdateJobState,
@@ -619,7 +621,59 @@ export async function finishGuiUpdateRestart(
619621
}
620622
const restartFn = io.restartAfterUpdateFn ?? restartAfterUpdate;
621623
await restartFn(job, captured, io);
622-
return confirmRestartedProxy(job, captured, io);
624+
if (installer !== "npm") {
625+
// Bun/source: health alone remains enough unless a richer identity probe is supplied.
626+
if (!io.probeProxyIdentity) return confirmRestartedProxy(job, captured, io);
627+
}
628+
return confirmNpmExplicitRestart(job, captured, io);
629+
}
630+
631+
/**
632+
* After an explicit npm (or identity-aware) restart, require update-correlated
633+
* evidence — not merely a healthy OpenCodex listener. A no-op restart or a
634+
* failed port reclaim can leave the pre-update process on the captured port;
635+
* `confirmRestartedProxy` alone would treat that as success.
636+
*/
637+
async function confirmNpmExplicitRestart(
638+
job: UpdateJobState,
639+
captured: { port: number; hostname: string; oldPid?: number },
640+
io: RestartIo = {},
641+
): Promise<boolean> {
642+
const healthy = await awaitRestartedProxyHealthy(job, captured, io);
643+
if (!healthy.ok) {
644+
const port = captured.port;
645+
const hostname = captured.hostname;
646+
const error = healthy.reason === "flapped"
647+
? `proxy restart became unhealthy on ${hostname}:${port}`
648+
: `proxy restart never became healthy on ${hostname}:${port}`;
649+
updateJob(job, {
650+
status: "failed",
651+
restarted: false,
652+
error,
653+
}, restartFailureHint(port));
654+
return false;
655+
}
656+
657+
const identity = await (io.probeProxyIdentity ?? defaultProbeProxyIdentity)(
658+
captured.port,
659+
captured.hostname,
660+
);
661+
const evidence = npmSelfUpdateRestartEvidence(job, captured, identity);
662+
if (!evidence.ok) {
663+
updateJob(job, {
664+
status: "failed",
665+
restarted: false,
666+
error: `proxy restart did not show update-correlated identity (${evidence.reason})`,
667+
}, restartFailureHint(captured.port));
668+
return false;
669+
}
670+
671+
updateJob(
672+
job,
673+
{},
674+
`Proxy restart confirmed on ${captured.hostname}:${captured.port} (${evidence.detail}).`,
675+
);
676+
return true;
623677
}
624678

625679
export async function runGuiUpdateWorker(jobId: string, channel: Channel, restart: boolean): Promise<void> {

‎tests/update-job.test.ts‎

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ describe("GUI update execution decisions", () => {
407407
)).toBe(true);
408408
});
409409

410-
test("npm finish does not skip when a healthy proxy is still the pre-update PID", async () => {
410+
test("npm finish fails when stale PID survives a no-op explicit restart", async () => {
411411
let now = 0;
412412
let restartCalls = 0;
413413
const job: UpdateJobState = {
@@ -431,7 +431,7 @@ describe("GUI update execution decisions", () => {
431431
"npm",
432432
{
433433
serviceInstalledFn: () => true,
434-
// Soft probe stays healthy (old process). Explicit restart then confirms.
434+
// Soft probe stays healthy (old process). Explicit restart is a no-op.
435435
probeProxy: async () => true,
436436
probeProxyIdentity: async () => ({ pid: 111, version: "2.7.40" }),
437437
now: () => now,
@@ -442,13 +442,109 @@ describe("GUI update execution decisions", () => {
442442
},
443443
},
444444
);
445-
expect(ok).toBe(true);
445+
expect(ok).toBe(false);
446446
expect(restartCalls).toBe(1);
447+
expect(readUpdateJob(job.id)).toMatchObject({
448+
status: "failed",
449+
restarted: false,
450+
});
451+
expect(readUpdateJob(job.id)?.error).toContain("still the pre-update PID");
447452
expect(readUpdateJob(job.id)?.log.some(line =>
448453
line.includes("still the pre-update PID") && line.includes("performing explicit restart"),
449454
)).toBe(true);
450455
});
451456

457+
test("npm finish succeeds when explicit restart yields a new PID at the target version", async () => {
458+
let now = 0;
459+
let restartCalls = 0;
460+
let livePid = 111;
461+
let liveVersion = "2.7.40";
462+
const job: UpdateJobState = {
463+
id: "npm-explicit-replaced",
464+
status: "restarting",
465+
startedAt: new Date().toISOString(),
466+
updatedAt: new Date().toISOString(),
467+
currentVersion: "2.7.40",
468+
latestVersion: "2.7.41",
469+
channel: "latest",
470+
installer: "npm",
471+
restart: true,
472+
command: "",
473+
releaseNotesUrl: "",
474+
log: [],
475+
};
476+
writeFileSync(updateJobPath(job.id), JSON.stringify(job));
477+
const ok = await finishGuiUpdateRestart(
478+
job,
479+
{ port: 10100, hostname: "127.0.0.1", oldPid: 111 },
480+
"npm",
481+
{
482+
serviceInstalledFn: () => true,
483+
probeProxy: async () => true,
484+
probeProxyIdentity: async () => ({ pid: livePid, version: liveVersion }),
485+
now: () => now,
486+
sleepMs: async (ms) => { now += ms; },
487+
restartAfterUpdateFn: async () => {
488+
restartCalls += 1;
489+
livePid = 222;
490+
liveVersion = "2.7.41";
491+
now = 0;
492+
},
493+
},
494+
);
495+
expect(ok).toBe(true);
496+
expect(restartCalls).toBe(1);
497+
expect(readUpdateJob(job.id)?.status).not.toBe("failed");
498+
expect(readUpdateJob(job.id)?.log.some(line =>
499+
line.includes("Proxy restart confirmed") && line.includes("pid changed"),
500+
)).toBe(true);
501+
});
502+
503+
test("npm finish fails when port reclaim leaves the pre-update proxy healthy", async () => {
504+
let now = 0;
505+
const job: UpdateJobState = {
506+
id: "npm-reclaim-stale",
507+
status: "restarting",
508+
startedAt: new Date().toISOString(),
509+
updatedAt: new Date().toISOString(),
510+
currentVersion: "2.7.40",
511+
latestVersion: "2.7.41",
512+
channel: "latest",
513+
installer: "npm",
514+
restart: true,
515+
command: "",
516+
releaseNotesUrl: "",
517+
log: [],
518+
};
519+
writeFileSync(updateJobPath(job.id), JSON.stringify(job));
520+
const ok = await finishGuiUpdateRestart(
521+
job,
522+
{ port: 10100, hostname: "127.0.0.1", oldPid: 111 },
523+
"npm",
524+
{
525+
// Direct path: reclaim failure returns without spawning a replacement.
526+
serviceInstalledFn: () => false,
527+
waitForPort: async () => false,
528+
spawnStart: () => {
529+
throw new Error("must not spawn when reclaim failed");
530+
},
531+
probeProxy: async () => true,
532+
probeProxyIdentity: async () => ({ pid: 111, version: "2.7.40" }),
533+
now: () => now,
534+
sleepMs: async (ms) => { now += ms; },
535+
},
536+
);
537+
expect(ok).toBe(false);
538+
expect(readUpdateJob(job.id)).toMatchObject({
539+
status: "failed",
540+
restarted: false,
541+
});
542+
expect(readUpdateJob(job.id)?.error).toContain("still the pre-update PID");
543+
expect(readUpdateJob(job.id)?.log.some(line =>
544+
line.includes("still busy") && line.includes("not starting on another port"),
545+
)).toBe(true);
546+
});
547+
452548
test("npm finish skips the soft probe for direct installs and restarts immediately", async () => {
453549
let now = 0;
454550
let restartCalls = 0;
@@ -474,6 +570,9 @@ describe("GUI update execution decisions", () => {
474570
// Only becomes healthy after the explicit restart (launcher printed `ocx start` only).
475571
return restartCalls > 0;
476572
},
573+
probeProxyIdentity: async () => (
574+
restartCalls > 0 ? { pid: 333, version: "2.7.41" } : null
575+
),
477576
now: () => now,
478577
sleepMs: async (ms) => { now += ms; },
479578
restartAfterUpdateFn: async () => {
@@ -512,6 +611,9 @@ describe("GUI update execution decisions", () => {
512611
serviceInstalledFn: () => true,
513612
// Soft probe times out (proxy down after npm update); confirm after explicit restart succeeds.
514613
probeProxy: async () => restartCalls > 0,
614+
probeProxyIdentity: async () => (
615+
restartCalls > 0 ? { pid: 444, version: "2.7.41" } : null
616+
),
515617
now: () => now,
516618
sleepMs: async (ms) => { now += ms; },
517619
restartAfterUpdateFn: async () => {

0 commit comments

Comments
 (0)