-
Notifications
You must be signed in to change notification settings - Fork 28.4k
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
remove the event listener from the { signal } option after the request finishes #51429
Comments
you can increase the amount of listeners before getting a warning by using https://nodejs.org/api/events.html#eventssetmaxlistenersn-eventtargets |
The point of this bug report is that there should only be one listener, which should be released after |
I can reproduce on 18.13 but not on 18.18 meaning it probably has been fixed in the latest 18 version. (async () => {
const c = new AbortController();
for (let i = 0; i < 20; i++) {
await fetch("https://google.com", { signal: c.signal }).then(r => r.text()).then(r => console.log(r.substring(0, 100)));
console.log(process.memoryUsage().rss)
}
})()
|
I don't see how "that's why it's increasing the number of listeners" follows from "we can see the memory increasing each iteration". Sounds to me like a non sequitur. It's not a memory leak that's causing the increase of the number of listeners, but rather, the unlimited listeners generate a memory leak. So I'm not sure what you meant. But I have, indeed, tested with On the other hand, your example, if you remove the AbortController code and the signal code, the apparent memory leak is still there, so it seems to have nothing to do with signals or aborting. I've tried on v18.13.0 and v19.9.0. (async () => {
for (let i = 0; i < 20; i++) {
await fetch("https://google.com").then(r => r.text()).then(r => console.log(r.substring(0, 100)));
console.log(process.memoryUsage().rss)
}
})() Goes from |
The behavior of fetch is |
I'm sorry for not seeing the link earlier, but I've taken a look and I believe it refers exclusively to an "ongoing fetch", in other words a HTTP request that is still sending or receiving data through the TCP socket:
Whereas I am executing fetches all the way through, in other words I am Also, I'm not the one who keeps "adding listeners to the abort controller", it's the fetch() call who is adding listeners, which itself finishes and should be unobservable, or unreferenced. I just give it a reference to the signal. I'm going to close this particular issue because the warning no longer occurs in |
There is some more discussion here #46435 and https://undici.nodejs.org/#/?id=specification-compliance |
Version
v18.13.0
Platform
Linux vampi 6.1.0-16-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.67-1 (2023-12-12) x86_64 GNU/Linux
Subsystem
fetch
What steps will reproduce the bug?
When trying to reuse the same AbortSignal and AbortController, as the
{ signal }
option for fetch(), after about 10 requests, a warning is generatedHow often does it reproduce? Is there a required condition?
node --trace-warnings -e '(async() => { const c = new AbortController(); for(let i = 0; i < 20; i++) { await fetch("https://google.com", { signal: c.signal }).then(r => r.text()).then(r => console.log(r.substring(0, 100))); }})()'
What is the expected behavior? Why is that the expected behavior?
Expected behavior is that the AbortSignal event listener should be released with
.removeEventListener()
. This cannot be done outside of fetch() since there's no way to clear all the event listeners for an EventTarget, which AbortSignal inherits, or all the event listeners for a given event. One can only clear the event listener for a given event and callback function.In fact, if you paste the same code in the browser:
It works just fine and there is no warning.
What do you see instead?
MaxListenersExceededWarning: Possible EventTarget memory leak detected. 11 abort listeners added to [AbortSignal].
It adds multiple event listeners instead of a single one.
Additional information
I am not sure if this is a fetch() issue, an EventTarget issue (adding the same function with the same event multiple times), and I'm also not sure if this is the right repo to report this issue, so please correct me if I'm wrong.
The text was updated successfully, but these errors were encountered: