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: track cppgc wrappers with CppgcWrapperList in Environment #56534

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

joyeecheung
Copy link
Member

@joyeecheung joyeecheung commented Jan 9, 2025

This allows us to perform cleanups of cppgc wrappers that rely on a living Environment during Environment shutdown. Otherwise the cleanup may happen during object destruction, which can be triggered by GC after Enivronment shutdown, leading to invalid access to Environment.

The general pattern for this type of non-trivial destruction is designed to be:

class MyWrap final : CPPGC_MIXIN(MyWrap) {
 public:
  ~MyWrap() { this->Clean(); }
  void CleanEnvResource(Environment* env) override {
     // Do cleanup that relies on a living Environemnt. This would be
     // called by CppgcMixin::Clean() first during Environment shutdown,
     // while the Environment is still alive. If the destructor calls
     // Clean() again later during garbage collection that happens after
     // Environment shutdown, CleanEnvResource() would be skipped, preventing
     // invalid access to the Environment.
  }
}

In addition, this allows us to trace external memory held by the wrappers in the heap snapshots if we add synthethic edges between the wrappers and other nodes in the embdder graph callback, or to perform snapshot serialization for them.

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Jan 9, 2025
@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

Copy link

codecov bot commented Jan 9, 2025

Codecov Report

Attention: Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.

Project coverage is 89.10%. Comparing base (19c8cc1) to head (195a9a4).
Report is 90 commits behind head on main.

Files with missing lines Patch % Lines
src/cppgc_helpers.h 88.88% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #56534      +/-   ##
==========================================
- Coverage   89.16%   89.10%   -0.07%     
==========================================
  Files         662      660       -2     
  Lines      191745   191328     -417     
  Branches    36902    36775     -127     
==========================================
- Hits       170971   170477     -494     
- Misses      13627    13782     +155     
+ Partials     7147     7069      -78     
Files with missing lines Coverage Δ
src/env.cc 85.76% <100.00%> (+0.09%) ⬆️
src/env.h 98.21% <100.00%> (+0.06%) ⬆️
src/cppgc_helpers.h 87.09% <88.88%> (+0.73%) ⬆️

... and 56 files with indirect coverage changes

src/README.md Outdated
the `Environment` is already gone, it must implement the cleanup with this pattern:

```c++
~MyWrap() { this->Clean(); }
Copy link
Member

@legendecas legendecas Jan 10, 2025

Choose a reason for hiding this comment

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

Assumptions should not be made about the order and the timing of their execution. There is no guarantee on the order in which the destructors are invoked. That's why destructors must not access any other on-heap objects (which might have already been destructed). If some destructor unavoidably needs to access other on-heap objects, it will have to be converted to a pre-finalizer. The pre-finalizer is allowed to access other on-heap objects.
https://github.com/v8/v8/tree/main/include/cppgc#sweeping-phase

This example should depend on CPPGC_USING_PRE_FINALIZER as it may access other heap objects.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated to mention CPPGC_USING_PRE_FINALIZER, though I noticed two things:

  1. ContextifyContext does not even need to have special cleanup and can just have a default destructor - when the destructors are invoked, the context is already going away as it would've been holding the ContextifyContext alive. So there's no need to set the internal pointers of the context; On the other hand since the context is already going away, the per-environment context list would now contain empty global handles to that context, and the context tracking code is already able to deal with empty handles, so it matters little to purge it from the list in the destructor (we could also do the housekeeping in the constructor, in TrackContext() instead, to make sure there aren't too many empty handles lying around).
  2. It seems the use of CPPGC_USING_PRE_FINALIZER would crash the construction of ContextifyContext, which I have not gotten to the bottom of yet.

This allows us to perform cleanups of cppgc wrappers that rely
on a living Environment during Environment shutdown. Otherwise
the cleanup may happen during object destruction, which can
be triggered by GC after Enivronment shutdown, leading to
invalid access to Environment.

The general pattern for this type of non-trivial destruction is
designed to be:

```
class MyWrap final : CPPGC_MIXIN(MyWrap) {
 public:
  ~MyWrap() { this->Clean(); }
  void CleanEnvResource(Environment* env) override {
     // Do cleanup that relies on a living Environemnt. This
     // would be called by CppgcMixin::Clean() first during
     // Environment shutdown, while the Environment is still
     // alive. If the destructor calls Clean() again later
     // during garbage collection that happens after
     // Environment shutdown, CleanEnvResource() would be
     // skipped, preventing invalid access to the Environment.
  }
}
```

In addition, this allows us to iterate over the wrappers to
trace external memory held by the wrappers in the heap snapshots
if we add synthethic edges between the wrappers and other
nodes in the embdder graph callback, or to perform snapshot
serialization for them.
Comment on lines +1243 to +1251
~MyWrap() { this->Clean(); }
void CleanEnvResource(Environment* env) override {
// Do cleanup that relies on a living Environemnt. This would be
// called by CppgcMixin::Clean() first during Environment shutdown,
// while the Environment is still alive. If the destructor calls
// Clean() again later during garbage collection that happens after
// Environment shutdown, CleanEnvResource() would be skipped, preventing
// invalid access to the Environment.
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm still in doubt if this example will entice people to access handles on the node::Environment given that it is passed in as a parameter, and ultimately call this CleanEnvResource through the destructor, which is invoked by cppgc.

I'm wondering if we should avoid creating a cppgc object that is short-lived than its associated env resources.

Copy link
Member Author

@joyeecheung joyeecheung Jan 21, 2025

Choose a reason for hiding this comment

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

Isn't the example in 3 enough to address this? I can remove Environment as a parameter though I don't think it makes much of a difference - people can still easily reach out to env() if they want, just like in the case of BaseObject's destructors, even if the destructors are called as the first pass weak callback.

Copy link
Member

Choose a reason for hiding this comment

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

Right, I think my comment was purely hypothetical.

@legendecas legendecas added the request-ci Add this label to start a Jenkins CI on a PR. label Jan 23, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 23, 2025
@nodejs-github-bot
Copy link
Collaborator

the cleanups of a cppgc-managed object should adhere to different patterns,
depending on what it needs to do:

1. If it does not need to do any non-trivial, nor does its members, just use
Copy link
Member

Choose a reason for hiding this comment

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

sentence appears to be incomplete? Do any non-trivial what?

1. If it does not need to do any non-trivial, nor does its members, just use
the default destructor.
2. If the cleanup relies on a living `Environment`, the class should use this
pattern in its class body:
Copy link
Member

Choose a reason for hiding this comment

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

This term "living Environment" is not defined. What is a living environment?

Copy link
Member

Choose a reason for hiding this comment

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

In this context, the term "living Enviroment" refers to a condition where a cppgc object can out-live its Environment as the cppgc heap is cleaned up after the Environment been destructed, as documented above:

When a cppgc-managed object is no longer reachable in the heap, its destructor
will be invoked by the garbage collection, which can happen after the Environment
is already gone.

Though, it didn't mention the term "living Environment".

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I know what was meant, but it should be explained a bit more in the document so that it is clear.

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++. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants