Skip to content

Calling clearImmediate on setTimeout return value can make future setTimeout callbacks never run #57063

Description

@190n

Version

v23.3.0

Platform

Darwin mac.local 24.1.0 Darwin Kernel Version 24.1.0: Thu Oct 10 21:05:23 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6031 arm64

Subsystem

timers

What steps will reproduce the bug?

Run the following script:

const t = setTimeout(() => {
  console.log("1");
}, 0);
clearImmediate(t);
await new Promise(resolve =>
  setTimeout(() => {
    console.log("2");
    resolve();
  }, 0),
);
await new Promise(resolve =>
  setTimeout(() => {
    console.log("3");
    resolve();
  }, 0),
);

How often does it reproduce? Is there a required condition?

Reproduces every time

What is the expected behavior? Why is that the expected behavior?

2
3

The first timeout should be cleared, and the second two should run because they were not cleared.

What do you see instead?

The last setTimeout callback never runs:

$ node example.mjs
2
Warning: Detected unsettled top-level await at file:///Users/ben/example.mjs:11
await new Promise(resolve =>
^
$

Additional information

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    timersIssues and PRs related to timers, setImmediate(), setInterval(), and setTimeout().

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions