Repository navigation
Clear away mutation run copies, and give every lock one shared mechanism #1957
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 42 commits
8dd0812
2e8a9b9
26139fb
26e0555
e22d816
95665aa
ac4504b
c15d68a
93306be
7a125db
7ac103d
2bbe57a
5543372
f9b37a0
7160d25
2c85a6a
1db7e21
8be3687
21f8ee6
60b3791
13c9ef0
7b15569
3e92f0a
a46db75
a92f53d
f48e0b4
7670e5b
8f8734e
cea09c3
3d51572
3e1e541
fb9b7a8
69f6206
3575635
482a9a3
e994301
c002fed
469e343
732549e
636989c
63e2367
28c53c3
2aed858
c1db784
e0d6567
f27fb64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /** | ||
| * A lock held for us by a child process, so that giving up on the wait really | ||
| * does give up. | ||
| * | ||
| * Waiting for a lock in this process cannot be called off: the wait keeps the | ||
| * process alive until the lock arrives, however long that takes, and closing | ||
| * the file does not stop it. A command that gave up after a quarter of a second | ||
| * would still sit there until the other job finished. So a child does the | ||
| * waiting, and gives up by ending itself. | ||
| */ | ||
|
|
||
| import { dirname } from "@std/path"; | ||
| import { statOrNull } from "#scripts/not-found.ts"; | ||
|
|
||
| /** | ||
| * Waits for the lock, says which file it got, and holds it until its input | ||
| * closes — when we let go, or if we die first. It gives itself the same time | ||
| * we would, counted from when it starts waiting, and ends if that runs out. | ||
| */ | ||
| const HOLD_LOCK_SCRIPT = ` | ||
| const [path, timeoutText] = Deno.args; | ||
| const say = (line) => Deno.stdout.write(new TextEncoder().encode(line + "\\n")); | ||
| const file = await Deno.open(path, { create: true, read: true, write: true }); | ||
| const gaveUp = setTimeout(() => Deno.exit(0), Number(timeoutText)); | ||
| await file.lock(true); | ||
| clearTimeout(gaveUp); | ||
| const { ino } = await file.stat(); | ||
| await say(\`held \${ino}\`); | ||
| await Deno.stdin.read(new Uint8Array(1)); | ||
| await file.unlock(); | ||
| file.close(); | ||
| `; | ||
|
|
||
| interface HeldLock { | ||
| /** The file it locked, or `NaN` when the disk keeps no file numbers. */ | ||
| fileNumber: number; | ||
| letGo: () => Promise<void>; | ||
| } | ||
|
|
||
| /** The child's `held` line, or `null` if it ends before saying a whole one. */ | ||
| const heldLine = async ( | ||
| reader: ReadableStreamDefaultReader<Uint8Array>, | ||
| ): Promise<string | null> => { | ||
| let said = ""; | ||
| for (;;) { | ||
| const { value } = await reader.read(); | ||
| if (value === undefined) return null; | ||
| said += new TextDecoder().decode(value); | ||
| // Whole lines only — everything after the last newline is still arriving. | ||
| // "held" and its file number can come in two pieces, and half a line taken | ||
| // for the whole of one loses the number. | ||
| const whole = said.split("\n").slice(0, -1); | ||
| const held = whole.find((line) => line.startsWith("held")); | ||
| if (held !== undefined) return held; | ||
| } | ||
| }; | ||
|
|
||
| /** Wait for the child to stop, leaving none of its pipes open behind us. */ | ||
| const closeDown = async ( | ||
| child: Deno.ChildProcess, | ||
| ): Promise<Deno.CommandStatus> => { | ||
| await child.stdin.close(); | ||
| return await child.status; | ||
| }; | ||
|
|
||
| /** | ||
| * Hold the lock at `path` through a child process. `null` means the lock did | ||
| * not come free within `timeoutMs`, or its folder is not there to lock in. A | ||
| * lock that could not be taken at all throws instead: that is not "busy". | ||
| */ | ||
| export const holdLockOrNull = async ( | ||
| path: string, | ||
| timeoutMs: number, | ||
| ): Promise<HeldLock | null> => { | ||
| // No folder to make the lock file in means nobody's lock to take. | ||
| if ((await statOrNull(dirname(path))) === null) return null; | ||
| const child = new Deno.Command(Deno.execPath(), { | ||
| args: ["eval", HOLD_LOCK_SCRIPT, "--", path, String(timeoutMs)], | ||
| stderr: "piped", | ||
| stdin: "piped", | ||
| stdout: "piped", | ||
| }).spawn(); | ||
|
|
||
| const reader = child.stdout.getReader(); | ||
| const line = await heldLine(reader); | ||
| reader.releaseLock(); | ||
| await child.stdout.cancel(); | ||
|
|
||
| if (line === null) { | ||
| // It ends quietly when its own time runs out. Ending any other way means | ||
| // the lock could not be taken at all, which is not the same as busy. | ||
| const why = await new Response(child.stderr).text(); | ||
| const stopped = await closeDown(child); | ||
| // Its folder can go while it is starting, which is nobody's lock rather | ||
| // than a failure to take one. | ||
| const folderWent = (await statOrNull(dirname(path))) === null; | ||
| if (!(stopped.success || folderWent)) { | ||
| throw new Error(`Could not take the lock at ${path}: ${why.trim()}`); | ||
| } | ||
| return null; | ||
|
Comment on lines
+100
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When the helper child fails before reporting AGENTS.md reference: AGENTS.md:L115-L117 Useful? React with 👍 / 👎.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed — a swallowed failure, and the worst kind, since the run would be called active and its copy left behind without a word. Fixed in c002fed. The child now ends quietly only when its own time runs out, so any other ending means the lock could not be taken at all, and that throws with the child's own error text attached. Writing the test caught a distinction worth naming: the folder going while the child is starting also kills the child, but that is the documented "nobody's lock", not a failure — so it throws only when the folder is still there. My own deleted-folder test failed until I got that right. New test: "says so loudly when the lock cannot be taken at all", which puts a folder where the lock file should be. Generated by Claude Code |
||
| } | ||
| await child.stderr.cancel(); | ||
| return { | ||
| fileNumber: Number(line.split(" ")[1]), | ||
| letGo: async () => { | ||
| await closeDown(child); | ||
| }, | ||
| }; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When another cleanup removes the run directory after the parent check but before the helper's
Deno.open, the child exits withNotFound; if the run owner recreates that directory before this laterstatOrNull,folderWentis false and an expected concurrent removal is misreported as a lock failure. In the mutation startup path, that rejection escapesremoveInactiveRunsand prevents the new run from starting. The fresh, separate path from the fixed lock probe isholdLockOrNull's post-exit directory check; have the child reportNotFounddistinctly instead of inferring it from mutable state afterward.AGENTS.md reference: AGENTS.md:L937-L941
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, and the reasoning is the part worth keeping: I was deciding whether the folder had gone by looking at the disk after the child had exited — a fact read from state that can change underneath it. The owner remaking the folder in that gap turns an ordinary concurrent clear-up into "the lock could not be taken", and on the startup path that rejection stops the next run from starting at all.
Fixed in e0d6567. The child reports a missing folder with an ending of its own rather than dying of it, so the answer comes from the moment it happened. Anything else it dies of still throws, with its own error text attached.
Second time tonight this same shape has bitten on this path, so to say it plainly: when something transient has to be told apart, the party that saw it should say so, instead of somebody re-deriving it later.
Generated by Claude Code