The best way to control Claude Code, Codex and more from any computer or phone
fix(ssh): concurrent archive installs no longer race on uutils mkdir (#20059)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Tristan Knight committed
Oct 19, 2026 at 19:55 UTC
99c692bb1f7dc31a6b1a28eddcff48aee6fd8186
2 files changed
+61
-28
packages/ssh/src/tunnel.test.ts
+57
-15
@@ -124,8 +124,6 @@ describe("ssh tunnel scripts", () => {
124
script,
125
'T3_LOCK="$HOME/.t3/runtime/versions/.$T3_ARCHIVE_VERSION.install.lock"',
126
);
127
- assert.include(script, 'while ! mkdir "$T3_LOCK" 2>/dev/null; do');
128
- assert.include(script, 'mv "$T3_LOCK/pid.tmp" "$T3_LOCK/pid"');
127
assert.include(script, 'if ! kill -0 "$T3_LOCK_OWNER" 2>/dev/null; then');
128
assert.include(script, 'if [ "$T3_LOCK_UNOWNED" -ge 5 ]; then');
129
assert.include(script, 'if [ "$T3_LOCK_WAITED" -ge 360 ]; then');
@@ -233,7 +231,6 @@ describe("ssh tunnel scripts", () => {
231
232
it("embeds the runner in the launch script byte for byte", () => {
233
const runner = SshTunnel.buildRemoteT3RunnerScript(ARCHIVE);
236
- assert.include(runner, 'printf \'%s\\n\' "$$" > "$T3_LOCK/pid.tmp"');
234
assert.include(SshTunnel.buildRemoteLaunchScript(ARCHIVE), runner);
235
});
236
@@ -1053,13 +1050,22 @@ describe("remote port picker", () => {
1050
);
1051
});
1052
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
+) =>
1062
Effect.gen(function* () {
1063
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
1064
const child = yield* spawner.spawn(
1065
ChildProcess.make("sh", args, {
1061
- env: { PATH: process.env.PATH ?? "", HOME: home },
1066
+ env: { PATH: options?.path ?? process.env.PATH ?? "", HOME: home },
1067
extendEnv: false,
1068
+ ...(options?.stdin ? { stdin: { stream: options.stdin } } : {}),
1069
}),
1070
);
1071
const [stdout, stderr, exitCode] = yield* Effect.all(
@@ -1073,9 +1079,11 @@ const runScript = (home: string, args: ReadonlyArray<string>) =>
1079
),
1080
child.stderr.pipe(
1081
Stream.decodeText(),
1082
+ Stream.splitLines,
1083
+ Stream.tap((line) => options?.onStderrLine?.(line) ?? Effect.void),
1084
Stream.runFold(
1085
() => "",
1078
- (acc, chunk) => acc + chunk,
1086
+ (acc, line) => `${acc}${line}\n`,
1087
),
1088
),
1089
child.exitCode.pipe(Effect.map(Number)),
@@ -1091,7 +1099,8 @@ describe("archive runner script", () => {
1099
const windowsHost = hostPlatform === "win32";
1100
const archiveVersion = "1.2.3-preview.20260911.4";
1101
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);
1104
1105
const makeMirror = Effect.fn("makeMirror")(function* (root: string) {
1106
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
@@ -1127,11 +1136,45 @@ describe("archive runner script", () => {
1136
);
1137
const home = `${root}/home`;
1138
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");
1178
for (const result of results) {
1179
assert.equal(result.exitCode, 0, result.stderr);
1180
assert.include(result.stdout, `t3 v${archiveVersion}`);
@@ -1145,13 +1188,12 @@ describe("archive runner script", () => {
1188
1189
const lock = `${versionsDir}/.${archiveVersion}.install.lock`;
1190
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");
1192
const afterDead = yield* runRunner(home, runner);
1193
assert.equal(afterDead.exitCode, 0, afterDead.stderr);
1194
1195
yield* fs.remove(`${versionsDir}/${archiveVersion}`, { recursive: true });
1154
- yield* fs.makeDirectory(lock);
1196
+ yield* fs.writeFileString(lock, "");
1197
const afterUnowned = yield* runRunner(home, runner);
1198
assert.equal(afterUnowned.exitCode, 0, afterUnowned.stderr);
1199
assert.isFalse(yield* fs.exists(lock));
packages/ssh/src/tunnel.ts
+4
-13
@@ -461,21 +461,13 @@ t3_runtime_ready() {
461
}
462
if ! t3_runtime_ready; then
463
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.
464
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.
467
T3_LOCK_WAITED=0
468
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)"
471
if [ -n "$T3_LOCK_OWNER" ]; then
472
T3_LOCK_UNOWNED=0
473
if ! kill -0 "$T3_LOCK_OWNER" 2>/dev/null; then
@@ -496,7 +488,6 @@ if ! t3_runtime_ready; then
488
sleep 1
489
T3_LOCK_WAITED=$((T3_LOCK_WAITED + 1))
490
done
499
- printf '%s\\n' "$$" > "$T3_LOCK/pid.tmp" && mv "$T3_LOCK/pid.tmp" "$T3_LOCK/pid"
491
trap 'rm -rf "$T3_LOCK"' EXIT
492
fi
493
if ! t3_runtime_ready; then