Skip to content

Commit 39722f1

Browse files
fix(node/timers): error when passing id to clearTimeout/clearInterval (denoland#27130)
As pointed out in denoland#27126 we used a variable which could potentially be of type `number` instead of the `Timeout` class instance. Ensure that we're always setting `_destroyed` on the class instead instead. Fixes denoland#27126
1 parent 026bbc4 commit 39722f1

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

ext/node/polyfills/timers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function clearTimeout(timeout?: Timeout | number) {
5454
const id = +timeout;
5555
const timer = MapPrototypeGet(activeTimers, id);
5656
if (timer) {
57-
timeout._destroyed = true;
57+
timer._destroyed = true;
5858
MapPrototypeDelete(activeTimers, id);
5959
}
6060
clearTimeout_(id);
@@ -74,7 +74,7 @@ export function clearInterval(timeout?: Timeout | number | string) {
7474
const id = +timeout;
7575
const timer = MapPrototypeGet(activeTimers, id);
7676
if (timer) {
77-
timeout._destroyed = true;
77+
timer._destroyed = true;
7878
MapPrototypeDelete(activeTimers, id);
7979
}
8080
clearInterval_(id);

tests/unit_node/timers_test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ Deno.test("[node/timers refresh cancelled timer]", () => {
100100
p.refresh();
101101
});
102102

103+
Deno.test("[node/timers] clearTimeout with number", () => {
104+
const timer = +timers.setTimeout(() => fail(), 10);
105+
timers.clearTimeout(timer);
106+
});
107+
108+
Deno.test("[node/timers] clearInterval with number", () => {
109+
const timer = +timers.setInterval(() => fail(), 10);
110+
timers.clearInterval(timer);
111+
});
112+
103113
Deno.test("[node/timers setImmediate returns Immediate object]", () => {
104114
const { clearImmediate, setImmediate } = timers;
105115

0 commit comments

Comments
 (0)