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.

Example migration that depends on this patch: #56522

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
// destructor. Outside of CleanEnvResource(), subclasses should avoid calling
// into JavaScript or perform any operation that can trigger garbage
// collection during the destruction.
void Clean() override {
Copy link
Member

@legendecas legendecas Jan 9, 2025

Choose a reason for hiding this comment

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

Looks like a pattern of Cleanable. Can this be merged into the Environment::cleanable_queue_?

node/src/env.h

Lines 606 to 616 in b8f6d84

class Cleanable {
public:
virtual ~Cleanable() = default;
protected:
ListNode<Cleanable> cleanable_queue_;
private:
virtual void Clean() = 0;
friend class Environment;
};

Copy link
Member Author

@joyeecheung joyeecheung Jan 9, 2025

Choose a reason for hiding this comment

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

That was what I did at first :) but then I realized if it's in that queue, to filter out all the wraps (for heap snapshots etc.) it would be an unnecessary hassle. Might as well just keep them in a separate queue and there would be no need for filtering.

@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.06%. Comparing base (7c3aa9f) to head (37e7bac).
Report is 20 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.12%   89.06%   -0.07%     
==========================================
  Files         662      662              
  Lines      191556   191670     +114     
  Branches    36860    36813      -47     
==========================================
- Hits       170732   170705      -27     
- Misses      13690    13808     +118     
- Partials     7134     7157      +23     
Files with missing lines Coverage Δ
src/env.cc 85.51% <100.00%> (-0.16%) ⬇️
src/env.h 98.21% <100.00%> (+0.06%) ⬆️
src/cppgc_helpers.h 87.09% <88.88%> (+0.73%) ⬆️

... and 54 files with indirect coverage changes

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.

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.

3 participants