Skip to content

Commit 075eb57

Browse files
committed
fix(ssh): the launcher no longer deletes or misdates a desktop update marker
The marker check behind the SSH launch script deleted expired markers. If a new update replaced the marker between that read and the delete, the launcher removed the new update's marker and could then start a competing server. The check is now read-only; the desktop already clears a leftover marker once its backend publishes its runtime. The archive helper also read the clock before reading the marker, so a marker published mid-read looked future-dated and was ignored. It now reads the clock afterwards, as the node fallback already did.
1 parent b8e9880 commit 075eb57

4 files changed

Lines changed: 23 additions & 12 deletions

File tree

‎apps/server/src/desktopUpdate/DesktopUpdateMarker.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,18 @@ describe("DesktopUpdateMarker", () => {
3838
}),
3939
});
4040
assert.deepStrictEqual(remaining, expected);
41-
assert.equal(removed, ["expired", "reversed", "expired missing start"].includes(name));
41+
assert.isFalse(removed, `${name}: the launcher must never delete the shared marker`);
42+
}),
43+
);
44+
45+
it.effect("honours a marker published while it is being read", () =>
46+
Effect.gen(function* () {
47+
yield* TestClock.setTime(0);
48+
const remaining = yield* remainingWait({
49+
readFileString: () =>
50+
TestClock.adjust(1000).pipe(Effect.as('{"startedAt":1000,"expiresAt":61000}')),
51+
});
52+
assert.deepStrictEqual(remaining, Option.some(60000));
4253
}),
4354
);
4455

‎apps/server/src/desktopUpdate/DesktopUpdateMarker.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const UpdateMarker = Schema.fromJsonString(
1414
export class DesktopUpdateMarker extends Context.Service<
1515
DesktopUpdateMarker,
1616
{
17-
/** Remaining SSH launch wait in ms, capped at this build's TTL; removes expired markers. */
17+
/**
18+
* Remaining SSH launch wait in ms, capped at this build's TTL.
19+
* Read-only: a newer update may have just replaced the marker.
20+
*/
1821
readonly remainingWait: (markerFile: string) => Effect.Effect<Option.Option<number>>;
1922
}
2023
>()("t3/desktopUpdate/DesktopUpdateMarker") {}
@@ -25,15 +28,14 @@ const make = Effect.gen(function* () {
2528
const remainingWait: DesktopUpdateMarker["Service"]["remainingWait"] = Effect.fn(
2629
"DesktopUpdateMarker.remainingWait",
2730
)(function* (markerFile) {
28-
const now = yield* Clock.currentTimeMillis;
2931
const marker = yield* fileSystem
3032
.readFileString(markerFile)
3133
.pipe(Effect.flatMap(Schema.decodeUnknownEffect(UpdateMarker)), Effect.option);
34+
// Sampled after the read so a marker published mid-read is not seen as future-dated.
35+
const now = yield* Clock.currentTimeMillis;
3236
if (Option.isSome(marker)) {
3337
const { startedAt, expiresAt } = marker.value;
34-
if (Number.isFinite(expiresAt) && expiresAt <= now) {
35-
yield* fileSystem.remove(markerFile, { force: true }).pipe(Effect.ignore);
36-
} else if (
38+
if (
3739
typeof startedAt === "number" &&
3840
Number.isFinite(startedAt) &&
3941
Number.isFinite(expiresAt) &&

‎packages/ssh/src/tunnel.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ describe("SSH launch during desktop update", () => {
512512
assert.deepEqual(result.output, { remotePort: 5999, serverKind: "managed" });
513513
assert.isAtLeast(result.now, 60_000);
514514
assert.isAtMost(result.now, 61_000);
515-
assert.isFalse(result.markerExists, "an expired marker must be removed");
515+
assert.isTrue(result.markerExists, "an expired marker is ignored, not deleted");
516516
assert.equal(result.events.filter((event) => event.startsWith("start ")).length, 1);
517517
}),
518518
);
@@ -525,18 +525,18 @@ describe("SSH launch during desktop update", () => {
525525
assert.deepEqual(result.output, { remotePort: 5999, serverKind: "managed" });
526526
assert.isAtLeast(result.now, 1_000);
527527
assert.isAtMost(result.now, 2_000);
528-
assert.isFalse(result.markerExists);
528+
assert.isTrue(result.markerExists);
529529
}),
530530
);
531531

532-
it.effect("ignores and removes a stale marker without waiting", () =>
532+
it.effect("ignores a stale marker without waiting or deleting it", () =>
533533
Effect.gen(function* () {
534534
const result = runLauncher(yield* captureLauncher, {
535535
marker: { startedAt: -60_000, expiresAt: 0 },
536536
});
537537
assert.deepEqual(result.output, { remotePort: 5999, serverKind: "managed" });
538538
assert.equal(result.now, 0);
539-
assert.isFalse(result.markerExists);
539+
assert.isTrue(result.markerExists);
540540
}),
541541
);
542542

‎packages/ssh/src/tunnel.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,6 @@ try {
647647
if (Number.isFinite(startedAt) && Number.isFinite(expiresAt) &&
648648
startedAt <= now && expiresAt > now && expiresAt > startedAt) {
649649
process.stdout.write(String(Math.min(expiresAt - now, @@T3_UPDATE_TTL_MS@@)));
650-
} else if (Number.isFinite(expiresAt) && expiresAt <= now) {
651-
fs.rmSync(path, { force: true });
652650
}
653651
} catch {}
654652
NODE

0 commit comments

Comments
 (0)