Skip to content

Commit 9c9ee34

Browse files
only21milclaude
andcommitted
fix(cli): recover a stale runtime folder under a launcher lock
Restore automatic replacement of an incomplete version folder, but only while holding <dir>.lock, after rechecking the marker, and by moving the stale folder aside instead of deleting it in place. A second launcher that finds the lock held exits with a message naming the lock instead of removing the install the first one just published. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7cd66f1 commit 9c9ee34

2 files changed

Lines changed: 55 additions & 12 deletions

File tree

‎scripts/build-npm-launcher.test.ts‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ it.layer(NodeServices.layer)("build-npm-launcher", (it) => {
200200
VERSION,
201201
]);
202202

203-
// A peer's executable and sidecar stay available at every recovery boundary,
203+
// A peer's executable and sidecar stay available after every later rename or removal,
204204
// even when it publishes after our missing-marker read.
205205
const staleHome = path.join(root, "stale-home");
206206
const staleDir = path.join(staleHome, "runtime/versions", VERSION);
@@ -274,25 +274,39 @@ fs.readFileSync = (file, ...rest) => {
274274
const checks = (yield* fs.readFileString(peerChecks)).trim().split("\n");
275275
assert.isAtLeast(checks.length, 2);
276276
assert.deepStrictEqual([...new Set(checks)], ["7|peer --version|peer sidecar"]);
277-
assert.equal(recovered.exitCode, 1);
278-
assert.equal(recovered.stdout, "");
279-
assert.match(recovered.stderr, /rename/);
277+
assert.equal(recovered.stdout.trim(), "peer --version", recovered.stderr);
278+
assert.equal(recovered.exitCode, 7);
280279
assert.deepStrictEqual(yield* fs.readDirectory(path.join(staleHome, "runtime/versions")), [
281280
VERSION,
282281
]);
283282

284-
// Without a peer, a nonempty incomplete install is left for manual repair.
283+
// While another launch holds the lock, a stale folder is left alone and the error says
284+
// how to clear a lock left by a crash.
285285
const staleOnlyHome = path.join(root, "stale-only-home");
286286
const staleOnlyDir = path.join(staleOnlyHome, "runtime/versions", VERSION);
287287
yield* fs.makeDirectory(staleOnlyDir, { recursive: true });
288288
yield* fs.writeFileString(path.join(staleOnlyDir, "t3"), "incomplete");
289-
const refused = yield* run(process.execPath, [launcher, "--version"], {
289+
yield* fs.makeDirectory(`${staleOnlyDir}.lock`);
290+
const locked = yield* run(process.execPath, [launcher, "--version"], {
290291
cwd: root,
291292
env: { ...env, T3CODE_HOME: staleOnlyHome },
292293
});
293-
assert.equal(refused.exitCode, 1);
294-
assert.match(refused.stderr, /rename/);
294+
assert.equal(locked.exitCode, 1);
295+
assert.include(locked.stderr, `delete ${staleOnlyDir}.lock`);
295296
assert.equal(yield* fs.readFileString(path.join(staleOnlyDir, "t3")), "incomplete");
297+
298+
// Without a peer, the stale folder is replaced by our own download.
299+
yield* fs.remove(`${staleOnlyDir}.lock`, { recursive: true });
300+
const replaced = yield* run(process.execPath, [launcher, "--version"], {
301+
cwd: root,
302+
env: { ...env, T3CODE_HOME: staleOnlyHome },
303+
});
304+
assert.equal(
305+
replaced.stdout.trim(),
306+
`stub --version launcher=${launcher}`,
307+
replaced.stderr,
308+
);
309+
assert.equal(replaced.exitCode, 7);
296310
assert.deepStrictEqual(
297311
yield* fs.readDirectory(path.join(staleOnlyHome, "runtime/versions")),
298312
[VERSION],

‎scripts/npm-launcher/t3.mjs‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,45 @@ const isComplete = (dir) => {
115115
/**
116116
* Moves a finished staging folder to `dir`. Rename cannot replace a nonempty
117117
* folder, so a complete install another launcher (or `t3 update`) published
118-
* first stays available at its original path. Refuse to clear an incomplete
119-
* folder: another publisher can complete it between the marker check and
120-
* removing or moving it.
118+
* first is kept, never deleted from under a running process. Only a folder
119+
* without the completion marker, left by an interrupted older installer, is
120+
* cleared before trying again, and only while holding `<dir>.lock`: two
121+
* launchers clearing it at once would let one delete the install the other
122+
* just published.
121123
*/
122124
function publish(staging, dir) {
123125
try {
124126
fs.renameSync(staging, dir);
127+
return;
125128
} catch (error) {
126129
if (isComplete(dir)) return;
127-
throw error;
130+
if (!fs.existsSync(dir)) throw error;
131+
}
132+
const lock = `${dir}.lock`;
133+
try {
134+
fs.mkdirSync(lock);
135+
} catch (error) {
136+
if (error.code !== "EEXIST") throw error;
137+
throw new Error(
138+
`another t3 is installing ${version}. If none is running, delete ${lock} and try again.`,
139+
{ cause: error },
140+
);
141+
}
142+
const stale = `${staging}.stale`;
143+
try {
144+
// A peer may have published between our marker check and taking the lock.
145+
if (isComplete(dir)) return;
146+
// Move it aside rather than deleting in place: once the folder is empty, a
147+
// peer's first rename can land in it.
148+
fs.renameSync(dir, stale);
149+
try {
150+
fs.renameSync(staging, dir);
151+
} catch (error) {
152+
if (!isComplete(dir)) throw error;
153+
}
154+
} finally {
155+
fs.rmSync(lock, { recursive: true, force: true });
156+
fs.rmSync(stale, { recursive: true, force: true });
128157
}
129158
}
130159

0 commit comments

Comments
 (0)