Skip to content

Commit

Permalink
fix(node/timers): error when passing id to clearTimeout/clearInterval (
Browse files Browse the repository at this point in the history
…#27130)

As pointed out in #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 #27126
  • Loading branch information
marvinhagemeister authored and bartlomieju committed Nov 28, 2024
1 parent 0ec2035 commit da64aa9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ext/node/polyfills/timers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function clearTimeout(timeout?: Timeout | number) {
const id = +timeout;
const timer = MapPrototypeGet(activeTimers, id);
if (timer) {
timeout._destroyed = true;
timer._destroyed = true;
MapPrototypeDelete(activeTimers, id);
}
clearTimeout_(id);
Expand All @@ -74,7 +74,7 @@ export function clearInterval(timeout?: Timeout | number | string) {
const id = +timeout;
const timer = MapPrototypeGet(activeTimers, id);
if (timer) {
timeout._destroyed = true;
timer._destroyed = true;
MapPrototypeDelete(activeTimers, id);
}
clearInterval_(id);
Expand Down
10 changes: 10 additions & 0 deletions tests/unit_node/timers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ Deno.test("[node/timers refresh cancelled timer]", () => {
p.refresh();
});

Deno.test("[node/timers] clearTimeout with number", () => {
const timer = +timers.setTimeout(() => fail(), 10);
timers.clearTimeout(timer);
});

Deno.test("[node/timers] clearInterval with number", () => {
const timer = +timers.setInterval(() => fail(), 10);
timers.clearInterval(timer);
});

Deno.test("[node/timers setImmediate returns Immediate object]", () => {
const { clearImmediate, setImmediate } = timers;

Expand Down

0 comments on commit da64aa9

Please sign in to comment.