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

Excessive NodeError creation while using AbortSignal.timeout #4032

Open
slagiewka opened this issue Jan 28, 2025 · 3 comments
Open

Excessive NodeError creation while using AbortSignal.timeout #4032

slagiewka opened this issue Jan 28, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@slagiewka
Copy link

Bug Description

I'm not sure if this is something that undici should handle or something that can be patched in node.

We're using a timeout signal in every request made by fetch. Since the service is working as an API gateway, we do this A LOT.

It seems like this check is running unnecessary: AbortSignal has [kMaxEventTargetListeners] set to 0.

So the getMaxListeners does not check considers the AbortSignal to be an EventTarget because the condition:

else if (emitterOrTarget?.[kMaxEventTargetListeners])

evaluates to 0.

Should this not be surrounded by try-catch like is in undici this would result in:

TypeError [ERR_INVALID_ARG_TYPE]: The "emitter" argument must be an instance of EventEmitter or EventTarget. Received an instance of AbortSignal

Reproducible By

This should be as simple as running

import {fetch} from 'undici';

await fetch('https://nodejs.org', {signal: AbortSignal.timeout(1_000));

Expected Behavior

Less time spent creating invisible, unimportant errors 🙂

Logs & Screenshots

The offending tree
Image

getMaxListeners
Image

Expensive NodeError
Image

Callsite
Image

Environment

Node v22.13.1 (running inside Alpine on Docker)
Using undici v7.

Additional context

I see that there was this issue about the error being visible: nodejs/node#46823
Now it's just silent, but expensive.

@slagiewka slagiewka added the bug Something isn't working label Jan 28, 2025
@KhafraDev
Copy link
Member

KhafraDev commented Jan 28, 2025

It seems like this check is running unnecessary: AbortSignal has [kMaxEventTargetListeners] set to 0.

This was changed a month ago and is currently only available in node v22.13.0+ and v23.5.0+. If it could be backported to v20, we can consider removing it.

So the getMaxListeners does not check considers the AbortSignal to be an EventTarget because the condition:

This looks like a bug in node. The condition should be changed to else if (typeof emitterOrTarget?.[kMaxEventTargetListeners] === 'number') or similar.

@slagiewka
Copy link
Author

This was changed a month ago and is currently only available in node v22.13.0+ and v23.5.0+.

Correct. I just found that out right now, as I tried to reproduce the issue inside Fedora 41 which has 22.11. No issue there.

This looks like a bug in node. The condition should be changed to else if (typeof emitterOrTarget?.[kMaxEventTargetListeners] === 'number') or similar.

I agree. Seems like an honest mistake, since AbortSignal IS an EventTarget and it functions properly with setMaxListeners. I see no reason why it wouldn't pass that check and return the symbol.

@KhafraDev
Copy link
Member

I submitted a PR to fix it, where I realized I added getEventListeners and therefore this bug. Whoops...

nodejs-github-bot pushed a commit to nodejs/node that referenced this issue Jan 30, 2025
PR-URL: #56807
Refs: nodejs/undici#4032
Refs: c1ccade
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Juan José Arboleda <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Chemi Atlow <[email protected]>
targos pushed a commit to nodejs/node that referenced this issue Feb 2, 2025
PR-URL: #56807
Refs: nodejs/undici#4032
Refs: c1ccade
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Juan José Arboleda <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Chemi Atlow <[email protected]>
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

2 participants