Merged
Conversation
7 tasks
mcollina approved these changes Aug 14, 2024
Member
There was a problem hiding this comment.
Great work. This confirms my suspects that the Response is actually not getting collected at the right moment, because the store is being kept alive. I'm trying to understand why that happens, but the fix is solid.
What puzzles me a bit is that over time, the memory consumption seems stable. I've not been able to generate a crash from this, so eventually things will get collected, but this particular loop of objects is pretty hard for the GC to track down.
mcollina pushed a commit that referenced this pull request Aug 17, 2024
* test: add test for memory leak * lint
Merged
Closed
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
@mcollina I was able to finally get a minimal reproduction without using next or react
https://github.com/snyamathi/undici-mem-leak
Looking at the chart, it does indeed appear to be a memory leak but it seems to be specific to Node's
AsyncLocalStoragewhich is something that I dug out of the React source code hereThis may actually have a root issue in Node itself which the team may want to be aware of - it seems like the reference prevents garbage collection so this same bug could bite others using
AsyncLocalStoragein the same way.The test is a little round-a-bout in its testing, but it tries to assert that the body is garbage collected by checking that the weak ref becomes
undefined.This test fails before the change that I put in, and only succeeds after.
I have the reproduction setup to test multiple versions of
undiciand show the problem does not exist in 6.15.0, does exist in 6.16.0 and is then fixed in 6.19.7 - check out the repo link above, but it looks like this:related to #3445