fix(server): a remote update points the service unit at the runtime it committed #19652
MergedMarve10s(GitHub profile, opens in a new tab)merged 0 commits into
main fromrefs/pull/19652/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/serviceLauncher.ts line 852OutdatedResolvedby Marve10s(GitHub profile, opens in a new tab)@@ -769,3 +849,4 @@769849 child.role = "active";770850 await discardDatabaseBackup(this.#baseDir, committed.id).catch(() => undefined);771851 await sendMessage(child.process, { type: "committed", updateId: committed.id });852+ await this.#repointServiceUnit(committed.targetVersion);
- apps/server/src/serviceLauncher.ts lines 851 to 852OutdatedResolvedby Marve10s(GitHub profile, opens in a new tab)@@ -769,3 +849,4 @@769849 child.role = "active";770850 await discardDatabaseBackup(this.#baseDir, committed.id).catch(() => undefined);771851 await sendMessage(child.process, { type: "committed", updateId: committed.id });852+ await this.#repointServiceUnit(committed.targetVersion);@@ -769,3 +849,16 @@769849 child.role = "active";770850 await discardDatabaseBackup(this.#baseDir, committed.id).catch(() => undefined);771851 await sendMessage(child.process, { type: "committed", updateId: committed.id });852+ await this.#repointServiceUnit(committed.targetVersion);853+ }854+855+ // The commit is durable before this runs: a unit that could not be rewritten856+ // leaves the service on the new version, pointed at the old runtime, as before.857+ async #repointServiceUnit(version: string): Promise<void> {858+ const unit = this.#serviceUnit;859+ if (unit === undefined) return;860+ try {861+ const contents = await NodeFSP.readFile(unit.path, "utf8");862+ const repointed = repointServiceUnit(contents, version);863+ if (repointed === contents) return;864+ await replaceFileDurably(unit.path, repointed);
- apps/server/src/serviceLauncher.ts lines 824 to 827OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -730,4 +808,20 @@730808 this.#state = next;731809 child.role = "active";732810 await discardDatabaseBackup(this.#baseDir, committed.id).catch(() => undefined);733- await sendMessage(child.process, { type: "committed", updateId: committed.id });811+ try {812+ await sendMessage(child.process, { type: "committed", updateId: committed.id });813+ } finally {814+ await this.#repointServiceUnit(committed.targetVersion);815+ }816+ }817+818+ // The commit is durable before this runs: a unit that could not be rewritten819+ // leaves the service on the new version, pointed at the old runtime, as before.820+ async #repointServiceUnit(version: string): Promise<void> {821+ const unit = this.#serviceUnit;822+ if (unit === undefined) return;823+ try {824+ const contents = await NodeFSP.readFile(unit.path, "utf8");825+ const repointed = repointServiceUnit(contents, version);826+ if (repointed === contents) return;827+ await replaceFileDurably(unit.path, repointed);
- @@ -730,4 +808,7 @@730808 this.#state = next;731809 child.role = "active";732810 await discardDatabaseBackup(this.#baseDir, committed.id).catch(() => undefined);733- await sendMessage(child.process, { type: "committed", updateId: committed.id });811+ try {812+ await sendMessage(child.process, { type: "committed", updateId: committed.id });813+ } finally {814+ await this.#repointServiceUnit(committed.targetVersion);
- @@ -730,4 +811,7 @@730811 this.#state = next;731812 child.role = "active";732813 await discardDatabaseBackup(this.#baseDir, committed.id).catch(() => undefined);733- await sendMessage(child.process, { type: "committed", updateId: committed.id });814+ try {815+ await sendMessage(child.process, { type: "committed", updateId: committed.id });816+ } finally {817+ await this.#repointServiceUnit(committed.targetVersion);apps/server/src/serviceLauncher.ts lines 621 to 623OutdatedResolvedby Marve10s(GitHub profile, opens in a new tab)@@ -540,3 +618,6 @@540618 if (update !== undefined) {541619 await discardDatabaseBackup(this.#baseDir, update.id).catch(() => undefined);542620 }621+ if (update?.status === "committed") {622+ await this.#repointServiceUnit(this.#state.activeVersion);623+ }
juliusmarminge assignedjuliusmarminge
- apps/server/src/cloud/serviceSelectionLock.ts lines 46 to 56OutdatedResolvedby Marve10s(GitHub profile, opens in a new tab)@@ -0,0 +36,21 @@36+ if (!["EEXIST", "ENOTEMPTY"].includes((error as NodeJS.ErrnoException).code ?? ""))37+ throw error;38+ }39+ let owners: string[];40+ try {41+ owners = NodeFS.readdirSync(lock);42+ } catch (error) {43+ if ((error as NodeJS.ErrnoException).code === "ENOENT") continue;44+ throw error;45+ }46+ if (owners.length === 1) {47+ const previous = owners[0]!;48+ const pid = Number(previous.split(".", 1)[0]);49+ if (!Number.isSafeInteger(pid) || pid <= 0)50+ throw new Error(`Invalid service selection lock owner at ${lock}.`);51+ try {52+ process.kill(pid, 0);53+ } catch (error) {54+ const code = (error as NodeJS.ErrnoException).code;55+ if (code !== "ESRCH" && code !== "EPERM") throw error;56+ if (code === "ESRCH") {
- @@ -66,0 +77,21 @@77+ readonly path: string;78+ /** systemd keeps a loaded copy of the unit; launchd re-reads the plist at bootstrap. */79+ readonly reload: () => Promise<void>;80+}81+82+function serviceUnitFromEnvironment(env: NodeJS.ProcessEnv = process.env): ServiceUnit | undefined {83+ const unitName = env[SERVICE_UNIT_ENV]?.trim();84+ const home = env.HOME?.trim();85+ if (!unitName || !home) return undefined;86+ if (unitName.endsWith(".plist")) {87+ return {88+ path: NodePath.join(home, "Library", "LaunchAgents", unitName),89+ reload: () => Promise.resolve(),90+ };91+ }92+ // The manager's own environment, which decides where it reads user units.93+ const xdgConfigHome = env.XDG_CONFIG_HOME?.trim();94+ const configHome =95+ xdgConfigHome && NodePath.isAbsolute(xdgConfigHome)96+ ? xdgConfigHome97+ : NodePath.join(home, ".config");@@ -0,0 +40,21 @@40+ if (!["EEXIST", "ENOTEMPTY"].includes((error as NodeJS.ErrnoException).code ?? ""))41+ throw error;42+ }43+ let owners: string[];44+ try {45+ owners = NodeFS.readdirSync(lock);46+ } catch (error) {47+ if ((error as NodeJS.ErrnoException).code === "ENOENT") continue;48+ throw error;49+ }50+ if (owners.length === 1) {51+ const previous = owners[0]!;52+ const [pidText, bootText] = previous.split(".");53+ const pid = Number(pidText);54+ if (!Number.isSafeInteger(pid) || pid <= 0)55+ throw new Error(`Invalid service selection lock owner at ${lock}.`);56+ const ownerBoot = Number(bootText);57+ const fromEarlierBoot =58+ Number.isSafeInteger(ownerBoot) && Math.abs(ownerBoot - bootEpochSeconds()) > 60;59+ let dead = fromEarlierBoot;60+ if (!dead) {
juliusmarminge merged thisd0d0608
juliusmarminge closed this
Sign in to join this conversation.


Codex Review Summary: ✅ Completed
This comment shows the latest Codex review activity on this pull request.
d43d893ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.