Skip to content

Commit 846de87

Browse files
only21milclaude
andauthored
fix(server): t3 update no longer deletes the runtime its launcher runs (#24289)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 5ffcb86 commit 846de87

4 files changed

Lines changed: 266 additions & 11 deletions

File tree

‎apps/server/src/cli/update.test.ts‎

Lines changed: 174 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { assert, it } from "@effect/vitest";
33
import * as HostProcess from "@t3tools/shared/HostProcess";
4+
import * as Duration from "effect/Duration";
45
import * as Effect from "effect/Effect";
6+
import * as Exit from "effect/Exit";
57
import * as FileSystem from "effect/FileSystem";
68
import * as Option from "effect/Option";
79
import * as Path from "effect/Path";
10+
import * as TestClock from "effect/testing/TestClock";
811
import { HttpClient, HttpClientResponse } from "effect/http";
12+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
913
import { afterEach, vi } from "vite-plus/test";
1014

15+
import { prunePinnedRuntimes } from "../cloud/pinnedRuntime.ts";
16+
import * as ProcessRunner from "../processRunner.ts";
1117
import { repointLauncher, resolveLauncherPath, resolveNewestVersion } from "./update.ts";
1218

19+
// lsof listing one process, so prune sees a process list in which no runtime is running.
20+
const unrelatedLsof = ProcessRunner.ProcessRunner.of({
21+
run: () =>
22+
Effect.succeed({
23+
stdout: "p1\nftxt\nn/usr/bin/true\n",
24+
stderr: "",
25+
code: ChildProcessSpawner.ExitCode(0),
26+
timedOut: false,
27+
stdoutTruncated: false,
28+
stderrTruncated: false,
29+
stdoutInvalidUtf8: false,
30+
stderrInvalidUtf8: false,
31+
}),
32+
});
33+
1334
afterEach(() => vi.restoreAllMocks());
1435

1536
it.effect("authenticates each release page until the requested channel is found", () =>
@@ -53,7 +74,7 @@ it.layer(NodeServices.layer)("t3 update launcher", (it) => {
5374

5475
const repointed = yield* repointLauncher({
5576
launchedAs: launcher,
56-
versionsDir: path.join(root, "runtime/versions"),
77+
baseDir: root,
5778
targetEntryPath: newExe,
5879
});
5980

@@ -62,6 +83,157 @@ it.layer(NodeServices.layer)("t3 update launcher", (it) => {
6283
}).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
6384
);
6485

86+
it.effect.each([
87+
{ name: "no launcher records", recorded: undefined },
88+
{ name: "an empty launcher record", recorded: "" },
89+
{ name: "whole launcher records", recorded: "/opt/t3\n" },
90+
{ name: "Windows launcher records", recorded: "C:\\t3.cmd\r\n" },
91+
// An interrupted append cut this record short.
92+
{ name: "an unterminated launcher record", recorded: "/opt/t" },
93+
// Another process's append is cut short after t3 update reads the records.
94+
{ name: "a record cut short after the read", recorded: "/opt/t3\n", cut: "/opt/t" },
95+
])("keeps the runtime a repointed launcher off PATH points at with $name", ({ recorded, cut }) =>
96+
Effect.gen(function* () {
97+
const fs = yield* FileSystem.FileSystem;
98+
const path = yield* Path.Path;
99+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
100+
yield* TestClock.setTime(Duration.toMillis(Duration.days(1)));
101+
for (const version of ["1.0.0", "2.0.0"]) {
102+
const directory = path.join(root, "runtime/versions", version);
103+
yield* fs.makeDirectory(directory, { recursive: true });
104+
yield* fs.writeFileString(path.join(directory, "t3"), "");
105+
yield* fs.writeFileString(path.join(directory, ".install-complete"), `${version}\n`);
106+
yield* fs.utimes(path.join(directory, ".install-complete"), 0, 0);
107+
}
108+
if (recorded !== undefined) {
109+
yield* fs.writeFileString(path.join(root, "runtime/launchers"), recorded);
110+
}
111+
// A launcher in the T3 home itself, which neither PATH nor the default bin dir reaches.
112+
const launcher = path.join(root, "t3");
113+
yield* fs.symlink(path.join(root, "runtime/versions/1.0.0/t3"), launcher);
114+
115+
const launchersFile = path.join(root, "runtime/launchers");
116+
yield* repointLauncher({
117+
launchedAs: launcher,
118+
baseDir: root,
119+
targetEntryPath: path.join(root, "runtime/versions/2.0.0/t3"),
120+
}).pipe(
121+
Effect.provideService(FileSystem.FileSystem, {
122+
...fs,
123+
readFileString: (file, encoding) =>
124+
fs
125+
.readFileString(file, encoding)
126+
.pipe(
127+
Effect.tap(() =>
128+
cut === undefined || file !== launchersFile
129+
? Effect.void
130+
: fs.writeFileString(file, cut, { flag: "a" }),
131+
),
132+
),
133+
}),
134+
);
135+
// A later server start prunes with no idea how this machine launches t3.
136+
const removed = yield* prunePinnedRuntimes({ baseDir: root, keep: [] }).pipe(
137+
Effect.provideService(ProcessRunner.ProcessRunner, unrelatedLsof),
138+
Effect.provideService(HostProcess.Platform, "darwin"),
139+
Effect.provideService(HostProcess.Environment, { PATH: "" }),
140+
Effect.provideService(HostProcess.HomeDirectory, root),
141+
);
142+
143+
assert.deepStrictEqual(removed, ["1.0.0"]);
144+
// macOS temp dirs sit behind the /var -> /private/var symlink, so compare canonical paths.
145+
assert.equal(
146+
yield* fs.realPath(launcher),
147+
yield* fs.realPath(path.join(root, "runtime/versions/2.0.0/t3")),
148+
);
149+
}).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
150+
);
151+
152+
const launcherFixture = Effect.fnUntraced(function* () {
153+
const fs = yield* FileSystem.FileSystem;
154+
const path = yield* Path.Path;
155+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
156+
const oldExe = path.join(root, "runtime/versions/1.0.0/t3");
157+
const newExe = path.join(root, "runtime/versions/2.0.0/t3");
158+
const launcher = path.join(root, "t3");
159+
for (const file of [oldExe, newExe]) {
160+
yield* fs.makeDirectory(path.dirname(file), { recursive: true });
161+
yield* fs.writeFileString(file, "");
162+
}
163+
yield* fs.symlink(oldExe, launcher);
164+
return { root, oldExe, newExe, launcher, launchersFile: path.join(root, "runtime/launchers") };
165+
});
166+
167+
it.effect("records a relative launcher path as absolute", () =>
168+
Effect.gen(function* () {
169+
const fs = yield* FileSystem.FileSystem;
170+
const path = yield* Path.Path;
171+
const { root, newExe, launcher, launchersFile } = yield* launcherFixture();
172+
173+
yield* repointLauncher({
174+
launchedAs: path.relative(path.resolve("."), launcher),
175+
baseDir: root,
176+
targetEntryPath: newExe,
177+
});
178+
179+
assert.equal(yield* fs.readFileString(launchersFile), `\n${launcher}\n`);
180+
}).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
181+
);
182+
183+
it.effect("fails instead of repointing a launcher it cannot record", () =>
184+
Effect.gen(function* () {
185+
const fs = yield* FileSystem.FileSystem;
186+
const { root, oldExe, newExe, launcher, launchersFile } = yield* launcherFixture();
187+
// Appending to a directory fails the way a read-only or full disk would.
188+
yield* fs.makeDirectory(launchersFile);
189+
190+
const result = yield* repointLauncher({
191+
launchedAs: launcher,
192+
baseDir: root,
193+
targetEntryPath: newExe,
194+
}).pipe(Effect.exit);
195+
196+
assert.isTrue(Exit.isFailure(result));
197+
assert.equal(yield* fs.readLink(launcher), oldExe);
198+
}).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
199+
);
200+
201+
it.effect.each(["recording", "repointing"] as const)(
202+
"never leaves a repointed launcher unrecorded when t3 update dies while %s",
203+
(step) =>
204+
Effect.gen(function* () {
205+
const fs = yield* FileSystem.FileSystem;
206+
const { root, newExe, launcher, launchersFile } = yield* launcherFixture();
207+
208+
yield* repointLauncher({
209+
launchedAs: launcher,
210+
baseDir: root,
211+
targetEntryPath: newExe,
212+
}).pipe(
213+
Effect.provideService(FileSystem.FileSystem, {
214+
...fs,
215+
writeFileString: (path, data, options) =>
216+
step === "recording" && path === launchersFile
217+
? Effect.die("t3 update was killed")
218+
: fs.writeFileString(path, data, options),
219+
rename: (from, to) =>
220+
step === "repointing" && to === launcher
221+
? Effect.die("t3 update was killed")
222+
: fs.rename(from, to),
223+
}),
224+
Effect.exit,
225+
);
226+
227+
const repointed = (yield* fs.readLink(launcher)) === newExe;
228+
const recorded = (yield* fs
229+
.readFileString(launchersFile)
230+
.pipe(Effect.orElseSucceed(() => "")))
231+
.split("\n")
232+
.includes(launcher);
233+
assert.isFalse(repointed && !recorded);
234+
}).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
235+
);
236+
65237
it.effect("leaves a plain copy or a foreign symlink alone", () =>
66238
Effect.gen(function* () {
67239
const fs = yield* FileSystem.FileSystem;
@@ -85,7 +257,7 @@ it.layer(NodeServices.layer)("t3 update launcher", (it) => {
85257
for (const launchedAs of [copy, foreign, otherLauncher, undefined]) {
86258
const repointed = yield* repointLauncher({
87259
launchedAs,
88-
versionsDir: path.join(root, "runtime/versions"),
260+
baseDir: root,
89261
targetEntryPath: newExe,
90262
});
91263
assert.equal(repointed._tag, "None", launchedAs ?? "undefined");

‎apps/server/src/cli/update.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import {
2626
pinnedRuntimeCommand,
2727
PinnedRuntimeInstallError,
2828
pinnedRuntimePaths,
29+
pinnedRuntimeVersionsDir,
2930
prunePinnedRuntimes,
31+
recordPinnedRuntimeLauncher,
3032
windowsShimTarget,
3133
} from "../cloud/pinnedRuntime.ts";
3234
import { compareExactServiceVersions, isExactServiceVersion } from "../cloud/serviceProtocol.ts";
@@ -92,26 +94,34 @@ export const resolveNewestVersion = Effect.fn("cli.update.resolve_newest")(funct
9294

9395
/**
9496
* Only a launcher that already points into this home's `runtime/versions` tree is touched; a
95-
* plain copy of the executable, or a launcher for some other install, is left alone.
97+
* plain copy of the executable, or a launcher for some other install, is left alone. It is
98+
* recorded before it moves, so prune never meets a repointed launcher it does not know about.
9699
*/
97100
export const repointLauncher = Effect.fn("cli.update.repoint_launcher")(function* (input: {
98101
readonly launchedAs: string | undefined;
99-
readonly versionsDir: string;
102+
readonly baseDir: string;
100103
readonly targetEntryPath: string;
101104
}) {
102105
const fs = yield* FileSystem.FileSystem;
103106
const path = yield* Path.Path;
104107
const platform = yield* HostProcess.Platform;
105108
if (input.launchedAs === undefined) return Option.none<string>();
106-
const ownsTarget = (candidate: string) =>
107-
launcherOwnsVersionsDir(path, input.versionsDir, candidate);
109+
const versionsDir = pinnedRuntimeVersionsDir(path, input.baseDir);
110+
const ownsTarget = (candidate: string) => launcherOwnsVersionsDir(path, versionsDir, candidate);
111+
const remember = (launcher: string) =>
112+
recordPinnedRuntimeLauncher({ baseDir: input.baseDir, launcher }).pipe(
113+
Effect.mapError(
114+
() => new CliUpdateError({ reason: `Could not record the t3 launcher at ${launcher}.` }),
115+
),
116+
);
108117

109118
if (platform === "win32") {
110119
const shimPath = yield* findWindowsShim(input.launchedAs);
111120
if (shimPath === undefined) return Option.none<string>();
112121
const current = yield* fs.readFileString(shimPath).pipe(Effect.option);
113122
const quoted = Option.isSome(current) ? windowsShimTarget(current.value) : undefined;
114123
if (quoted === undefined || !ownsTarget(quoted)) return Option.none<string>();
124+
yield* remember(shimPath);
115125
yield* fs
116126
.writeFileString(shimPath, `@echo off\r\n"${input.targetEntryPath}" %*`)
117127
.pipe(
@@ -126,6 +136,7 @@ export const repointLauncher = Effect.fn("cli.update.repoint_launcher")(function
126136
if (Option.isNone(linkTarget)) return Option.none<string>();
127137
const resolvedTarget = path.resolve(path.dirname(input.launchedAs), linkTarget.value);
128138
if (!ownsTarget(resolvedTarget)) return Option.none<string>();
139+
yield* remember(input.launchedAs);
129140
const tempLink = `${input.launchedAs}.${process.pid}.tmp`;
130141
yield* fs.symlink(input.targetEntryPath, tempLink).pipe(
131142
Effect.andThen(fs.rename(tempLink, input.launchedAs)),
@@ -472,7 +483,7 @@ const runUpdate = Effect.fn("cli.update.run")(function* (input: {
472483
const launchedAs = (yield* HostProcess.IsExecutable) ? yield* resolveLauncherPath : undefined;
473484
const repointed = yield* repointLauncher({
474485
launchedAs,
475-
versionsDir: path.dirname(runtime.versionDir),
486+
baseDir: input.baseDir,
476487
targetEntryPath: runtime.entryPath,
477488
});
478489

‎apps/server/src/cloud/pinnedRuntime.test.ts‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ it.layer(NodeServices.layer)("prunePinnedRuntimes", (it) => {
409409
// The launcher found on PATH is remembered for a service that cannot see it.
410410
assert.equal(
411411
yield* fs.readFileString(path.join(baseDir, "runtime", "launchers")),
412-
`${path.join(customBinDir, "t3")}\n${path.join(binDir, "t3")}\n`,
412+
`${path.join(customBinDir, "t3")}\n\n${path.join(binDir, "t3")}\n`,
413413
);
414414
assert.deepEqual((yield* fs.readDirectory(versionsDir)).toSorted(), [
415415
".staging-abc",
@@ -428,6 +428,52 @@ it.layer(NodeServices.layer)("prunePinnedRuntimes", (it) => {
428428
}),
429429
);
430430

431+
it.effect.each([
432+
// An interrupted append cut this record short.
433+
{ name: "an unterminated launcher record", recorded: "/opt/t", cut: "" },
434+
// Another process's append is cut short after prune reads the records.
435+
{ name: "a record cut short after the read", recorded: "/opt/t3\n", cut: "/opt/t" },
436+
])("remembers a launcher found on PATH after $name", ({ recorded, cut }) =>
437+
Effect.gen(function* () {
438+
const fs = yield* FileSystem.FileSystem;
439+
const path = yield* Path.Path;
440+
const { baseDir, versionsDir } = yield* setup(["1.0.0", "1.1.0"]);
441+
const binDir = path.join(baseDir, "custom-bin");
442+
yield* fs.makeDirectory(binDir);
443+
yield* fs.symlink(path.join(versionsDir, "1.0.0", "t3"), path.join(binDir, "t3"));
444+
const launchersFile = path.join(baseDir, "runtime", "launchers");
445+
yield* fs.writeFileString(launchersFile, recorded);
446+
const prune = (environment: Record<string, string>) =>
447+
prunePinnedRuntimes({ baseDir, keep: [] }).pipe(
448+
Effect.provideService(ProcessRunner.ProcessRunner, lsofRunner("p1\nftxt\nn/bin/sh\n")),
449+
Effect.provideService(HostProcess.Platform, "darwin"),
450+
Effect.provideService(HostProcess.Environment, environment),
451+
Effect.provideService(HostProcess.HomeDirectory, baseDir),
452+
);
453+
454+
yield* prune({ PATH: binDir }).pipe(
455+
Effect.provideService(FileSystem.FileSystem, {
456+
...fs,
457+
readFileString: (file, encoding) =>
458+
fs
459+
.readFileString(file, encoding)
460+
.pipe(
461+
Effect.tap(() =>
462+
file === launchersFile
463+
? fs.writeFileString(file, cut, { flag: "a" })
464+
: Effect.void,
465+
),
466+
),
467+
}),
468+
);
469+
// A service that cannot see the interactive PATH.
470+
const removed = yield* prune({});
471+
472+
assert.deepEqual(removed, []);
473+
assert.isTrue(yield* fs.exists(path.join(versionsDir, "1.0.0", "t3")));
474+
}),
475+
);
476+
431477
it.effect("matches a Windows shim whatever its casing", () =>
432478
Effect.gen(function* () {
433479
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/cloud/pinnedRuntime.ts‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,13 @@ const PINNED_RUNTIME_PRUNE_GRACE = Duration.hours(1);
377377
*/
378378
const PINNED_RUNTIME_LAUNCHERS_FILE = "launchers";
379379

380+
/**
381+
* Starts on a fresh line whatever the file ends with when the append lands: another process's
382+
* append may have been cut short since this one read the file. Readers skip the blank lines.
383+
*/
384+
const launcherRecords = (launchers: ReadonlyArray<string>) =>
385+
`\n${launchers.map((launcher) => `${launcher}\n`).join("")}`;
386+
380387
export function launcherOwnsVersionsDir(
381388
path: Path.Path,
382389
versionsDir: string,
@@ -386,6 +393,27 @@ export function launcherOwnsVersionsDir(
386393
return relative.length > 0 && !relative.startsWith("..") && !path.isAbsolute(relative);
387394
}
388395

396+
/**
397+
* `t3 update` repoints the launcher it was started through, which may be on neither PATH nor the
398+
* default bin dir. Unrecorded, the next prune would remove the runtime it now points at.
399+
*/
400+
export const recordPinnedRuntimeLauncher = Effect.fn("cloud.pinned_runtime.record_launcher")(
401+
function* (input: { readonly baseDir: string; readonly launcher: string }) {
402+
const fs = yield* FileSystem.FileSystem;
403+
const path = yield* Path.Path;
404+
const launchersFile = path.join(
405+
input.baseDir,
406+
PINNED_RUNTIME_DIR,
407+
PINNED_RUNTIME_LAUNCHERS_FILE,
408+
);
409+
// Prune may run in another process, such as the service, whose working directory differs.
410+
const launcher = path.resolve(input.launcher);
411+
const recorded = yield* fs.readFileString(launchersFile).pipe(Effect.orElseSucceed(() => ""));
412+
if (recorded.split(/\r?\n/).includes(launcher)) return;
413+
yield* fs.writeFileString(launchersFile, launcherRecords([launcher]), { flag: "a" });
414+
},
415+
);
416+
389417
/** The executable a `t3.cmd` shim written by the install script or `t3 update` runs. */
390418
export const windowsShimTarget = (contents: string) => /^"([^"]+)"/m.exec(contents)?.[1];
391419

@@ -437,9 +465,7 @@ const shellLauncherTargets = Effect.fn("cloud.pinned_runtime.shell_launcher_targ
437465
}
438466
if (unrecorded.length > 0) {
439467
yield* fs
440-
.writeFileString(launchersFile, unrecorded.map((launcher) => `${launcher}\n`).join(""), {
441-
flag: "a",
442-
})
468+
.writeFileString(launchersFile, launcherRecords(unrecorded), { flag: "a" })
443469
.pipe(Effect.ignore);
444470
}
445471
return targets;

0 commit comments

Comments
 (0)