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 <DivyamTalwar@users.noreply.github.com>
This commit is contained in:
Divyam Talwar
2026-11-30 21:46:02 +08:00
committed by GitHub
co-authored by Divyam Talwar Divyam Talwar
parent 80c889cd9e
commit c90382e9c4
3 changed files with 46 additions and 10 deletions
+4 -4
View File
@@ -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.
+30
View File
@@ -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<void>((_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<void>((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 }),
+12 -6
View File
@@ -278,10 +278,10 @@ export class Job<T = void> {
*
* 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<T = void> {
) {
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<string, unknown>) => {
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) {