diff --git a/docs/src/js/interfaces/AddDataOptions.md b/docs/src/js/interfaces/AddDataOptions.md index 567269471..c69e4ad20 100644 --- a/docs/src/js/interfaces/AddDataOptions.md +++ b/docs/src/js/interfaces/AddDataOptions.md @@ -51,8 +51,9 @@ The callback is fired once per batch written and once more with asynchronously to the JS event loop and never block the write — a slow callback will queue events rather than back-pressure the writer. -Errors thrown from the callback are logged with `console.warn` and -swallowed — they do not abort the write. +If a callback throws or returns a rejected promise, the error is logged +with `console.warn`. The write neither awaits the callback nor fails +because of it. #### Parameters diff --git a/nodejs/__test__/table.test.ts b/nodejs/__test__/table.test.ts index e712b59e7..74ebdd853 100644 --- a/nodejs/__test__/table.test.ts +++ b/nodejs/__test__/table.test.ts @@ -393,6 +393,41 @@ describe.each(arrowVersions)( } }); + it("reports rejected progress promises without interrupting a completed append", async () => { + const error = new Error("write observer failed"); + let reject!: (reason: Error) => void; + const pending = new Promise((_resolve, rejectPromise) => { + reject = rejectPromise; + }); + // Prevent Jest from treating the baseline's escaped rejection as + // a harness error. The missing warning is the regression assertion. + pending.catch(() => undefined); + const progress = jest.fn( + (snapshot: import("../lancedb").WriteProgress) => + snapshot.done ? pending : undefined, + ); + const warn = jest + .spyOn(console, "warn") + .mockImplementation(() => undefined); + try { + const initialCount = await table.countRows(); + const result = await table.add([{ id: 1 }, { id: 2 }], { progress }); + expect(result.version).toBeGreaterThan(0); + expect(progress).toHaveBeenCalledWith( + expect.objectContaining({ done: true }), + ); + expect(await table.countRows()).toBe(initialCount + 2); + reject(error); + await new Promise((resolve) => setImmediate(resolve)); + expect(warn).toHaveBeenCalledWith( + "Table.add progress callback threw:", + error, + ); + } finally { + warn.mockRestore(); + } + }); + it("should let me close the table", async () => { expect(table.isOpen()).toBe(true); table.close(); diff --git a/nodejs/lancedb/table.ts b/nodejs/lancedb/table.ts index 5dc1c445e..7b6fe4efc 100644 --- a/nodejs/lancedb/table.ts +++ b/nodejs/lancedb/table.ts @@ -197,8 +197,9 @@ export interface AddDataOptions { * asynchronously to the JS event loop and never block the write — a slow * callback will queue events rather than back-pressure the writer. * - * Errors thrown from the callback are logged with `console.warn` and - * swallowed — they do not abort the write. + * If a callback throws or returns a rejected promise, the error is logged + * with `console.warn`. The write neither awaits the callback nor fails + * because of it. * * @example * ```ts @@ -1809,12 +1810,18 @@ export class LocalTable extends Table { // unhandled exception (the callback fires from a napi threadsafe // function — exceptions there crash the process). const userProgress = options?.progress; + const reportProgressError = (error: unknown) => { + console.warn("Table.add progress callback threw:", error); + }; const progress = userProgress ? (p: WriteProgress) => { try { - userProgress(p); + const result = userProgress(p); + if (result !== undefined) { + void Promise.resolve(result).catch(reportProgressError); + } } catch (e) { - console.warn("Table.add progress callback threw:", e); + reportProgressError(e); } } : undefined;