From c90382e9c4e30734afb99885add715a4c184e60e Mon Sep 17 00:00:00 2001 From: Divyam Talwar Date: Mon, 30 Nov 2026 19:16:02 +0530 Subject: [PATCH] fix(node): handle rejected job progress callbacks (#5272) Fixes #5271 ## Summary A promise returned from a job progress callback could reject outside the SDK's synchronous error handler. This calls the observer synchronously as before and attaches a rejection handler without awaiting it. ## Validation - The regression test holds the observer promise open until `Job.wait` completes, then rejects it and verifies the warning. - All 4 tests in `nodejs/__test__/job.test.ts` pass using the native job boundary stub and the published matching beta native declarations. - A separate actual Node run with `--unhandled-rejections=strict` exits at the observer error on main. It logs the error and continues with the patch. - Biome lint and format checks passed on the two changed files. `git diff --check` passed. --------- Co-authored-by: Divyam Talwar <237515611+DivyamTalwar@users.noreply.github.com> Co-authored-by: Divyam Talwar --- docs/src/js/classes/Job.md | 8 ++++---- nodejs/__test__/job.test.ts | 30 ++++++++++++++++++++++++++++++ nodejs/lancedb/job.ts | 18 ++++++++++++------ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/docs/src/js/classes/Job.md b/docs/src/js/classes/Job.md index 86c5abc1f..bc703b927 100644 --- a/docs/src/js/classes/Job.md +++ b/docs/src/js/classes/Job.md @@ -501,10 +501,10 @@ Wait until the operation reaches a terminal state. The optional callback observes the same snapshots fetched by the backend poller that drives completion; it does not start another poller. Without a -callback, this preserves the original wait behavior. The callback is a -synchronous observer: its return value is ignored and not awaited. If it -throws, the error is logged with `console.warn` and waiting continues; a -callback exception does not reject `wait`. +callback, this preserves the original wait behavior. The callback is +invoked synchronously and its return value is not awaited. If it throws +or returns a promise that rejects, the error is logged with +`console.warn` and waiting continues. Pass an options object to set a client-side timeout. Expiry rejects with an error and stops this wait, including pending status requests and backoff. diff --git a/nodejs/__test__/job.test.ts b/nodejs/__test__/job.test.ts index 31049f820..49f0d4248 100644 --- a/nodejs/__test__/job.test.ts +++ b/nodejs/__test__/job.test.ts @@ -34,6 +34,36 @@ describe("Job attempt contract", () => { expect(wait).toHaveBeenCalledWith(undefined, 1); expect(cancel).not.toHaveBeenCalled(); }); + + test("logs rejected progress observers without waiting for them", async () => { + const error = new Error("observer failed"); + let reject!: (reason: Error) => void; + const pending = new Promise((_resolve, rejectPromise) => { + reject = rejectPromise; + }); + // Keep Jest running on the old implementation so the missing warning + // is the regression assertion, not an unrelated unhandled rejection. + pending.catch(() => undefined); + const onProgress = jest.fn().mockReturnValue(pending); + const wait = jest.fn(async (observe: (progress: object) => void) => { + observe({ state: "in_progress" }); + }); + const job = new Job({ wait } as unknown as NativeJob); + const warn = jest + .spyOn(console, "warn") + .mockImplementation(() => undefined); + + try { + await expect(job.wait({ onProgress })).resolves.toBeUndefined(); + expect(onProgress).toHaveBeenCalledWith({ state: "in_progress" }); + reject(error); + await new Promise((resolve) => setImmediate(resolve)); + expect(warn).toHaveBeenCalledWith("Job progress callback threw:", error); + } finally { + warn.mockRestore(); + } + }); + test("retry retains the typed result decoder", async () => { const successor = { waitResult: jest.fn().mockResolvedValue({ value: 7 }), diff --git a/nodejs/lancedb/job.ts b/nodejs/lancedb/job.ts index a8298c8b9..1d3a9a822 100644 --- a/nodejs/lancedb/job.ts +++ b/nodejs/lancedb/job.ts @@ -278,10 +278,10 @@ export class Job { * * The optional callback observes the same snapshots fetched by the backend * poller that drives completion; it does not start another poller. Without a - * callback, this preserves the original wait behavior. The callback is a - * synchronous observer: its return value is ignored and not awaited. If it - * throws, the error is logged with `console.warn` and waiting continues; a - * callback exception does not reject `wait`. + * callback, this preserves the original wait behavior. The callback is + * invoked synchronously and its return value is not awaited. If it throws + * or returns a promise that rejects, the error is logged with + * `console.warn` and waiting continues. * * Pass an options object to set a client-side timeout. Expiry rejects with an * error and stops this wait, including pending status requests and backoff. @@ -323,14 +323,20 @@ export class Job { ) { throw new TypeError("onProgress must be a function"); } + const reportProgressError = (error: unknown) => { + console.warn("Job progress callback threw:", error); + }; const callback = progressCallback === undefined ? undefined : (progress: Record) => { try { - progressCallback(this.enrichProgress(progress)); + const result = progressCallback(this.enrichProgress(progress)); + if (result !== undefined) { + void Promise.resolve(result).catch(reportProgressError); + } } catch (error) { - console.warn("Job progress callback threw:", error); + reportProgressError(error); } }; if (this.resultDecoder !== undefined) {