mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
Fix crash on aborted timer (#12348)
This commit is contained in:
@@ -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 = {}) {
|
||||
@@ -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 = {}) {
|
||||
|
||||
52
test/js/node/timers.promises/timers.promises.test.ts
Normal file
52
test/js/node/timers.promises/timers.promises.test.ts
Normal file
@@ -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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user