-
-
Notifications
You must be signed in to change notification settings - Fork 384
Fix memory leak caused by shared abort signals #683
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 memory leak caused by shared abort signals #683
Conversation
When using a shared `AbortSignal` across multiple requests, event listeners were being added but never removed. This resulted in memory leaks as each request's context remained in memory indefinitely. The fix uses `AbortSignal.any()` to properly combine signals without leaking memory, and eliminates a need for a manual event listener approach. Additionally, this already handles the case where a signal is already aborted, making the explicit `.aborted` check unnecessary.
756f1f2
to
1c199ad
Compare
Awesome! Thanks for writing a test. Is there a good way to detect the leak without importing a dependency? For example, if duplicate event listeners were being installed, maybe they could increment a counter in the test, which could be asserted at the end to have the expected count. Or if that doesn't work because the old listeners aren't triggered, then maybe we could directly count how many listeners there are with |
The logic for However, I'd recommend testing in this way instead of counting the number of event listeners, because otherwise you're testing the current cause of the leak instead of whether there is a leak or not, and (in an http client) you'll eventually get more bang-for-buck by having a generalisable way of unit testing for leaks. I also don't think the list of event listeners is generally exposed outside of Devtools, too. FWIW, I learnt the technique of testing for leaks from reading the unit tests of FYI, the leak itself is due to the event handler closing over everything passed into |
If the goal is to detect regressions for this specific issue, I think counting listeners would be perfectly fine. If the goal is to create a more general purpose system for detecting leaks throughout the Ky tests, then I agree that checking actual memory usage is much better. However, in that case, it would be preferable to implement it as a reusable utility, perhaps as a test macro so it can run assertions. |
I'll merge this now so we can get the fix out. It would be nice to have this refactored as a utility, though. |
When using a shared
AbortSignal
across multiple requests, eventlisteners were being added but never removed. This resulted in
memory leaks as each request's context remained in memory indefinitely.
The fix uses
AbortSignal.any()
to properly combine signals withoutleaking memory, and eliminates a need for a manual event listener
approach. Additionally, this already handles the case where a signal is
already aborted, making the explicit
.aborted
check unnecessary.