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
src: fix memory leak for v8.serialize #42695
src: fix memory leak for v8.serialize #42695
Conversation
When Buffer::New passes in existing data, it cannot be garbage collected in js synchronous execution. Fixes: nodejs#40828 Refs: nodejs#38300
24c5304
to
809386a
Compare
#40828 (comment) is the right comment here here: This is currently not working because the garbage collector does not get a chance to run, including in the test case here.
This change here is not a good solution. It adds an unnecessary buffer copy without real benefit.
If you do want to fix this in a better way, you’ll need to modify Buffer::New(Environment*,char*,size_t) to not call into Buffer::New(Environment*,char*,size_t,FreeCallback,void); instead.
The best way to fix this is to keep reminding users that Buffer GC may need to perform some cleanup asynchronously, and Buffer instances may be kept alive until synchronous code stops running.
|
Thanks for your review!
Although an extra memory copy is added, the test becomes faster, perhaps due to the reduced use of And it takes less memory and is more stable. In some cases, such as
Can you give me more help? I'd love to use this better method! I have tried modifying the And using
But this problem leads to bad performance and even crashes, and there is no good way to avoid it other than changing everything to async. |
The goal here would be to avoid So – the idea here is, instead of using
The really nice thing about this approach would be that it takes care of this problem in all situations in which
I assume that this would depend a bit on how much memory is allocated – longer buffers take longer to copy, but (typically) a constant amount of time to release.
Nice! To be clear, I would absolutely love to see this :) |
|
Thank you very much for your detailed help, I will try it! |
|
It seems that the test is so strict that it fails on jenkins, I changed it from less than 10mb increments to less than double. |
Commit Queue failed- Loading data for nodejs/node/pull/42695 ✔ Done loading data for nodejs/node/pull/42695 ----------------------------------- PR info ------------------------------------ Title src: fix memory leak for v8.serialize (#42695) ⚠ Could not retrieve the email or name of the PR author's from user's GitHub profile! Branch liuxingbaoyu:fix-buffer-callback -> nodejs:master Labels c++, author ready Commits 3 - src: fix memory leak for v8.serialize - use a better way - fix test Committers 1 - liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> PR-URL: https://github.com/nodejs/node/pull/42695 Fixes: https://github.com/nodejs/node/issues/40828 Refs: https://github.com/nodejs/node/issues/38300 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Minwoo Jung ------------------------------ Generated metadata ------------------------------ PR-URL: https://github.com/nodejs/node/pull/42695 Fixes: https://github.com/nodejs/node/issues/40828 Refs: https://github.com/nodejs/node/issues/38300 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Minwoo Jung -------------------------------------------------------------------------------- ℹ This PR was created on Mon, 11 Apr 2022 14:50:06 GMT ✔ Approvals: 3 ✔ - Anna Henningsen (@addaleax): https://github.com/nodejs/node/pull/42695#pullrequestreview-940855691 ✔ - James M Snell (@jasnell) (TSC): https://github.com/nodejs/node/pull/42695#pullrequestreview-941134072 ✔ - Minwoo Jung (@JungMinu): https://github.com/nodejs/node/pull/42695#pullrequestreview-941762487 ✔ Last GitHub CI successful ℹ Last Full PR CI on 2022-04-28T18:38:01Z: https://ci.nodejs.org/job/node-test-pull-request/43747/ - Querying data for job/node-test-pull-request/43747/ ✔ Last Jenkins CI successful -------------------------------------------------------------------------------- ✔ No git cherry-pick in progress ✔ No git am in progress ✔ No git rebase in progress -------------------------------------------------------------------------------- - Bringing origin/master up to date... From https://github.com/nodejs/node * branch master -> FETCH_HEAD ✔ origin/master is now up-to-date - Downloading patch for 42695 From https://github.com/nodejs/node * branch refs/pull/42695/merge -> FETCH_HEAD ✔ Fetched commits as 73a8a0ee3ceb..74a59561b032 -------------------------------------------------------------------------------- [master ece4132a09] src: fix memory leak for v8.serialize Author: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Tue Apr 12 02:16:57 2022 +0800 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-v8-serialize-leak.js [master 840fa5c1df] use a better way Author: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Wed Apr 13 19:37:11 2022 +0800 2 files changed, 15 insertions(+), 4 deletions(-) [master f5d4840fed] fix test Author: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Wed Apr 13 20:45:13 2022 +0800 1 file changed, 2 insertions(+), 2 deletions(-) ✔ Patches applied There are 3 commits in the PR. Attempting autorebase. Rebasing (2/6)https://github.com/nodejs/node/actions/runs/2259461302 |
|
Landed in ce29d28 |


Fixes: #40828
Refs: #38300