Skip to content

Commit b252a8b

Browse files
committed
fix(ssh): concurrent archive installs no longer race on uutils mkdir
The remote runner used `mkdir` as its install lock, but uutils coreutils 0.10 (Ubuntu's default since 25.10) exits 0 when it loses the race to create the directory (uutils/coreutils#13970, fixed in 0.11). Concurrent launches could then all take the lock, collide on its pid.tmp, or clobber each other's runtime directory. Create the lock as a file with the shell's noclobber redirect, which opens it with O_EXCL and writes the owner's pid in the same step. The race test now shims a uutils-style mkdir so it covers such hosts on any runner.
1 parent a9c9c7c commit b252a8b

2 files changed

Lines changed: 39 additions & 22 deletions

File tree

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ describe("ssh tunnel scripts", () => {
122122
script,
123123
'T3_LOCK="$HOME/.t3/runtime/versions/.$T3_ARCHIVE_VERSION.install.lock"',
124124
);
125-
assert.include(script, 'while ! mkdir "$T3_LOCK" 2>/dev/null; do');
126-
assert.include(script, 'mv "$T3_LOCK/pid.tmp" "$T3_LOCK/pid"');
125+
assert.include(script, 'while ! (set -C; printf \'%s\\n\' "$$" > "$T3_LOCK") 2>/dev/null; do');
127126
assert.include(script, 'if ! kill -0 "$T3_LOCK_OWNER" 2>/dev/null; then');
128127
assert.include(script, 'if [ "$T3_LOCK_UNOWNED" -ge 5 ]; then');
129128
assert.include(script, 'if [ "$T3_LOCK_WAITED" -ge 360 ]; then');
@@ -231,7 +230,7 @@ describe("ssh tunnel scripts", () => {
231230

232231
it("embeds the runner in the launch script byte for byte", () => {
233232
const runner = SshTunnel.buildRemoteT3RunnerScript(ARCHIVE);
234-
assert.include(runner, 'printf \'%s\\n\' "$$" > "$T3_LOCK/pid.tmp"');
233+
assert.include(runner, 'printf \'%s\\n\' "$$" > "$T3_LOCK"');
235234
assert.include(SshTunnel.buildRemoteLaunchScript(ARCHIVE), runner);
236235
});
237236

@@ -818,12 +817,16 @@ describe("remote port picker", () => {
818817
);
819818
});
820819

821-
const runScript = (home: string, args: ReadonlyArray<string>) =>
820+
const runScript = (
821+
home: string,
822+
args: ReadonlyArray<string>,
823+
path: string = process.env.PATH ?? "",
824+
) =>
822825
Effect.gen(function* () {
823826
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
824827
const child = yield* spawner.spawn(
825828
ChildProcess.make("sh", args, {
826-
env: { PATH: process.env.PATH ?? "", HOME: home },
829+
env: { PATH: path, HOME: home },
827830
extendEnv: false,
828831
}),
829832
);
@@ -856,7 +859,8 @@ describe("archive runner script", () => {
856859
const windowsHost = hostPlatform === "win32";
857860
const archiveVersion = "1.2.3-preview.20260911.4";
858861

859-
const runRunner = (home: string, runner: string) => runScript(home, [runner, "--version"]);
862+
const runRunner = (home: string, runner: string, path?: string) =>
863+
runScript(home, [runner, "--version"], path);
860864

861865
const makeMirror = Effect.fn("makeMirror")(function* (root: string) {
862866
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
@@ -892,9 +896,24 @@ describe("archive runner script", () => {
892896
);
893897
const home = `${root}/home`;
894898
yield* fs.makeDirectory(home, { recursive: true });
899+
// uutils coreutils (Ubuntu's default) mkdir exits 0 when it loses the
900+
// race to create a directory. Shim that behavior so the race covers
901+
// such hosts wherever the test runs.
902+
const hostPath = process.env.PATH ?? "";
903+
yield* fs.makeDirectory(`${root}/bin`);
904+
yield* fs.writeFileString(
905+
`${root}/bin/mkdir`,
906+
`#!/bin/sh\nPATH='${hostPath}' exec mkdir -p "$@"\n`,
907+
);
908+
yield* fs.chmod(`${root}/bin/mkdir`, 0o755);
909+
const racePath = `${root}/bin:${hostPath}`;
895910

896911
const results = yield* Effect.all(
897-
[runRunner(home, runner), runRunner(home, runner), runRunner(home, runner)],
912+
[
913+
runRunner(home, runner, racePath),
914+
runRunner(home, runner, racePath),
915+
runRunner(home, runner, racePath),
916+
],
898917
{ concurrency: "unbounded" },
899918
);
900919
for (const result of results) {
@@ -910,13 +929,12 @@ describe("archive runner script", () => {
910929

911930
const lock = `${versionsDir}/.${archiveVersion}.install.lock`;
912931
yield* fs.remove(`${versionsDir}/${archiveVersion}`, { recursive: true });
913-
yield* fs.makeDirectory(lock);
914-
yield* fs.writeFileString(`${lock}/pid`, "999999\n");
932+
yield* fs.writeFileString(lock, "999999\n");
915933
const afterDead = yield* runRunner(home, runner);
916934
assert.equal(afterDead.exitCode, 0, afterDead.stderr);
917935

918936
yield* fs.remove(`${versionsDir}/${archiveVersion}`, { recursive: true });
919-
yield* fs.makeDirectory(lock);
937+
yield* fs.writeFileString(lock, "");
920938
const afterUnowned = yield* runRunner(home, runner);
921939
assert.equal(afterUnowned.exitCode, 0, afterUnowned.stderr);
922940
assert.isFalse(yield* fs.exists(lock));

‎packages/ssh/src/tunnel.ts‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -461,20 +461,20 @@ t3_runtime_ready() {
461461
if ! t3_runtime_ready; then
462462
mkdir -p "$HOME/.t3/runtime/versions"
463463
# Concurrent launches (two clients, a retry racing a slow first run) must
464-
# not both install: mkdir is the atomic lock and the ready check repeats
465-
# under it.
464+
# not both install: the lock is a file created exclusively and the ready
465+
# check repeats under it.
466466
T3_LOCK="$HOME/.t3/runtime/versions/.$T3_ARCHIVE_VERSION.install.lock"
467-
# mkdir is the only portable atomic exclusive create (mv would silently
468-
# nest a candidate inside an existing lock). The owner publishes its pid
469-
# right after, so a lock with a live owner is never reclaimed however
470-
# slow its download is, and a lock whose owner is dead is reclaimed at
471-
# once. A lock with no pid at all is a crash between mkdir and the pid
472-
# write; it is reclaimed after a short grace so a live owner has time to
473-
# publish.
467+
# noclobber makes the shell create the lock with O_EXCL. mkdir is not
468+
# exclusive everywhere: uutils coreutils 0.10 (Ubuntu's default since
469+
# 25.10) exits 0 when it loses the race to create the directory
470+
# (uutils/coreutils#13970, fixed in 0.11). The lock holds the owner's pid,
471+
# so a lock with a live owner is never reclaimed however slow its download
472+
# is, and a lock whose owner is dead is reclaimed at once. An empty lock is a crash between the create and the pid write; it
473+
# is reclaimed after a short grace so a live owner has time to publish.
474474
T3_LOCK_WAITED=0
475475
T3_LOCK_UNOWNED=0
476-
while ! mkdir "$T3_LOCK" 2>/dev/null; do
477-
T3_LOCK_OWNER="$(cat "$T3_LOCK/pid" 2>/dev/null || true)"
476+
while ! (set -C; printf '%s\\n' "$$" > "$T3_LOCK") 2>/dev/null; do
477+
T3_LOCK_OWNER="$(cat "$T3_LOCK" 2>/dev/null || true)"
478478
if [ -n "$T3_LOCK_OWNER" ]; then
479479
T3_LOCK_UNOWNED=0
480480
if ! kill -0 "$T3_LOCK_OWNER" 2>/dev/null; then
@@ -495,7 +495,6 @@ if ! t3_runtime_ready; then
495495
sleep 1
496496
T3_LOCK_WAITED=$((T3_LOCK_WAITED + 1))
497497
done
498-
printf '%s\\n' "$$" > "$T3_LOCK/pid.tmp" && mv "$T3_LOCK/pid.tmp" "$T3_LOCK/pid"
499498
trap 'rm -rf "$T3_LOCK"' EXIT
500499
fi
501500
if ! t3_runtime_ready; then

0 commit comments

Comments
 (0)