Skip to content

Commit 5bf2a80

Browse files
committed
fix(flows): the writer owns its staging file from the exclusive open; the dir form claims the referent too
- The staging file is opened `wx` first and owned from that moment, so a write that fails after the create (disk full) still removes its own file, and a create that fails on another writer's file removes nothing. - The claim covers both names a file has: the referent as the filesystem names it now (which is the first target's on-disk leaf once it exists, catching a leaf spelled in another case on a case-insensitive filesystem) and the leaf the rename reaches (catching a leaf that was a link).
1 parent e6bdf99 commit 5bf2a80

4 files changed

Lines changed: 72 additions & 11 deletions

File tree

‎src/flows/redact.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ export function emitRedactedResult(
151151
}
152152
}
153153

154-
/** A result the switch above does not name fails to compile here, so a new one cannot fall through silently. */
155154
function unreachable(result: never): never {
156155
throw new Error(`BUG: emitRedactedResult has no arm for the run result ${String(result)}`);
157156
}

‎src/flows/settings-write.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@
88
*/
99

1010
import { randomBytes } from "node:crypto";
11-
import { mkdirSync, realpathSync, renameSync, rmSync, writeFileSync } from "node:fs";
11+
import {
12+
closeSync,
13+
mkdirSync,
14+
openSync,
15+
realpathSync,
16+
renameSync,
17+
rmSync,
18+
writeFileSync,
19+
} from "node:fs";
1220
import { dirname, join, parse, resolve, sep } from "node:path";
1321
import { err, ok, type Result } from "neverthrow";
1422

@@ -48,16 +56,25 @@ function realOrSpelled(path: string): string {
4856
/** The error is the filesystem's own reason; each caller names the input that chose the path. */
4957
export function writeReplacing(path: string, text: string): Result<void, string> {
5058
const staging = `${path}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`;
51-
// Set once the exclusive create succeeded: only a staging file THIS write made is removed on failure, never one
59+
// Set once the exclusive open succeeded: only a staging file THIS write made is removed on failure, never one
5260
// another writer got there first with (`wx` fails on it, and that failure is the one reported).
5361
let created = false;
62+
let fd: number | undefined;
5463
try {
5564
mkdirSync(dirname(path), { recursive: true });
56-
writeFileSync(staging, text, { flag: "wx" });
65+
fd = openSync(staging, "wx");
5766
created = true;
67+
writeFileSync(fd, text);
68+
closeSync(fd);
69+
fd = undefined;
5870
renameSync(staging, path);
5971
return ok();
6072
} catch (error) {
73+
if (fd !== undefined) {
74+
try {
75+
closeSync(fd);
76+
} catch {}
77+
}
6178
if (created) {
6279
try {
6380
rmSync(staging, { force: true });

‎src/flows/snapshot.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,12 @@ async function snapshotTarget(ctx: {
198198
* directory that leads into the other, or an owner spelled ".github". Two
199199
* targets can land on ONE file the same way (a link `out/bob -> out/alice`
200200
* with targets alice/r and bob/r), so every landing is claimed in `claimed`
201-
* for the run and a second claim is refused. The claim is the file the RENAME
202-
* reaches: the directory as the filesystem names it plus the leaf as spelled,
203-
* since the rename replaces a link at the leaf rather than following it. The
201+
* for the run and a second claim is refused. Two names are claimed per file:
202+
* the referent as the filesystem names it now, and the file the RENAME
203+
* reaches (the directory as the filesystem names it plus the leaf as spelled,
204+
* since the rename replaces a link at the leaf rather than following it). The
205+
* first catches a leaf spelled in another case on a case-insensitive
206+
* filesystem once the file exists; the second, a leaf that was a link. The
204207
* refusal names the earlier target through `display`, so a redacted one stays
205208
* sealed, and never the landing, whose spelling is the operator's.
206209
*/
@@ -228,14 +231,19 @@ function snapshotFilePath(
228231
error: `cannot write the snapshot to ${path}: the filesystem carries it to ${landing}, inside the "repos-dir" input "${cfg.reposDir}". Write the snapshots to a directory that leads to no central file`,
229232
};
230233
}
231-
const written = join(canonicalPath(dirname(path)), basename(path));
232-
const earlier = claimed.get(written);
234+
const names = [landing, join(canonicalPath(dirname(path)), basename(path))];
235+
const earlier = names.map((name) => claimed.get(name)).find((slug) => slug !== undefined);
233236
if (earlier !== undefined) {
234237
return {
235-
error: `cannot write the snapshot to ${path}: the filesystem carries it to the file this run already claimed for ${display(earlier)}. Remove the link under the "snapshot-dir" input that folds the two owners together, so each target has a file of its own`,
238+
error:
239+
`cannot write the snapshot to ${path}: the filesystem carries it to the file this run already claimed for ` +
240+
`${display(earlier)}. Remove the link under the "snapshot-dir" input that folds the two owners together, so ` +
241+
"each target has a file of its own",
236242
};
237243
}
238-
claimed.set(written, repo.slug);
244+
for (const name of names) {
245+
claimed.set(name, repo.slug);
246+
}
239247
return { path };
240248
}
241249

‎test/flows/snapshot.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,43 @@ describe("runSnapshot, dir form", () => {
849849
expect(readFileSync(join(dir, "old", "r.yml"), "utf8")).toBe("labels: []\n");
850850
}));
851851

852+
test("two targets whose leaves differ only in case are one file on a case-insensitive filesystem: the second is refused", () =>
853+
withTempDir("snapshot-flow-", async (dir) => {
854+
// Slugs dedupe case-insensitively, so the alias needs two owners folded by a link and leaves spelled apart:
855+
// alice/R writes alice/R.yml, bob/r reaches the same directory through the link with the leaf r.yml. The
856+
// filesystem's own name for that path (realpath, on-disk case) is the first's file, so the referent claim
857+
// catches what the leaf key cannot.
858+
writeFileSync(join(dir, "Probe"), "");
859+
const caseInsensitive = existsSync(join(dir, "probe"));
860+
const api = new MockApi({
861+
"GET /repos/alice/R": { data: { private: false } },
862+
"GET /repos/bob/r": { data: { private: false } },
863+
...labelsRoute("alice/R", [BUG]),
864+
...labelsRoute("bob/r", [DOCS]),
865+
});
866+
const cfg = dirCfg(dir, { reposInput: "alice/R,bob/r" });
867+
mkdirSync(join(cfg.snapshotDir, "alice"), { recursive: true });
868+
symlinkSync("alice", join(cfg.snapshotDir, "bob"));
869+
const collected = collectingIo();
870+
expect(await run(api, cfg, collected.io)).toBe(caseInsensitive ? 1 : 0);
871+
expect(parseYaml(readFileSync(join(cfg.snapshotDir, "alice", "R.yml"), "utf8"))).toEqual(
872+
doc(BUG),
873+
);
874+
if (caseInsensitive) {
875+
expect(collected.lines[1]).toEqual({
876+
level: "error",
877+
line: expect.stringMatching(
878+
/^bob\/r: cannot write the snapshot to .*: the filesystem carries it to the file this run already claimed for alice\/R\. /,
879+
),
880+
});
881+
expect(readdirSync(join(cfg.snapshotDir, "alice"))).toEqual(["R.yml"]);
882+
} else {
883+
expect(parseYaml(readFileSync(join(cfg.snapshotDir, "alice", "r.yml"), "utf8"))).toEqual(
884+
doc(DOCS),
885+
);
886+
}
887+
}));
888+
852889
test("writes one <owner>/<name>.yml per resolved target and publishes the per-target rollup", () =>
853890
withTempDir("snapshot-flow-", async (dir) => {
854891
const api = new MockApi({

0 commit comments

Comments
 (0)