Fix crash on aborted timer (#12348)

This commit is contained in:
Vadzim
2024-07-05 01:20:59 +02:00
committed by GitHub
parent fad58168d2
commit 4f3ef07455
2 changed files with 58 additions and 8 deletions

View File

@@ -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 = {}) {

View 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);
});
});