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

src: integrate ContextifyContext to cppgc #56522

Closed
wants to merge 4 commits into from

Conversation

joyeecheung
Copy link
Member

@joyeecheung joyeecheung commented Jan 8, 2025

This simplifies the memory management of ContextifyContext,
making all references visible to V8.

The destructors don't need to do anything because when the wrapper is
going away, the context is already going away or otherwise it would've
been holding the wrapper alive, so there's no need to reset the
pointers in the context. Also, any global handles to the context
would've been empty at this point, and the per-Environment context
tracking code is capable of dealing with empty handles from contexts
purged elsewhere.

To this end, the context tracking code also purges empty handles
from the list now, to prevent keeping too many empty handles around.

@joyeecheung joyeecheung marked this pull request as draft January 8, 2025 17:44
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Jan 8, 2025
Copy link

codecov bot commented Jan 8, 2025

Codecov Report

Attention: Patch coverage is 69.23077% with 8 lines in your changes missing coverage. Please review.

Project coverage is 89.20%. Comparing base (c8df98d) to head (b18f41a).
Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
src/node_contextify.cc 61.11% 7 Missing ⚠️
src/node_contextify.h 66.66% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #56522   +/-   ##
=======================================
  Coverage   89.20%   89.20%           
=======================================
  Files         662      662           
  Lines      191816   191818    +2     
  Branches    36926    36923    -3     
=======================================
+ Hits       171102   171111    +9     
+ Misses      13559    13555    -4     
+ Partials     7155     7152    -3     
Files with missing lines Coverage Δ
src/env.cc 85.87% <100.00%> (+0.12%) ⬆️
src/env.h 98.14% <ø> (ø)
src/node_contextify.h 86.66% <66.66%> (+13.33%) ⬆️
src/node_contextify.cc 81.37% <61.11%> (+<0.01%) ⬆️

... and 34 files with indirect coverage changes

@joyeecheung joyeecheung force-pushed the contextify-cppgc branch 2 times, most recently from 28b6b67 to 4cfe15f Compare January 9, 2025 16:49
@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Jan 9, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 9, 2025
@nodejs-github-bot
Copy link
Collaborator

@joyeecheung joyeecheung force-pushed the contextify-cppgc branch 3 times, most recently from bcb919d to a4ef690 Compare January 10, 2025 16:36
@joyeecheung joyeecheung marked this pull request as ready for review January 10, 2025 16:38
@joyeecheung joyeecheung changed the title [WIP] Migrate ContextifyContext to cppgc src: igrate ContextifyContext to cppgc Jan 10, 2025
@joyeecheung
Copy link
Member Author

joyeecheung commented Jan 10, 2025

After tracing the lifetime of ContextifyContexts a bit I realized that ContextifyContext does not even need to do any special cleanups, as the weak global handles in the context list would just become empty when the cycle involving Context/ContextifyContext is unreachable and will get purged sooner or later. So this no longer depend on #56534

@joyeecheung joyeecheung changed the title src: igrate ContextifyContext to cppgc src: integrate ContextifyContext to cppgc Jan 10, 2025
src/node_contextify.h Outdated Show resolved Hide resolved
src/node_contextify.cc Outdated Show resolved Hide resolved
@joyeecheung joyeecheung added request-ci Add this label to start a Jenkins CI on a PR. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Jan 13, 2025
Copy link
Member

@legendecas legendecas left a comment

Choose a reason for hiding this comment

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

The failed test-inspector-contexts seems to be persistent and could be related

@joyeecheung
Copy link
Member Author

That test seems to be doing something pretty unreliable - calling gc() in a loop until a context is collected. It should at least do that with setImmediate to free up the main thread, but we also already know from all the workarounds in test/common/gc that there is no reliable way to count on one particular object to be garbage collected (modulo taking a heap snapshot but that's a hack that I prefer not to abuse). We can see if using setImmediate helps and if not, try allocating a ton of contexts and see if any of them would be destroyed. If none of it helps stabilizing the test maybe we should just remove it or mark it as flaky...

@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Jan 14, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 14, 2025
@nodejs-github-bot

This comment was marked as outdated.

This simplifies the memory management of ContextifyContext,
making all references visible to V8.

The destructors don't need to do anything because when the wrapper is
going away, the context is already going away or otherwise it would've
been holding the wrapper alive, so there's no need to reset the
pointers in the context. Also, any global handles to the context
would've been empty at this point, and the per-Environment context
tracking code is capable of dealing with empty handles from contexts
purged elsewhere.

To this end, the context tracking code also purges empty handles
from the list now, to prevent keeping too many empty handles around.
@joyeecheung joyeecheung force-pushed the contextify-cppgc branch 2 times, most recently from 8b366bd to e67c128 Compare January 15, 2025 12:38
@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Jan 15, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 15, 2025
@nodejs-github-bot
Copy link
Collaborator

* The v8::TracedReference and the ContextEmbedderIndex::kContextifyContext
* slot in the context act as shortcuts from the node::ContextifyContext
* C++ object to the V8 context.
*/
Copy link
Member

Choose a reason for hiding this comment

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

Love the additional comment detail :-)

src/node_contextify.h Outdated Show resolved Hide resolved
@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Jan 15, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 15, 2025
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@joyeecheung joyeecheung added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-rebase Add this label to allow the Commit Queue to land a PR in several commits. labels Jan 17, 2025
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Jan 17, 2025
@nodejs-github-bot
Copy link
Collaborator

Landed in cee63dc...74717cb

nodejs-github-bot pushed a commit that referenced this pull request Jan 17, 2025
PR-URL: #56522
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
nodejs-github-bot pushed a commit that referenced this pull request Jan 17, 2025
This simplifies the memory management of ContextifyContext,
making all references visible to V8.

The destructors don't need to do anything because when the wrapper is
going away, the context is already going away or otherwise it would've
been holding the wrapper alive, so there's no need to reset the
pointers in the context. Also, any global handles to the context
would've been empty at this point, and the per-Environment context
tracking code is capable of dealing with empty handles from contexts
purged elsewhere.

To this end, the context tracking code also purges empty handles
from the list now, to prevent keeping too many empty handles around.

PR-URL: #56522
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. commit-queue-rebase Add this label to allow the Commit Queue to land a PR in several commits. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants