Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

addaleax
Copy link
Member

@addaleax addaleax commented Oct 3, 2020

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.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines

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.
@nodejs-github-bot nodejs-github-bot added the fs Issues and PRs related to the fs subsystem / file system. label Oct 3, 2020
@addaleax addaleax added request-ci Add this label to start a Jenkins CI on a PR. lts-watch-v12.x labels Oct 3, 2020
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 3, 2020
@nodejs-github-bot
Copy link
Collaborator

addaleax added a commit to addaleax/node that referenced this pull request Oct 3, 2020
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
@nodejs-github-bot
Copy link
Collaborator

Copy link
Member

@joyeecheung joyeecheung left a 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

@addaleax
Copy link
Member Author

addaleax commented Oct 4, 2020

@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

@nodejs-github-bot
Copy link
Collaborator

@joyeecheung
Copy link
Member

@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..

@addaleax
Copy link
Member Author

addaleax commented Oct 4, 2020

@joyeecheung I’ve added a comment.

@addaleax addaleax added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 5, 2020
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 5, 2020
@nodejs-github-bot
Copy link
Collaborator

@addaleax addaleax added the commit-queue Add this label to land a pull request using GitHub Actions. label Oct 6, 2020
@github-actions github-actions bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Oct 6, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2020

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

@github-actions github-actions bot added the commit-queue-failed An error occurred while landing this pull request using GitHub Actions. label Oct 6, 2020
addaleax added a commit that referenced this pull request Oct 6, 2020
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>
@addaleax
Copy link
Member Author

addaleax commented Oct 6, 2020

Landed in e817c36

@addaleax addaleax closed this Oct 6, 2020
@addaleax addaleax deleted the fsreqcallback branch October 6, 2020 08:31
@danielleadams danielleadams mentioned this pull request Oct 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants