-
Notifications
You must be signed in to change notification settings - Fork 26
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
fix: Use an AbortSignal for per-request timeouts #96
Conversation
A potential fix for #63, largely inspired by a community member's PR that was never merged: #55 According to an Undici core committer in this comment elastic/elasticsearch-js#1716 (comment) the issue that triggers the MaxListenersExceededWarning, and possibly a memory leak in some cases, is caused by attaching an EventEmitter to each request by default when a per-request timeout is set, rather than attaching an AbortSignal. My assumption is that an EventEmitter was used because AbortSignal and AbortController were not added to Node.js until v14.17.0, so we couldn't guarantee v14 users would have it. I'm not certain why using EventEmitters makes a difference memory-wise, but it does get rid of the MaxListenersExceededWarning.
@ronag Since you were helpful in digging into this issue with your knowledge of Undici, I'd love a quick review from you if you have the time. 🙏 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM but I'm unsure why you don't just pass null if there is no signal?
Because the code implements per-request timeout functionality, so it needs a signal to manually abort the request when the timeout is reached. @delvedor discussed adding this to the Undici library nodejs/undici#165 but the functionality never shipped, so he added similar logic to do the same thing here. elastic-transport-js/src/connection/UndiciConnection.ts Lines 130 to 142 in b006b9a
|
Does it breaks code which uses emitter(which is removed)? |
The use of |
A potential fix for #63, largely inspired by a community member's PR that was never merged: #55
According to an Undici core committer in this comment the issue that triggers the
MaxListenersExceededWarning
, and possibly a memory leak in some cases, is caused by attaching anEventEmitter
to each request by default when a per-request timeout is set, rather than attaching anAbortSignal
.My assumption is that an
EventEmitter
was used becauseAbortSignal
andAbortController
were not added to Node.js until v14.17.0, so we couldn't guarantee v14 users would have it. I'm not certain why usingEventEmitters
makes a difference memory-wise, but it does get rid of theMaxListenersExceededWarning
.