From 0d55457d9e030c901da64fdd24d3c88ae255e06e Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Sat, 14 Nov 2026 20:57:49 +0800 Subject: [PATCH] test(node): make blob read cancellation deterministic (#5029) The Node blob cancellation test assumes a 32 MiB local read is still in flight when JavaScript immediately aborts. The native future can finish first, in which case returning its completed buffer is valid. Replace that timing assumption with the existing HTTP fixture: send one byte of a five-byte range response, hold back the remaining body, then abort. Verify `AbortError`, response disconnection, listener cleanup, and a subsequent read on the same handle with the single request permit. Existing local pre-abort, successful-read, cursor, and close coverage remains in place; production cancellation semantics are unchanged. Validated the 97 scoped Blob tests on Linux x64 / Node 22.23.4 with both a locally built beta18 binding and the [beta19 native binding built for #4967](https://github.com/lancedb/lancedb/actions/runs/43381450237/job/131739908253). The beta19 run used the published repair tests at `8096f848`, passed all 97 cases with exit 0, and verified the downloaded artifact digest, loaded binding hash, and native build checkout against #4967's source tree. The pre-existing `CustomGC` import diagnostic remains; the process exited normally. The [hosted macOS ARM64 job](https://github.com/lancedb/lancedb/actions/runs/43395339041/job/131784902005) also passed the cancellation regression and the complete applicable Jest suite: 1,695 tests passed and 22 were skipped on Node 24.22.2 / beta18. Its actual merge checkout `e906a2da` has matching test, native Blob, dependency, and workflow sources to PR head `8096f848`. A separate [macOS ARM64 / Node 22.23.4 / beta19 composition run](https://github.com/lancedb/lancedb/actions/runs/43559120836/job/132353156252) now passed 1,694 tests (23 skipped), including both in-flight cancellation and later-response-body cancellation/permit release. It tested the normal merge of #4967 `6b175764` and this repair `8096f848`, at validation head `144e5294` whose only subsequent change narrows the workflow and disables publication. The loaded native binding hash and downloaded artifact digest were captured. The original failed #4967 job is preserved; this separate validation does not replace normal prerequisite integration or current-branch CI on #4967. --- nodejs/__test__/blob_ranges.test.ts | 73 +++++++++++++++++++++++++---- nodejs/__test__/table.test.ts | 23 --------- 2 files changed, 64 insertions(+), 32 deletions(-) diff --git a/nodejs/__test__/blob_ranges.test.ts b/nodejs/__test__/blob_ranges.test.ts index 7d2fab566..9e7b0e53d 100644 --- a/nodejs/__test__/blob_ranges.test.ts +++ b/nodejs/__test__/blob_ranges.test.ts @@ -51,9 +51,10 @@ async function withRangeServer( run: (db: Connection, calls: Request[], paths: string[]) => Promise, fault?: string | number, capability: string | null = "1", - onStall?: () => void, + onStall?: (response: http.ServerResponse) => void, ) { let takes = 0; + let rangeReads = 0; const calls: Request[] = []; const paths: string[] = []; const server = http.createServer((req, res) => { @@ -91,13 +92,18 @@ async function withRangeServer( expect(url.searchParams.get("version")).toBe("3"); const [start, end] = req.headers.range!.slice(6).split("-").map(Number); const bytes = Buffer.from("alpha").subarray(start, end + 1); - res - .writeHead(206, { - "Content-Range": `bytes ${start}-${end}/5`, - "Content-Length": bytes.length, - "x-lancedb-version": "3", - }) - .end(bytes); + res.writeHead(206, { + "Content-Range": `bytes ${start}-${end}/5`, + "Content-Length": bytes.length, + "x-lancedb-version": "3", + }); + if (fault === "stall-read" && ++rangeReads === 1) { + // Withhold the rest of the body so native completion cannot win the abort. + res.write(bytes.subarray(0, 1)); + onStall?.(res); + return; + } + res.end(bytes); return; } expect(req.method).toBe("POST"); @@ -191,7 +197,7 @@ async function withRangeServer( "x-lancedb-query-snapshot": IDENTITY, }); res.flushHeaders(); - onStall?.(); + onStall?.(res); return; } if (typeof failure === "number") { @@ -327,6 +333,55 @@ describe("fetchBlobRanges", () => { }); }); + it("cancels an in-flight BlobFile read and leaves the handle usable", async () => { + let responseTimedOut = false; + let markStalled!: () => void; + const stalled = new Promise((resolve) => { + markStalled = resolve; + }); + let markDisconnected!: () => void; + const disconnected = new Promise((resolve) => { + markDisconnected = resolve; + }); + await withRangeServer( + async (db) => { + const table = await open(db, 3); + const [handle] = await table.fetchBlobFiles("image", [10n]); + const controller = new AbortController(); + try { + const pending = handle!.readRange(0n, handle!.size(), { + signal: controller.signal, + }); + const rejected = expect(pending).rejects.toMatchObject({ + name: "AbortError", + }); + await stalled; + controller.abort(); + await rejected; + await disconnected; + expect(responseTimedOut).toBe(false); + expect(getEventListeners(controller.signal, "abort")).toHaveLength(0); + expect(handle!.isClosed()).toBe(false); + expect(await handle!.tell()).toBe(0n); + // The single request permit must also be released by cancellation. + expect(await handle!.readRange(0n, 5n)).toEqual(Buffer.from("alpha")); + } finally { + controller.abort(); + await handle!.close(); + } + }, + "stall-read", + null, + (response) => { + response.once("timeout", () => { + responseTimedOut = true; + }); + response.once("close", markDisconnected); + markStalled(); + }, + ); + }); + it("cancels while waiting for a later response body and releases the only permit", async () => { let markStalled!: () => void; const stalled = new Promise((resolve) => { diff --git a/nodejs/__test__/table.test.ts b/nodejs/__test__/table.test.ts index 59d6929b4..2c45dbccd 100644 --- a/nodejs/__test__/table.test.ts +++ b/nodejs/__test__/table.test.ts @@ -3271,29 +3271,6 @@ describe("when dealing with blob columns", () => { expect(await handle!.read()).toEqual(alpha); }); - it("cancels an in-flight read", async () => { - const db = await connect(tmpDir.name); - const schema = new Schema([ - new Field("id", new Int64(), true), - blob("payload", { inlineSizeThreshold: 0, dedicatedSizeThreshold: 1 }), - ]); - const payload = Buffer.alloc(32 * 1024 * 1024, 7); - const table = await db.createTable("large_blobs", [{ id: 1n, payload }], { - schema, - }); - const [row] = await table.query().withRowId().toArray(); - const [handle] = await table.fetchBlobFiles("payload", [ - row._rowid as bigint, - ]); - const controller = new AbortController(); - const pending = handle!.readRange(0n, handle!.size(), { - signal: controller.signal, - }); - controller.abort(); - await expect(pending).rejects.toMatchObject({ name: "AbortError" }); - expect(await handle!.readRange(0n, 4n)).toEqual(payload.subarray(0, 4)); - }); - it("toArrow returns blob bytes with blobMode bytes", async () => { const { table, alpha, beta } = await openBlobTable(); const result = await table.query().toArrow({ blobMode: "bytes" });