fix: prevent memory leaks from AbortController closures #12024
Merged
+184
−15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Inspired by https://x.com/jarredsumner/status/2017825694731145388. Definitely not expecting to see the same 1 gb saving but its at least more correct.
Arrow functions like
() => controller.abort()capture the entire lexical scope, including large request bodies. When attached to long-lived signals or timeouts, this memory cannot be reclaimed.Using
controller.abort.bind(controller)only retains a reference to the controller itself, allowing request bodies to be GC'd.How did you verify your code works?
see (AI generated)
abort-leak.test.ts. I am sure there's a better way to verify this fix, but I do not know what that way is.related issues
There are tons of memory leak issues: #9385
Relates to: #9385
(It might not actually fix that particular issue. But the contributing bot makes me put that there)