Skip to content

Commit

Permalink
Fix crash on aborted timer (oven-sh#12348)
Browse files Browse the repository at this point in the history
  • Loading branch information
vadzim authored Jul 4, 2024
1 parent fad5816 commit 4f3ef07
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/js/node/timers.promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ function setTimeoutPromise(after = 1, value, options = {}) {
signal.addEventListener("abort", onCancel);
}
});
if (typeof onCancel !== "undefined") {
returnValue.finally(() => signal.removeEventListener("abort", onCancel));
}
return returnValue;
return typeof onCancel !== "undefined"
? returnValue.finally(() => signal.removeEventListener("abort", onCancel))
: returnValue;
}

function setImmediatePromise(value, options = {}) {
Expand Down Expand Up @@ -124,10 +123,9 @@ function setImmediatePromise(value, options = {}) {
signal.addEventListener("abort", onCancel);
}
});
if (typeof onCancel !== "undefined") {
returnValue.finally(() => signal.removeEventListener("abort", onCancel));
}
return returnValue;
return typeof onCancel !== "undefined"
? returnValue.finally(() => signal.removeEventListener("abort", onCancel))
: returnValue;
}

function setIntervalPromise(after = 1, value, options = {}) {
Expand Down
52 changes: 52 additions & 0 deletions test/js/node/timers.promises/timers.promises.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { describe, test, it, expect } from "bun:test";
import { setTimeout, setImmediate } from "node:timers/promises";

describe("setTimeout", () => {
it("abort() does not emit global error", async () => {
let unhandledRejectionCaught = false;

const catchUnhandledRejection = () => {
unhandledRejectionCaught = true;
};
process.on('unhandledRejection', catchUnhandledRejection);

const c = new AbortController();

global.setTimeout(() => c.abort());

await setTimeout(100, undefined, { signal: c.signal }).catch(() => "aborted");

// let unhandledRejection to be fired
await setTimeout(100)

process.off('unhandledRejection', catchUnhandledRejection);

expect(c.signal.aborted).toBe(true);
expect(unhandledRejectionCaught).toBe(false);
});
});

describe("setImmediate", () => {
it("abort() does not emit global error", async () => {
let unhandledRejectionCaught = false;

const catchUnhandledRejection = () => {
unhandledRejectionCaught = true;
};
process.on('unhandledRejection', catchUnhandledRejection);

const c = new AbortController();

global.setImmediate(() => c.abort());

await setImmediate(undefined, { signal: c.signal }).catch(() => "aborted");

// let unhandledRejection to be fired
await setTimeout(100)

process.off('unhandledRejection', catchUnhandledRejection);

expect(c.signal.aborted).toBe(true);
expect(unhandledRejectionCaught).toBe(false);
});
});

0 comments on commit 4f3ef07

Please sign in to comment.