Skip to content

Commit 274980e

Browse files
fix(server): adopt legacy service state without rewriting it
Rewriting the state captured before runtime validation could replace a concurrent install's newer state and launch the older runtime. AI-assisted; verified with the tests and tools listed above.
1 parent 7611438 commit 274980e

2 files changed

Lines changed: 7 additions & 22 deletions

File tree

‎apps/server/src/serviceLauncher.test.ts‎

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
compareExactServiceVersions,
2727
decodeServiceState,
2828
isExactServiceVersion,
29-
parseServiceState,
3029
SERVICE_ENV_FILE,
3130
SERVICE_LAUNCHER_PROTOCOL,
3231
SERVICE_RESTART_PENDING_FILE,
@@ -187,7 +186,7 @@ it.layer(NodeServices.layer)("service state persistence", (it) => {
187186
}),
188187
);
189188

190-
it.effect("migrates a legacy install atomically before the launcher spawns its runtime", () =>
189+
it.effect("adopts a legacy install record without rewriting it", () =>
191190
Effect.gen(function* () {
192191
const fs = yield* FileSystem.FileSystem;
193192
const path = yield* Path.Path;
@@ -200,34 +199,20 @@ it.layer(NodeServices.layer)("service state persistence", (it) => {
200199
path,
201200
versionDir,
202201
`
203-
import { readFileSync, writeFileSync } from "node:fs";
204-
writeFileSync(new URL("./started", import.meta.url), readFileSync(new URL("../../service-state.json", import.meta.url)));
202+
import { writeFileSync } from "node:fs";
203+
writeFileSync(new URL("./started", import.meta.url), "");
205204
process.exit(0);
206205
`,
207206
);
208207
const legacyContents = `{ "protocol": 2, "activeVersion": "${activeVersion}" }`;
209208
yield* fs.writeFileString(statePath, legacyContents);
210-
const previousDocument = path.join(root, "previous-state.json");
211-
yield* fs.link(statePath, previousDocument);
212209

213-
assert.isUndefined(decodeServiceState({ protocol: 2, activeVersion }));
214-
assert.isUndefined(parseServiceState(legacyContents));
215-
yield* Effect.promise(() =>
216-
expect(readServiceState(statePath)).rejects.toThrow(
217-
"Service state is invalid or unsupported.",
218-
),
219-
);
220210
yield* Effect.promise(() =>
221211
expect(runLauncher(root)).rejects.toThrow("Active child exited unexpectedly (0)."),
222212
);
223213

224-
const migrated = { protocol: SERVICE_LAUNCHER_PROTOCOL, activeVersion };
225-
assert.deepEqual(yield* Effect.promise(() => readServiceState(statePath)), migrated);
226-
assert.deepEqual(
227-
yield* Effect.promise(() => readServiceState(path.join(versionDir, "started"))),
228-
migrated,
229-
);
230-
assert.equal(yield* fs.readFileString(previousDocument), legacyContents);
214+
assert.isTrue(yield* fs.exists(path.join(versionDir, "started")));
215+
assert.equal(yield* fs.readFileString(statePath), legacyContents);
231216
}),
232217
);
233218

‎apps/server/src/serviceLauncher.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,9 +939,9 @@ export async function main(): Promise<void> {
939939
throw new Error(`Selected t3@${legacy.activeVersion} runtime is missing or incomplete.`);
940940
}
941941
// An older coordinator can write protocol 2 beside a protocol-3 runtime.
942-
// Adopt only its install record; update state needs its original launcher.
942+
// Adopt only its install record, in memory: rewriting it here could replace
943+
// state a concurrent install wrote after this read.
943944
state = { protocol: 3, activeVersion: legacy.activeVersion };
944-
await writeServiceState(statePath, state);
945945
}
946946
await new Launcher(baseDir, state, { environment }).run();
947947
}

0 commit comments

Comments
 (0)