Skip to content

Commit 99c692b

Browse files
fix(ssh): concurrent archive installs no longer race on uutils mkdir (#20059)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 3f7c3aa commit 99c692b

2 files changed

Lines changed: 61 additions & 28 deletions

File tree

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

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ describe("ssh tunnel scripts", () => {
124124
script,
125125
'T3_LOCK="$HOME/.t3/runtime/versions/.$T3_ARCHIVE_VERSION.install.lock"',
126126
);
127-
assert.include(script, 'while ! mkdir "$T3_LOCK" 2>/dev/null; do');
128-
assert.include(script, 'mv "$T3_LOCK/pid.tmp" "$T3_LOCK/pid"');
129127
assert.include(script, 'if ! kill -0 "$T3_LOCK_OWNER" 2>/dev/null; then');
130128
assert.include(script, 'if [ "$T3_LOCK_UNOWNED" -ge 5 ]; then');
131129
assert.include(script, 'if [ "$T3_LOCK_WAITED" -ge 360 ]; then');
@@ -233,7 +231,6 @@ describe("ssh tunnel scripts", () => {
233231

234232
it("embeds the runner in the launch script byte for byte", () => {
235233
const runner = SshTunnel.buildRemoteT3RunnerScript(ARCHIVE);
236-
assert.include(runner, 'printf \'%s\\n\' "$$" > "$T3_LOCK/pid.tmp"');
237234
assert.include(SshTunnel.buildRemoteLaunchScript(ARCHIVE), runner);
238235
});
239236

@@ -1053,13 +1050,22 @@ describe("remote port picker", () => {
10531050
);
10541051
});
10551052

1056-
const runScript = (home: string, args: ReadonlyArray<string>) =>
1053+
const runScript = (
1054+
home: string,
1055+
args: ReadonlyArray<string>,
1056+
options?: {
1057+
path?: string;
1058+
stdin?: Stream.Stream<Uint8Array>;
1059+
onStderrLine?: (line: string) => Effect.Effect<void>;
1060+
},
1061+
) =>
10571062
Effect.gen(function* () {
10581063
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
10591064
const child = yield* spawner.spawn(
10601065
ChildProcess.make("sh", args, {
1061-
env: { PATH: process.env.PATH ?? "", HOME: home },
1066+
env: { PATH: options?.path ?? process.env.PATH ?? "", HOME: home },
10621067
extendEnv: false,
1068+
...(options?.stdin ? { stdin: { stream: options.stdin } } : {}),
10631069
}),
10641070
);
10651071
const [stdout, stderr, exitCode] = yield* Effect.all(
@@ -1073,9 +1079,11 @@ const runScript = (home: string, args: ReadonlyArray<string>) =>
10731079
),
10741080
child.stderr.pipe(
10751081
Stream.decodeText(),
1082+
Stream.splitLines,
1083+
Stream.tap((line) => options?.onStderrLine?.(line) ?? Effect.void),
10761084
Stream.runFold(
10771085
() => "",
1078-
(acc, chunk) => acc + chunk,
1086+
(acc, line) => `${acc}${line}\n`,
10791087
),
10801088
),
10811089
child.exitCode.pipe(Effect.map(Number)),
@@ -1091,7 +1099,8 @@ describe("archive runner script", () => {
10911099
const windowsHost = hostPlatform === "win32";
10921100
const archiveVersion = "1.2.3-preview.20260911.4";
10931101

1094-
const runRunner = (home: string, runner: string) => runScript(home, [runner, "--version"]);
1102+
const runRunner = (home: string, runner: string, options?: Parameters<typeof runScript>[2]) =>
1103+
runScript(home, [runner, "--version"], options);
10951104

10961105
const makeMirror = Effect.fn("makeMirror")(function* (root: string) {
10971106
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
@@ -1127,11 +1136,45 @@ describe("archive runner script", () => {
11271136
);
11281137
const home = `${root}/home`;
11291138
yield* fs.makeDirectory(home, { recursive: true });
1130-
1131-
const results = yield* Effect.all(
1132-
[runRunner(home, runner), runRunner(home, runner), runRunner(home, runner)],
1133-
{ concurrency: "unbounded" },
1134-
);
1139+
// Reproduce mkdir succeeding on EEXIST, even on hosts without the uutils bug.
1140+
const bin = `${root}/bin`;
1141+
yield* fs.makeDirectory(bin);
1142+
for (const [name, script] of Object.entries({
1143+
mkdir: 'PATH=${PATH#*:}\nexec mkdir -p "$@"',
1144+
tar: 'echo >> "${0%/*}/installs"\necho installing >&2\nread -r release\nPATH=${PATH#*:}\nexec tar "$@"',
1145+
sleep: 'echo waiting >&2\nPATH=${PATH#*:}\nexec sleep "$@"',
1146+
})) {
1147+
yield* fs.writeFileString(`${bin}/${name}`, `#!/bin/sh\n${script}\n`);
1148+
yield* fs.chmod(`${bin}/${name}`, 0o755);
1149+
}
1150+
const path = `${bin}:${process.env.PATH ?? ""}`;
1151+
const installing = yield* Deferred.make<void>();
1152+
const contended = yield* Deferred.make<void>();
1153+
// Hold the first extraction until another runner either waits for the lock
1154+
// or incorrectly begins installing. This forces contention without timing assumptions.
1155+
const first = yield* runRunner(home, runner, {
1156+
path,
1157+
stdin: Stream.fromEffect(
1158+
Deferred.await(contended).pipe(Effect.as(new TextEncoder().encode("release\n"))),
1159+
),
1160+
onStderrLine: (line) =>
1161+
line === "installing"
1162+
? Deferred.succeed(installing, undefined).pipe(Effect.asVoid)
1163+
: Effect.void,
1164+
}).pipe(Effect.forkChild);
1165+
yield* Deferred.await(installing);
1166+
const contender = runRunner(home, runner, {
1167+
path,
1168+
stdin: Stream.make(new TextEncoder().encode("release\n")),
1169+
onStderrLine: (line) =>
1170+
line === "installing" || line === "waiting"
1171+
? Deferred.succeed(contended, undefined).pipe(Effect.asVoid)
1172+
: Effect.void,
1173+
});
1174+
const results = yield* Effect.all([Fiber.join(first), contender, contender], {
1175+
concurrency: "unbounded",
1176+
});
1177+
assert.equal(yield* fs.readFileString(`${bin}/installs`), "\n");
11351178
for (const result of results) {
11361179
assert.equal(result.exitCode, 0, result.stderr);
11371180
assert.include(result.stdout, `t3 v${archiveVersion}`);
@@ -1145,13 +1188,12 @@ describe("archive runner script", () => {
11451188

11461189
const lock = `${versionsDir}/.${archiveVersion}.install.lock`;
11471190
yield* fs.remove(`${versionsDir}/${archiveVersion}`, { recursive: true });
1148-
yield* fs.makeDirectory(lock);
1149-
yield* fs.writeFileString(`${lock}/pid`, "999999\n");
1191+
yield* fs.writeFileString(lock, "999999\n");
11501192
const afterDead = yield* runRunner(home, runner);
11511193
assert.equal(afterDead.exitCode, 0, afterDead.stderr);
11521194

11531195
yield* fs.remove(`${versionsDir}/${archiveVersion}`, { recursive: true });
1154-
yield* fs.makeDirectory(lock);
1196+
yield* fs.writeFileString(lock, "");
11551197
const afterUnowned = yield* runRunner(home, runner);
11561198
assert.equal(afterUnowned.exitCode, 0, afterUnowned.stderr);
11571199
assert.isFalse(yield* fs.exists(lock));

‎packages/ssh/src/tunnel.ts‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -461,21 +461,13 @@ t3_runtime_ready() {
461461
}
462462
if ! t3_runtime_ready; then
463463
mkdir -p "$HOME/.t3/runtime/versions"
464-
# Concurrent launches (two clients, a retry racing a slow first run) must
465-
# not both install: mkdir is the atomic lock and the ready check repeats
466-
# under it.
467464
T3_LOCK="$HOME/.t3/runtime/versions/.$T3_ARCHIVE_VERSION.install.lock"
468-
# mkdir is the only portable atomic exclusive create (mv would silently
469-
# nest a candidate inside an existing lock). The owner publishes its pid
470-
# right after, so a lock with a live owner is never reclaimed however
471-
# slow its download is, and a lock whose owner is dead is reclaimed at
472-
# once. A lock with no pid at all is a crash between mkdir and the pid
473-
# write; it is reclaimed after a short grace so a live owner has time to
474-
# publish.
465+
# uutils mkdir can report success on EEXIST, so use shell noclobber.
466+
# An empty lock gets a short grace period for its owner to publish a PID.
475467
T3_LOCK_WAITED=0
476468
T3_LOCK_UNOWNED=0
477-
while ! mkdir "$T3_LOCK" 2>/dev/null; do
478-
T3_LOCK_OWNER="$(cat "$T3_LOCK/pid" 2>/dev/null || true)"
469+
while ! (set -C; printf '%s\\n' "$$" > "$T3_LOCK") 2>/dev/null; do
470+
T3_LOCK_OWNER="$(cat "$T3_LOCK" 2>/dev/null || true)"
479471
if [ -n "$T3_LOCK_OWNER" ]; then
480472
T3_LOCK_UNOWNED=0
481473
if ! kill -0 "$T3_LOCK_OWNER" 2>/dev/null; then
@@ -496,7 +488,6 @@ if ! t3_runtime_ready; then
496488
sleep 1
497489
T3_LOCK_WAITED=$((T3_LOCK_WAITED + 1))
498490
done
499-
printf '%s\\n' "$$" > "$T3_LOCK/pid.tmp" && mv "$T3_LOCK/pid.tmp" "$T3_LOCK/pid"
500491
trap 'rm -rf "$T3_LOCK"' EXIT
501492
fi
502493
if ! t3_runtime_ready; then

0 commit comments

Comments
 (0)