Skip to content

Commit b153ba8

Browse files
authored
fix(process): honor cancellation at bounded file read completion (#8908)
1 parent b28b1e0 commit b153ba8

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎src/process/BoundedFileBytes.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const readBoundedFileBytes = async (
4141
expectedSize > maxBytes)
4242
)
4343
throw new RangeError("Expected file size must fit the byte limit");
44+
assertActive();
4445
if (expectedSize === undefined) {
4546
const chunks: Buffer[] = [];
4647
const buffer = Buffer.allocUnsafe(Math.min(64 * 1_024, maxBytes + 1));
@@ -52,6 +53,7 @@ export const readBoundedFileBytes = async (
5253
0,
5354
Math.min(buffer.length, maxBytes - total + 1),
5455
);
56+
assertActive();
5557
if (bytesRead === 0) return Buffer.concat(chunks, total);
5658
total += bytesRead;
5759
if (total > maxBytes) return undefined;
@@ -64,6 +66,7 @@ export const readBoundedFileBytes = async (
6466
for (;;) {
6567
assertActive();
6668
const { bytesRead } = await read(buffer, total, expectedSize - total);
69+
assertActive();
6770
if (bytesRead === 0) return buffer.subarray(0, total);
6871
total += bytesRead;
6972
if (total > maxBytes) return undefined;

‎tests/boundary/filesystem/boundedFileBytes.test.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe("bounded file byte reads", () => {
2626
},
2727
);
2828

29-
it.each([undefined, 5])(
29+
it.each([undefined, 0, 5])(
3030
"preserves raw reasons or caller-owned cancellation with expected size %s",
3131
async (expectedSize) => {
3232
const root = await createTestTempDirectory("rea-bounded-cancel-");
@@ -61,6 +61,48 @@ describe("bounded file byte reads", () => {
6161
},
6262
);
6363

64+
it.each([
65+
{ contents: "", maxBytes: 0, expectedSize: undefined, outcome: "EOF" },
66+
{ contents: "", maxBytes: 5, expectedSize: 5, outcome: "early EOF" },
67+
{
68+
contents: "hello",
69+
maxBytes: 5,
70+
expectedSize: 5,
71+
outcome: "expected size",
72+
},
73+
{
74+
contents: "hello",
75+
maxBytes: 4,
76+
expectedSize: undefined,
77+
outcome: "overflow",
78+
},
79+
])(
80+
"honors cancellation while the $outcome read is in flight",
81+
async ({ contents, maxBytes, expectedSize }) => {
82+
const root = await createTestTempDirectory("rea-bounded-cancel-read-");
83+
const path = join(root, "input");
84+
await writeFile(path, contents);
85+
const handle = await open(path, "r");
86+
const controller = new AbortController();
87+
const reason = new Error("cancelled during read");
88+
const typed = new Error("typed cancellation", { cause: reason });
89+
try {
90+
const result = readBoundedFileBytes(handle, maxBytes, {
91+
signal: controller.signal,
92+
expectedSize,
93+
cancellationFailure: (observed) => {
94+
expect(observed).toBe(reason);
95+
return typed;
96+
},
97+
});
98+
controller.abort(reason);
99+
await expect(result).rejects.toBe(typed);
100+
} finally {
101+
await handle.close();
102+
}
103+
},
104+
);
105+
64106
it("stops after one overflow byte when a file grows beyond its admitted size", async () => {
65107
const root = await createTestTempDirectory("rea-growing-bytes-");
66108
const path = join(root, "input");

0 commit comments

Comments
 (0)