-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
fs: do not throw exception after creating FSReqCallback #35487
Conversation
Once an `FSReqCallback` instance is created, it is a GC root until the underlying fs operation has completed, meaning that it cannot be garbage collected. This is a problem when the underlying operation never starts because an exception is thrown before that happens, for example as part of parameter validation. Instead, move all potentially throwing code before the `FSReqCallback` creation.
When a process exits cleanly, i.e. because the event loop ends up without things to wait for, the Node.js objects that are left on the heap should be: 1. weak, i.e. ready for garbage collection once no longer referenced, or 2. detached, i.e. scheduled for destruction once no longer referenced, or 3. an unrefed libuv handle, i.e. does not keep the event loop alive, or 4. an inactive libuv handle (essentially the same here) There are a few exceptions to this rule, but generally, if there are C++-backed Node.js objects on the heap that do not fall into the above categories, we may be looking at a potential memory leak. Most likely, the cause is a missing `MakeWeak()` call on the corresponding object. In order to avoid this kind of problem, we check the list of BaseObjects for these criteria. In this commit, we only do so when explicitly instructed to or when in debug mode (where --verify-base-objects is always-on). In particular, this avoids the kinds of memory leak issues that were fixed in the PRs referenced below. Refs: nodejs#35488 Refs: nodejs#35487 Refs: nodejs#35481
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we document this somehow? Otherwise next time someone may add a new method that does this again and we would have to catch it through code reviews
@joyeecheung I don’t really know where to document this, but #35490 would make CI fail if this happens again, so I think we’re okay there |
@addaleax Maybe at the top of the file? (I think when someone tries to add a new method they would probably look there). I imagine when the CI fails they would just see the callback being printed before exit which could be difficult for new comers to understand.. |
@joyeecheung I’ve added a comment. |
Commit Queue failed- Loading data for nodejs/node/pull/35487 ✔ Done loading data for nodejs/node/pull/35487 ----------------------------------- PR info ------------------------------------ Title fs: do not throw exception after creating FSReqCallback (#35487) ⚠ Could not retrieve the email or name of the PR author's from user's GitHub profile! Branch addaleax:fsreqcallback -> nodejs:master Labels fs, lts-watch-v12.x Commits 2 - fs: do not throw exception after creating FSReqCallback - fixup! fs: do not throw exception after creating FSReqCallback Committers 1 - Anna Henningsen PR-URL: https://github.com/nodejs/node/pull/35487 Reviewed-By: James M Snell Reviewed-By: Joyee Cheung ------------------------------ Generated metadata ------------------------------ PR-URL: https://github.com/nodejs/node/pull/35487 Reviewed-By: James M Snell Reviewed-By: Joyee Cheung -------------------------------------------------------------------------------- ⚠ Commits were pushed since the last review: ⚠ - fixup! fs: do not throw exception after creating FSReqCallback ✔ Last GitHub Actions successful ℹ Last Full PR CI on 2020-10-05T20:23:13Z: https://ci.nodejs.org/job/node-test-pull-request/33410/ - Querying data for job/node-test-pull-request/33410/ ✔ Build data downloaded ✔ Last Jenkins CI successful ℹ This PR was created on Sat, 03 Oct 2020 22:43:13 GMT ✔ Approvals: 2 ✔ - James M Snell (@jasnell) (TSC): https://github.com/nodejs/node/pull/35487#pullrequestreview-501578948 ✔ - Joyee Cheung (@joyeecheung) (TSC): https://github.com/nodejs/node/pull/35487#pullrequestreview-501630754 -------------------------------------------------------------------------------- ✔ Aborted `git node land` session in /home/runner/work/node/node/.ncu |
Once an `FSReqCallback` instance is created, it is a GC root until the underlying fs operation has completed, meaning that it cannot be garbage collected. This is a problem when the underlying operation never starts because an exception is thrown before that happens, for example as part of parameter validation. Instead, move all potentially throwing code before the `FSReqCallback` creation. PR-URL: #35487 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Landed in e817c36 |
Once an
FSReqCallback
instance is created, it is a GC root untilthe underlying fs operation has completed, meaning that it cannot
be garbage collected.
This is a problem when the underlying operation never starts
because an exception is thrown before that happens, for example
as part of parameter validation.
Instead, move all potentially throwing code before the
FSReqCallback
creation.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes