diff --git a/CHANGELOG.md b/CHANGELOG.md index 00ef9ec4d..fb7606e77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ All notable changes to **omp-web** (`@kahme247/ompweb`) are documented in this f - **In-app updates can replace an npm-global Windows install started by the tray.** The update stops the tray program in the package and any node or bun process still using that install, without ending the update itself, and runs the install from outside the package so Windows can rename it. When the tray started the server, it is started again in the logged-on session and starts the one server; otherwise the server is started directly and still told which install to serve. - **Windows updates no longer fail when the update status is read while it is written.** Host update state, the server worker's status and upgrade plans retry their atomic replacement for a moment when a concurrent reader holds the file, as the server's own update state already did. +- **A host update's failed candidate is stopped on Windows even when it got the old daemon's pid.** Windows can reuse a freed pid, so the restarted candidate could receive the pid the update had inspected; the stop then refused the plan as inconsistent ("Invalid upgrade process plan: current instance does not match the inspected process"), left the candidate running and required recovery instead of restoring the previous release. A stop checks the instance it acts on against the live process, so a recycled pid is still refused. - **GitHub workflow watches no longer regress to stale run states.** Workflow updates retain GitHub's run timestamp and ignore older snapshots for the same or an earlier attempt, so a delayed `queued` response cannot follow a newer `in_progress` delivery. - **Keep sidebar transcript results aligned with expandable hits on every linked host.** Each daemon now searches its own active conversations, ignoring abandoned branches and pre-compaction messages. diff --git a/lib/hosts/update-worker.test.mjs b/lib/hosts/update-worker.test.mjs index ec7754668..ddda0a03d 100644 --- a/lib/hosts/update-worker.test.mjs +++ b/lib/hosts/update-worker.test.mjs @@ -101,7 +101,8 @@ async function fixture(id, { candidate = "ack", installKind = "ack", method = "n writeFileSync(starterScript, `require("node:fs").writeFileSync(${JSON.stringify(ready)},String(process.pid));\n` + (shutdownDelay ? `process.on("SIGTERM",()=>setTimeout(()=>process.exit(0),${shutdownDelay}));\n` : "") + (sessions ? sessionDaemonScript(join(install, "bin", "idle-web.js"), sessionPids, sessions === "orphan") : `require(${JSON.stringify(host)});`)); starter = await spawnNodeOutsideService(starterScript, launch.env, [host, ...layout.hostArgs]); assert.ok(starter, "the fixture requires a scope outside the suite's service"); - for (let tries = 0; !existsSync(ready) || (sessions && !existsSync(sessionPids)); tries++) { + // The starter writes its pid with writeFileSync, which can be seen empty before the write lands. + for (let tries = 0; !existsSync(ready) || !readFileSync(ready, "utf8").trim() || (sessions && !existsSync(sessionPids)); tries++) { assert.ok(tries < 100, "the scoped daemon never became ready"); await wait(100); } diff --git a/lib/upgrade-process.test.mjs b/lib/upgrade-process.test.mjs index 8d059f765..5215e1a9b 100644 --- a/lib/upgrade-process.test.mjs +++ b/lib/upgrade-process.test.mjs @@ -238,10 +238,20 @@ test("inspection refuses the wrong role, installation or process, and stop refus await assert.rejects(upgrade.stopUpgradeProcess({ ...plan, platform: "plan9" }), /Invalid upgrade process plan/); await assert.rejects(upgrade.stopUpgradeProcess({ ...plan, supervisor: { kind: "systemd", unit: "../evil", user: false, configHash: "x" } }), /Invalid upgrade process plan/); await assert.rejects(upgrade.stopUpgradeProcess({ ...plan, supervisor: { kind: "launchd", label: "dev.ompweb.test", domain: "pid/1", plist: join(fixture.dir, "test.plist"), configHash: "x" } }), /Invalid upgrade process plan/); - await assert.rejects(upgrade.stopUpgradeProcess({ ...plan, identity: `${plan.identity}-other` }), /Invalid upgrade process plan/); assert.ok(running(fixture.pid), "none of the refused plans touched the process"); }); +test("an instance that took the inspected pid over after a restart is stopped, and a recycled pid is still refused", { timeout: 120_000 }, async () => { + const fixture = await orphanHost("restarted onto the inspected pid"); + const inspected = await inspect(fixture); + // Windows can reuse a freed pid, so a restarted instance may hold the inspected pid with a later start time; simulated here by changing the recorded identities. + const reused = { ...inspected, identity: `${inspected.identity}-previous-instance` }; + await assert.rejects(upgrade.stopUpgradeProcess({ ...reused, current: { pid: fixture.pid, identity: `${inspected.identity}-recycled` } }), /no longer the process that was inspected/); + assert.ok(running(fixture.pid), "an instance that is not the recorded one is never signalled"); + await upgrade.stopUpgradeProcess(reused); + assert.ok(!running(fixture.pid), "the recorded instance was stopped"); +}); + test("a shell restart loop is frozen while its daemon is replaced, then resumed to start the daemon again", { skip: windows && "POSIX shell loops only; Windows supervisors are tasks and services", timeout: 120_000 }, async () => { const { dir, host, pidFile } = install("loop with spaces"); const script = join(dir, "loop.sh"); diff --git a/lib/upgrade-process.ts b/lib/upgrade-process.ts index 4fc74c1c8..43f5ec82b 100644 --- a/lib/upgrade-process.ts +++ b/lib/upgrade-process.ts @@ -663,7 +663,7 @@ function assertPlan(plan: UpgradeProcess): void { if (typeof plan.installDir !== "string" || !isAbsolute(plan.installDir)) bad("installDir"); const current = plan.current; if (!current || typeof current !== "object" || (current.pid === null) !== (current.identity === null) || (current.pid !== null && (!Number.isSafeInteger(current.pid) || current.pid <= 1 || typeof current.identity !== "string"))) bad("current instance"); - if (current.pid === plan.pid && current.identity !== plan.identity) bad("current instance does not match the inspected process"); + // `current` follows restarts and is checked against the live process when a stop acts on it; Windows may give the restarted instance the inspected pid, so the pid alone says nothing about identity. const s = plan.supervisor; const text = (value: unknown) => typeof value === "string" && value.length > 0 && value.length <= 4096 && !/[\0-\x1f]/.test(value); if (!s || typeof s !== "object") bad("supervisor");