Skip to content

Fix memory leak on context shutdown#1856

Merged
ianhattendorf merged 5 commits into
nodegit:masterfrom
AlexaXs:fix/memory-leak-when-closing-context
Oct 27, 2021
Merged

Fix memory leak on context shutdown#1856
ianhattendorf merged 5 commits into
nodegit:masterfrom
AlexaXs:fix/memory-leak-when-closing-context

Conversation

@AlexaXs

@AlexaXs AlexaXs commented Sep 8, 2021

Copy link
Copy Markdown
Contributor

There was a memory leak when v8 context is closed: the NodeGitWrapper objects created (which inherit from Nan::ObjectWrap) during the execution of NodeGit were not freed when for example a worker thread was terminated.

To fix it, a new class has been added from which NodeGitWrapper objects inherit, so that they can be tracked in the nodegit::Context and freed when its cleanup handle is triggered.

This new class allows for cheap tracking via Link/Unlink to a tracker list, allowing each NodeGitWrapper object to link/unlink itself while the normal GC is running, as well as for the nodegit::Context to keep a tracker list of them and free them in a 'owned objects first' way when closing.

@ianhattendorf ianhattendorf self-requested a review September 8, 2021 14:19
@ianhattendorf ianhattendorf self-assigned this Sep 8, 2021
@ianhattendorf

Copy link
Copy Markdown
Member

This should be rebased onto master for updated tests to run.

There was a memory leak when v8 context is closed: the Nan::ObjectWrap objects created inside nodegit were not freed, since their WeakCallback were not triggered.

To fix it, a new class has been added from which NodeGitWrapper objects inherit, so that they can be tracked in the nodegit::Context and freed when its cleanup handle is triggered.

This new class allows for cheap tracking via Link/Unlink to a tracker list, allowing each NodeGitWrapper object to link/unlink itself while the normal GC is running, as well as for the nodegit::Context to keep a tracker list of them and free them in a 'owned objects first' way when closing.

The ownership model and the fact that a NodeGitWrapper object might have multiple owners have also been considered.
- add code to get the number of tracked objects.
- export getNumberOfTrackedObjects for NodeGit addon.
so that they can be freed on context shutdown
@AlexaXs AlexaXs force-pushed the fix/memory-leak-when-closing-context branch from 1e28f2d to e3c8dde Compare October 26, 2021 16:25

@ianhattendorf ianhattendorf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great, just a few comments. Going to run some manual tests.

Comment thread generate/templates/manual/src/nodegit_wrapper.cc Outdated
Comment thread generate/templates/manual/src/nodegit_wrapper.cc Outdated
Comment thread generate/templates/manual/include/tracker_wrap.h
Comment thread generate/templates/manual/src/tracker_wrap.cc Outdated

@ianhattendorf ianhattendorf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local testing in Node and Electron checks out 👍

@ianhattendorf ianhattendorf merged commit fb0675f into nodegit:master Oct 27, 2021
@AlexaXs AlexaXs deleted the fix/memory-leak-when-closing-context branch October 28, 2021 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants