mirror of
https://github.com/morluto/rea.git
synced 2026-11-26 14:18:43 +00:00
fix(rizin): hold a hashed path until its stream closes
A hash released its path when the file's open returned. A read that a stalled mount then held kept its libuv worker after the digest timed out, while the next hash of the same path opened the file again, so repeated hashes could start stalled reads without bound. Keep each stream registered until it closes: a file stream closes only after its pending open or read returns, so a later hash of the path waits for it, and stalled opens and reads stay bounded by the paths.
This commit is contained in:
1 parent
a4e6d06bba
commit
13515107a7
3 files changed
+111
-70
No files matched your search
@@ -161,8 +161,9 @@ the version unknown, and Evidence or the launch failure keeps it as
|
||||
`provider_executable_path`. A lookup or hash that stops waiting for a stalled
|
||||
`stat` or `access` leaves that call running; a later lookup or hash of the same
|
||||
path waits for it instead of starting another. A hash also ends at the deadline
|
||||
or on cancellation while the file's open is still pending, and a later hash of
|
||||
the same path waits for that open to return before it opens the file again.
|
||||
or on cancellation while the file's open or a read is still pending, and a
|
||||
later hash of the same path waits until that earlier hash has closed the file
|
||||
before it opens the file again.
|
||||
|
||||
Captured stdout and stderr are retained up to a 16 MiB combined limit; `output_truncated` marks output beyond that limit. One-shot commands use the `command_timeout_ms` budget described above. That 16 MiB cap has no runtime override. A typed failure's `captured_output` marks a stream that held invalid UTF-8 with `stdout_invalid_utf8` or `stderr_invalid_utf8`.
|
||||
|
||||
|
||||
@@ -1480,12 +1480,14 @@ const reverseCommandTimeoutGuidance = (
|
||||
});
|
||||
|
||||
/**
|
||||
* Opens that have not returned, by stream factory and path. Destroying a
|
||||
* stream cannot cancel its open, which a stalled mount can hold, with a libuv
|
||||
* worker, indefinitely; a later hash of the same path waits for that open
|
||||
* instead of starting another, so stalled opens stay bounded by the paths.
|
||||
* Streams that have not closed, by stream factory and path. Destroying a
|
||||
* stream cancels neither its open nor a read in flight, which a stalled mount
|
||||
* can hold, with a libuv worker, indefinitely; the stream closes only after
|
||||
* that operation returns. A later hash of the same path waits for that close
|
||||
* instead of starting another, so stalled opens and reads stay bounded by the
|
||||
* paths.
|
||||
*/
|
||||
const unreturnedOpens = new WeakMap<object, Map<string, Promise<void>>>();
|
||||
const unclosedStreams = new WeakMap<object, Map<string, Promise<void>>>();
|
||||
|
||||
export const digestFile = async (
|
||||
path: string,
|
||||
@@ -1504,12 +1506,12 @@ export const digestFile = async (
|
||||
throw Object.assign(new Error("Artifact digest requires a regular file"), {
|
||||
code: "EINVAL",
|
||||
});
|
||||
const opens = unreturnedOpens.get(open) ?? new Map<string, Promise<void>>();
|
||||
unreturnedOpens.set(open, opens);
|
||||
const streams = unclosedStreams.get(open) ?? new Map<string, Promise<void>>();
|
||||
unclosedStreams.set(open, streams);
|
||||
for (
|
||||
let earlier = opens.get(path);
|
||||
let earlier = streams.get(path);
|
||||
earlier !== undefined;
|
||||
earlier = opens.get(path)
|
||||
earlier = streams.get(path)
|
||||
) {
|
||||
const pending = earlier;
|
||||
await settleWithDeadline(
|
||||
@@ -1527,19 +1529,18 @@ export const digestFile = async (
|
||||
return new Promise<string>((resolveDigest, rejectDigest) => {
|
||||
const hash = createHash("sha256");
|
||||
const stream = open(path);
|
||||
const returned = new Promise<void>((resolve) => {
|
||||
stream.once("open", () => resolve());
|
||||
const closed = new Promise<void>((resolve) => {
|
||||
stream.once("error", () => resolve());
|
||||
stream.once("close", () => resolve());
|
||||
});
|
||||
opens.set(path, returned);
|
||||
void returned.then(() => {
|
||||
if (opens.get(path) === returned) opens.delete(path);
|
||||
streams.set(path, closed);
|
||||
void closed.then(() => {
|
||||
if (streams.get(path) === closed) streams.delete(path);
|
||||
});
|
||||
// A stream destroyed while its open is pending reports the error only
|
||||
// after that open returns, which a stalled mount can delay indefinitely.
|
||||
// The deadline and cancellation therefore end the wait themselves; the
|
||||
// stream closes its file once the open returns.
|
||||
// A stream destroyed while its open or a read is pending reports the
|
||||
// error only after that operation returns, which a stalled mount can delay
|
||||
// indefinitely. The deadline and cancellation therefore end the wait
|
||||
// themselves; the stream closes its file once the operation returns.
|
||||
const stop = (cause: Error): void => {
|
||||
cleanup();
|
||||
stream.destroy(cause);
|
||||
|
||||
@@ -586,9 +586,28 @@ it("shares one preflight deadline across stat and digest", async () => {
|
||||
expect(performance.now() - started).toBeLessThan(280);
|
||||
});
|
||||
|
||||
/** Joins a destroyed stream's close within a bound. */
|
||||
const joinClose = async (stream: Readable): Promise<void> => {
|
||||
if (stream.closed) return;
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
try {
|
||||
await Promise.race([
|
||||
new Promise<void>((resolve) => stream.once("close", () => resolve())),
|
||||
new Promise<never>((_resolve, reject) => {
|
||||
timer = setTimeout(
|
||||
() => reject(new Error("the stalled stream did not close")),
|
||||
1_000,
|
||||
);
|
||||
}),
|
||||
]);
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* A stream whose open stalls, as on an unresponsive mount, until the test
|
||||
* returns it. `release` returns the open, even after a failed assertion, and
|
||||
* answers it. `release` answers the open, even after a failed assertion, and
|
||||
* joins the stream's close within a bound.
|
||||
*/
|
||||
const stalledOpen = () => {
|
||||
@@ -599,31 +618,45 @@ const stalledOpen = () => {
|
||||
},
|
||||
read() {},
|
||||
});
|
||||
const returnOpen = (): void => {
|
||||
const answer = (): void => {
|
||||
const callback = held;
|
||||
held = undefined;
|
||||
callback?.();
|
||||
};
|
||||
const release = async (): Promise<void> => {
|
||||
returnOpen();
|
||||
answer();
|
||||
stream.destroy();
|
||||
if (stream.closed) return;
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
try {
|
||||
await Promise.race([
|
||||
new Promise<void>((resolve) => stream.once("close", () => resolve())),
|
||||
new Promise<never>((_resolve, reject) => {
|
||||
timer = setTimeout(
|
||||
() => reject(new Error("the stalled open's stream did not close")),
|
||||
1_000,
|
||||
);
|
||||
}),
|
||||
]);
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
await joinClose(stream);
|
||||
};
|
||||
return { stream, returnOpen, release };
|
||||
return { stream, answer, release };
|
||||
};
|
||||
|
||||
/**
|
||||
* A stream that opens and then holds a read, as an unresponsive mount can,
|
||||
* until the test answers it. Like a file stream, it closes only after the read
|
||||
* in flight returns. `release` answers the read, even after a failed
|
||||
* assertion, and joins the stream's close within a bound.
|
||||
*/
|
||||
const stalledRead = () => {
|
||||
let held: (() => void) | undefined;
|
||||
const stream = new Readable({
|
||||
read() {},
|
||||
destroy(error, callback) {
|
||||
held = () => callback(error);
|
||||
},
|
||||
});
|
||||
queueMicrotask(() => stream.emit("open", 3));
|
||||
const answer = (): void => {
|
||||
const callback = held;
|
||||
held = undefined;
|
||||
callback?.();
|
||||
};
|
||||
const release = async (): Promise<void> => {
|
||||
stream.destroy();
|
||||
answer();
|
||||
await joinClose(stream);
|
||||
};
|
||||
return { stream, answer, release };
|
||||
};
|
||||
|
||||
it.each(["deadline", "cancellation"] as const)(
|
||||
@@ -668,38 +701,44 @@ it.each(["deadline", "cancellation"] as const)(
|
||||
},
|
||||
);
|
||||
|
||||
it("makes a hash wait for an earlier open of the same path that has not returned", async () => {
|
||||
const stat = async (): Promise<Stats> => ({ isFile: () => true }) as Stats;
|
||||
let stalled: ReturnType<typeof stalledOpen> | undefined;
|
||||
const opened: Readable[] = [];
|
||||
// The first open stalls until the test returns it; a later one reads "x".
|
||||
const open = () => {
|
||||
const stream =
|
||||
opened.length === 0
|
||||
? (stalled = stalledOpen()).stream
|
||||
: Readable.from([Buffer.from("x")]);
|
||||
opened.push(stream);
|
||||
return stream as unknown as ReturnType<
|
||||
typeof import("node:fs").createReadStream
|
||||
>;
|
||||
};
|
||||
const path = "/tmp/rea-unreturned-open";
|
||||
try {
|
||||
for (const _attempt of [1, 2])
|
||||
it.each([
|
||||
["open", stalledOpen],
|
||||
["read", stalledRead],
|
||||
] as const)(
|
||||
"makes a hash wait for an earlier %s of the same path that has not returned",
|
||||
async (operation, stall) => {
|
||||
const stat = async (): Promise<Stats> => ({ isFile: () => true }) as Stats;
|
||||
let stalled: ReturnType<typeof stall> | undefined;
|
||||
const opened: Readable[] = [];
|
||||
// The first stream stalls until the test answers it; a later one reads "x".
|
||||
const open = () => {
|
||||
const stream =
|
||||
opened.length === 0
|
||||
? (stalled = stall()).stream
|
||||
: Readable.from([Buffer.from("x")]);
|
||||
opened.push(stream);
|
||||
return stream as unknown as ReturnType<
|
||||
typeof import("node:fs").createReadStream
|
||||
>;
|
||||
};
|
||||
const path = `/tmp/rea-unreturned-${operation}`;
|
||||
try {
|
||||
for (const _attempt of [1, 2])
|
||||
await expect(
|
||||
digestFile(path, undefined, stat, 50, open),
|
||||
).rejects.toMatchObject({ code: "ETIMEDOUT" });
|
||||
// The second hash waited for the first stream instead of starting another.
|
||||
expect(opened).toHaveLength(1);
|
||||
stalled?.answer();
|
||||
await expect(
|
||||
digestFile(path, undefined, stat, 50, open),
|
||||
).rejects.toMatchObject({ code: "ETIMEDOUT" });
|
||||
// The second hash waited for the first open instead of starting another.
|
||||
expect(opened).toHaveLength(1);
|
||||
stalled?.returnOpen();
|
||||
await expect(digestFile(path, undefined, stat, 1_000, open)).resolves.toBe(
|
||||
createHash("sha256").update("x").digest("hex"),
|
||||
);
|
||||
expect(opened).toHaveLength(2);
|
||||
} finally {
|
||||
await stalled?.release();
|
||||
}
|
||||
});
|
||||
digestFile(path, undefined, stat, 1_000, open),
|
||||
).resolves.toBe(createHash("sha256").update("x").digest("hex"));
|
||||
expect(opened).toHaveLength(2);
|
||||
} finally {
|
||||
await stalled?.release();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it("shares a stalled stat between hashes of the same path", async () => {
|
||||
let stats = 0;
|
||||
|
||||
Reference in new issue
Block a user