Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difference from node in timers/promises setInterval #5021

Open
ThePotatoChronicler opened this issue Sep 12, 2023 · 3 comments
Open

Difference from node in timers/promises setInterval #5021

ThePotatoChronicler opened this issue Sep 12, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@ThePotatoChronicler
Copy link

ThePotatoChronicler commented Sep 12, 2023

What version of Bun is running?

1.0.0+822a00c4d508b54f650933a73ca5f4a3af9a7983

What platform is your computer?

Linux 6.5.2-arch1-1 x86_64 unknown

What steps can reproduce the bug?

Test code:

import { setInterval } from "timers/promises";

const controller = new AbortController();

setTimeout(() => {  
  controller.abort();
}, 200);

async function thing() {
  for await (const _nothing of setInterval(10, null, { signal: controller.signal })) {
  }
}

await thing();

Running with node:

$ node --version
v20.6.1
$ node main.mjs 
node:timers/promises:166
              new AbortError(undefined, { cause: signal.reason })));
              ^

AbortError: The operation was aborted
    at EventTarget.onCancel (node:timers/promises:166:15)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:807:20)
    at EventTarget.dispatchEvent (node:internal/event_target:742:26)
    at abortSignal (node:internal/abort_controller:369:10)
    at AbortController.abort (node:internal/abort_controller:391:5)
    at Timeout._onTimeout (file:///tmp/test/main.mjs:6:14)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7) {
  code: 'ABORT_ERR',
  [cause]: DOMException [AbortError]: This operation was aborted
      at new DOMException (node:internal/per_context/domexception:53:5)
      at AbortController.abort (node:internal/abort_controller:390:18)
      at Timeout._onTimeout (file:///tmp/test/main.mjs:6:14)
      at listOnTimeout (node:internal/timers:573:17)
      at process.processTimers (node:internal/timers:514:7)
}

Node.js v20.6.1

Running with bun, we see no output.

What is the expected behavior?

Program exits with an uncaught exception

What do you see instead?

NodeJS successfully throws an AbortError, Bun does not

Additional information

@smashah
Copy link

smashah commented Sep 15, 2023

I was getting this error but it is a side effect from something else. Run it in node and it should tell you what the actual problem is. Once I corrected the error reported from running in node then it started working ok in bun.

@ThePotatoChronicler
Copy link
Author

I was getting this error but it is a side effect from something else. Run it in node and it should tell you what the actual problem is. Once I corrected the error reported from running in node then it started working ok in bun.

You can see in the original message that I am running it with node, and there is a clear difference. NodeJS ends with an uncaught exception, Bun does not. Bun claims to be compatible with NodeJS, so this is clearly breaking behaviour.

@nigelnindodev
Copy link

@ThePotatoChronicler I think the PR title is misleading, because the issue doesn't seem to be with timers/promises.

I haven't used AbortController before, but that's seems to be where your issue lies. A few changes to your code:

import { setInterval } from "timers/promises";

const controller = new AbortController();

setTimeout(() => {  
  controller.abort();
}, 200);

async function thing() {
  for await (const _nothing of setInterval(10, null, { signal: controller.signal })) {
    console.log(controller.signal.aborted);
  }
}

await thing();
console.log(controller.signal.aborted);

You should get a series of false outputs then a final true output, indicating the signal was actually aborted.

Recommend you close this ticket and maybe submit a new bug report related to AbortController.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants