Repository navigation
src: track cppgc wrappers with a list in Realm - #56534
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #56534 +/- ##
==========================================
- Coverage 90.14% 90.12% -0.03%
==========================================
Files 630 632 +2
Lines 186784 186905 +121
Branches 36653 36682 +29
==========================================
+ Hits 168377 168447 +70
- Misses 11205 11247 +42
- Partials 7202 7211 +9
🚀 New features to boost your workflow:
|
37e7bac to
195a9a4
Compare
195a9a4 to
9b53840
Compare
|
Trying to rebase #51017 on top of it to verify the "use list for heap snapshot integration" idea. It seems we'll need to update the MemoryTracker code to stop creating duplicate nodes for cppgc wrappers (either that, or use something different for the cppgc wrapper list). Still looking into it.. |
ed3648a to
036116a
Compare
There was a problem hiding this comment.
Some of these rules may be non-obvious to many developers. For example, let's say someone has a wrapped object that is holding a BaseObjectPtr<Foo> as a member... Just based on reading this documentation, as a newcomer I would not know which of these conditions may apply. Do I need to clear the BaseObjectPtr in the CleanEnvResource callback? What about if it has a v8::Global<...> member field? Is it ok to just let that be cleared by the default destructor? etc. I think calling out a few more detailed examples (and not partial examples like you have currently) of each case would be most helpful.
There was a problem hiding this comment.
let's say someone has a wrapped object that is holding a
BaseObjectPtr<Foo>as a member
I don't think this would be supported in the nearest future, or will ever be supported - a cppgc-object should not hold on to a BaseObjectPtr<Foo> without converting that BaseObjectPtr<Foo> to cppgc-managed first - and then in that case they should use cppgc::Member<Foo> to hold on to that instead. The migration needs to be done bottom-to-top. In the case of using cppgc::Member<Foo>, nothing special needs to be done in the destructor, V8 will handle that properly.
What about if it has a v8::Global<...> member field?
I think in most cases, this should be v8::TracedReference instead. In that case nothing special needs to be done in the destructor. This has been documented in the Creating C++ to JavaScript references in cppgc-managed objects section.
There was a problem hiding this comment.
Added a note about not supporting cross BaseObject and cppgc-managed wrapeprs. I don't think it's impossible but someone needs to implement some helpers for that first (I am not 100% sure what needs to be done, but using cppgc::Persistent would be a start).
There was a problem hiding this comment.
I wonder if we can somehow have c++ linting detect it for us if someone includes a BaseObjectPtr member in one of these? .... in any case, this all sounds fine, I'd just like to make sure this gets included somehow in the documentation.
There was a problem hiding this comment.
If it comes up in the future that some people attempt to use BaseObjectPtr in a cppgc wrapper, we can consider adding some linting rules. For now it seems a bit too premature. Since we are still in an early phase of cppgc migration, I think the rule of thumb is probably:
- If you are not looking into also updating the cppgc helpers, only use cppgc on classes that aren't more than what existing cppgc wrappers are doing (at this point in time, v8 handles and MemoryRetainer over other plain types are supported, cppgc members are supposed to be supported by we haven't tried it internally yet)
- If you want to tread on untrodden paths and manage more complex memory structures, like a cppgc wrapper referencing BaseObject, expect to figure out how to make it work and update the cppgc helpers yourself.
909a80a to
6fd34b0
Compare
|
I did a few updates to the design:
|
Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com>
Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com>
Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com>
Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com>
Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com>
Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com>
Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com>
Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com>
Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com>
Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com>
Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com>
Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com>
Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com>
Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com>
* chore: bump node in DEPS to v24.19.0 * chore: remove upstreamed patch Ref: nodejs/node#64053 Co-Authored-By: GitHub Copilot <copilot@github.com> * chore: update patches (trivial only) Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): add perfetto trace agent Backport Node's Perfetto tracing agent now that Chromium V8 no longer exposes the legacy tracing controller APIs. Ref: nodejs/node#64565 Ref: nodejs/node#64721 Co-Authored-By: GitHub Copilot <copilot@github.com> * node#64565: src: rename legacy trace event headers Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): sample delay per event loop iteration Ref: nodejs/node#62935 Ref: nodejs/node#64480 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): avoid redundant std::get_if<>() call Ref: nodejs/node#64094 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): add perfetto trace agent Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * node#64053: src: abstract tracing agent for legacy and perfetto Ref: nodejs/node#64053 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): add perfetto trace agent Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * node#64053: src: abstract tracing agent for legacy and perfetto Ref: nodejs/node#64053 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): support native IPC serialization in V8 sandbox Ref: nodejs/node#63933 Co-Authored-By: GitHub Copilot <copilot@github.com> * test: hide output package.json in node spec runner Virtual CommonJS files rooted at process.execPath inherit the output directory's type=module package unless the runner hides it alongside Chromium's root package. Ref: nodejs/node#44713 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): account for libc++ rm error on Electron Linux Node's fs.rmSync() delegates recursive deletion to std::filesystem::remove_all(). Electron's Linux build uses libc++, which reports ENOTEMPTY here while Node's libstdc++ build reports EACCES. Ref: nodejs/node#57103 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): mark worker stack-size test flaky Ref: nodejs/node#33085 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): restore user timing trace events Ref: #50591 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): own fallback tracing controller Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): release cppgc wrapper tracking nodes Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com> * test: extend cpp heap remote app timeout The ChunkedDataPipeReadableStream liveness test can exceed the remote fixture's 30-second watchdog under Linux ASAN, which disconnects the control socket before assertions run. Ref: #52447 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): remove unused legacy tracing backend Electron builds Node exclusively with Perfetto since #50591. Wire the upstream Perfetto agent directly and drop the legacy header split and dual-backend source lists. Ref: #50591 Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * docs: better explanation of test_account_for_libc_rm_error_on_electron_linux.patch * refactor: node tracing agent and track event registration (#52678) * fix: register Node tracing in utility processes The Node upgrade moved tracing setup out of JavascriptEnvironment, but the utility process did not adopt the explicit registration used by other embedded Node hosts. Register its TrackEvent data source with Chromium before creating the isolate. Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * chore: update patches (trivial only) * chore: update patches * Revert "fix(patch): mark worker stack-size test flaky" This reverts commit f2e5cb5. * fixup! support native IPC serialization in V8 sandbox * chore: address review feedback * fixup! support native IPC serialization in V8 sandbox Allocate ValueSerializer buffers as V8 backing stores and retain the original backing store when adopting the released buffer. This preserves the shared allocator lifetime when serialized buffers are transferred from a worker and outlive its isolate. * fixup! refactor: node tracing agent and track event registration --------- Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr <charles@charleskerr.com> Co-authored-by: GitHub Copilot <copilot@github.com> Co-authored-by: Robo <hop2deep@gmail.com> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com>
* chore: bump node in DEPS to v24.19.0 * chore: remove upstreamed patch Ref: nodejs/node#64053 Co-Authored-By: GitHub Copilot <copilot@github.com> * chore: update patches (trivial only) * fix(patch): add perfetto trace agent Backport Node's Perfetto tracing agent now that Chromium V8 no longer exposes the legacy tracing controller APIs. Ref: nodejs/node#64565 Ref: nodejs/node#64721 Co-Authored-By: GitHub Copilot <copilot@github.com> * node#64565: src: rename legacy trace event headers Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): sample delay per event loop iteration Ref: nodejs/node#62935 Ref: nodejs/node#64480 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): avoid redundant std::get_if<>() call Ref: nodejs/node#64094 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): add perfetto trace agent Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * node#64053: src: abstract tracing agent for legacy and perfetto Ref: nodejs/node#64053 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): add perfetto trace agent Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * node#64053: src: abstract tracing agent for legacy and perfetto Ref: nodejs/node#64053 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): support native IPC serialization in V8 sandbox Ref: nodejs/node#63933 Co-Authored-By: GitHub Copilot <copilot@github.com> * test: hide output package.json in node spec runner Virtual CommonJS files rooted at process.execPath inherit the output directory's type=module package unless the runner hides it alongside Chromium's root package. Ref: nodejs/node#44713 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): account for libc++ rm error on Electron Linux Node's fs.rmSync() delegates recursive deletion to std::filesystem::remove_all(). Electron's Linux build uses libc++, which reports ENOTEMPTY here while Node's libstdc++ build reports EACCES. Ref: nodejs/node#57103 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): mark worker stack-size test flaky Ref: nodejs/node#33085 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): restore user timing trace events Ref: #50591 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): own fallback tracing controller Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): release cppgc wrapper tracking nodes Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): remove unused legacy tracing backend Electron builds Node exclusively with Perfetto since #50591. Wire the upstream Perfetto agent directly and drop the legacy header split and dual-backend source lists. Ref: #50591 Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * docs: better explanation of test_account_for_libc_rm_error_on_electron_linux.patch * refactor: node tracing agent and track event registration (#52678) * fix: register Node tracing in utility processes The Node upgrade moved tracing setup out of JavascriptEnvironment, but the utility process did not adopt the explicit registration used by other embedded Node hosts. Register its TrackEvent data source with Chromium before creating the isolate. Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * chore: update patches (trivial only) * chore: update patches (trivial only) Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): skip unsupported WebCrypto fixtures with BoringSSL Ref: nodejs/node#63520 Co-Authored-By: GitHub Copilot <copilot@github.com> * Revert "fix(patch): mark worker stack-size test flaky" This reverts commit f2e5cb5. * chore: address review feedback (cherry picked from commit b8f475c) * fixup! refactor: node tracing agent and track event registration (cherry picked from commit d898f33) --------- Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr <charles@charleskerr.com> Co-authored-by: GitHub Copilot <copilot@github.com> Co-authored-by: Robo <hop2deep@gmail.com> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
* chore: bump node in DEPS to v24.19.0 * chore: remove upstreamed patch Ref: nodejs/node#64053 Co-Authored-By: GitHub Copilot <copilot@github.com> * chore: update patches (trivial only) * fix(patch): add perfetto trace agent Backport Node's Perfetto tracing agent now that Chromium V8 no longer exposes the legacy tracing controller APIs. Ref: nodejs/node#64565 Ref: nodejs/node#64721 Co-Authored-By: GitHub Copilot <copilot@github.com> * node#64565: src: rename legacy trace event headers Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): sample delay per event loop iteration Ref: nodejs/node#62935 Ref: nodejs/node#64480 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): avoid redundant std::get_if<>() call Ref: nodejs/node#64094 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): add perfetto trace agent Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * node#64053: src: abstract tracing agent for legacy and perfetto Ref: nodejs/node#64053 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): add perfetto trace agent Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * node#64053: src: abstract tracing agent for legacy and perfetto Ref: nodejs/node#64053 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): support native IPC serialization in V8 sandbox Ref: nodejs/node#63933 Co-Authored-By: GitHub Copilot <copilot@github.com> * test: hide output package.json in node spec runner Virtual CommonJS files rooted at process.execPath inherit the output directory's type=module package unless the runner hides it alongside Chromium's root package. Ref: nodejs/node#44713 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): account for libc++ rm error on Electron Linux Node's fs.rmSync() delegates recursive deletion to std::filesystem::remove_all(). Electron's Linux build uses libc++, which reports ENOTEMPTY here while Node's libstdc++ build reports EACCES. Ref: nodejs/node#57103 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): mark worker stack-size test flaky Ref: nodejs/node#33085 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): restore user timing trace events Ref: #50591 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): own fallback tracing controller Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): release cppgc wrapper tracking nodes Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com> * test: extend cpp heap remote app timeout The ChunkedDataPipeReadableStream liveness test can exceed the remote fixture's 30-second watchdog under Linux ASAN, which disconnects the control socket before assertions run. Ref: #52447 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): remove unused legacy tracing backend Electron builds Node exclusively with Perfetto since #50591. Wire the upstream Perfetto agent directly and drop the legacy header split and dual-backend source lists. Ref: #50591 Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * docs: better explanation of test_account_for_libc_rm_error_on_electron_linux.patch * refactor: node tracing agent and track event registration (#52678) * fix: register Node tracing in utility processes The Node upgrade moved tracing setup out of JavascriptEnvironment, but the utility process did not adopt the explicit registration used by other embedded Node hosts. Register its TrackEvent data source with Chromium before creating the isolate. Co-Authored-By: John Kleinschmidt <jkleinsc@electronjs.org> * chore: update patches (trivial only) * chore: update patches (trivial only) Co-Authored-By: GitHub Copilot <copilot@github.com> * Revert "fix(patch): mark worker stack-size test flaky" This reverts commit f2e5cb5. * fixup! support native IPC serialization in V8 sandbox * chore: address review feedback (cherry picked from commit b8f475c) * fixup! support native IPC serialization in V8 sandbox Allocate ValueSerializer buffers as V8 backing stores and retain the original backing store when adopting the released buffer. This preserves the shared allocator lifetime when serialized buffers are transferred from a worker and outlive its isolate. (cherry picked from commit f35b26d) * fixup! refactor: node tracing agent and track event registration (cherry picked from commit d898f33) --------- Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr <charles@charleskerr.com> Co-authored-by: GitHub Copilot <copilot@github.com> Co-authored-by: Robo <hop2deep@gmail.com> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
* chore: bump node in DEPS to v24.19.0 * chore: remove upstreamed patch Ref: nodejs/node#64053 Co-Authored-By: GitHub Copilot <copilot@github.com> * chore: update patches (trivial only) Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): add perfetto trace agent Backport Node's Perfetto tracing agent now that Chromium V8 no longer exposes the legacy tracing controller APIs. Ref: nodejs/node#64565 Ref: nodejs/node#64721 Co-Authored-By: GitHub Copilot <copilot@github.com> * node#64565: src: rename legacy trace event headers Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): sample delay per event loop iteration Ref: nodejs/node#62935 Ref: nodejs/node#64480 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): avoid redundant std::get_if<>() call Ref: nodejs/node#64094 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): add perfetto trace agent Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * node#64053: src: abstract tracing agent for legacy and perfetto Ref: nodejs/node#64053 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): add perfetto trace agent Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * node#64053: src: abstract tracing agent for legacy and perfetto Ref: nodejs/node#64053 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): support native IPC serialization in V8 sandbox Ref: nodejs/node#63933 Co-Authored-By: GitHub Copilot <copilot@github.com> * test: hide output package.json in node spec runner Virtual CommonJS files rooted at process.execPath inherit the output directory's type=module package unless the runner hides it alongside Chromium's root package. Ref: nodejs/node#44713 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): account for libc++ rm error on Electron Linux Node's fs.rmSync() delegates recursive deletion to std::filesystem::remove_all(). Electron's Linux build uses libc++, which reports ENOTEMPTY here while Node's libstdc++ build reports EACCES. Ref: nodejs/node#57103 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): mark worker stack-size test flaky Ref: nodejs/node#33085 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): restore user timing trace events Ref: #50591 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): own fallback tracing controller Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): release cppgc wrapper tracking nodes Ref: nodejs/node#56534 Co-Authored-By: GitHub Copilot <copilot@github.com> * test: extend cpp heap remote app timeout The ChunkedDataPipeReadableStream liveness test can exceed the remote fixture's 30-second watchdog under Linux ASAN, which disconnects the control socket before assertions run. Ref: #52447 Co-Authored-By: GitHub Copilot <copilot@github.com> * fix(patch): remove unused legacy tracing backend Electron builds Node exclusively with Perfetto since #50591. Wire the upstream Perfetto agent directly and drop the legacy header split and dual-backend source lists. Ref: #50591 Ref: nodejs/node#64565 Co-Authored-By: GitHub Copilot <copilot@github.com> * docs: better explanation of test_account_for_libc_rm_error_on_electron_linux.patch * refactor: node tracing agent and track event registration (#52678) * fix: register Node tracing in utility processes The Node upgrade moved tracing setup out of JavascriptEnvironment, but the utility process did not adopt the explicit registration used by other embedded Node hosts. Register its TrackEvent data source with Chromium before creating the isolate. Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> * chore: update patches (trivial only) * chore: update patches (trivial only) Co-Authored-By: GitHub Copilot <copilot@github.com> * Revert "fix(patch): mark worker stack-size test flaky" This reverts commit f2e5cb5. * fixup! support native IPC serialization in V8 sandbox * chore: update patch * chore: address review feedback (cherry picked from commit b8f475c) * fixup! support native IPC serialization in V8 sandbox Allocate ValueSerializer buffers as V8 backing stores and retain the original backing store when adopting the released buffer. This preserves the shared allocator lifetime when serialized buffers are transferred from a worker and outlive its isolate. (cherry picked from commit f35b26d) * fixup! refactor: node tracing agent and track event registration (cherry picked from commit d898f33) --------- Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr <charles@charleskerr.com> Co-authored-by: GitHub Copilot <copilot@github.com> Co-authored-by: Robo <hop2deep@gmail.com> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
`Realm::RunCleanup()` finalizes the cppgc-managed wrappers it tracks so that none of them touches the Realm once it is gone, but it reaches them through weak persistents, and the GC clears those as soon as it finds a wrapper dead. With lazy and concurrent sweeping the destructor can run much later, so a wrapper collected shortly before `FreeEnvironment()` and swept after it was skipped by the cleanup and kept its `realm_`: `~CppgcMixin()` then wrote `should_purge_empty_cppgc_wrappers_` into the freed Realm, and a subclass destructor calling `Finalize()` as documented would have called `Clean()` with a dangling Realm. A Worker that compiles a few `vm.Script`s, gets a full GC from external memory pressure and calls `process.exit()` is enough to hit the first case. Move the Realm pointer into the list node, which the wrapper now owns and deletes in its destructor. `CppgcWrapperList::Cleanup()` unlinks every node, finalizing the wrappers that are still alive and clearing the Realm pointer for the collected ones, which only their own destructor may still touch. `Realm::PendingCleanup()` accounts for the list so it is always drained. The purge flag, its GC epilogue callback and `PurgeEmpty()` are no longer needed, and removing them also stops the list nodes of wrappers that are alive at `FreeEnvironment()` from leaking. Refs: nodejs#56534 Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
`MemoryTracker::AddNode(const CppgcMixin*)` allocates a `MemoryRetainerNode` that only stands in for the wrapper's JS node while its `MemoryInfo()` runs; unlike the other node kinds it is not handed to the `EmbedderGraph`, and nothing freed it. Every heap snapshot (or other `BuildEmbedderGraph` call) leaked one node per live `vm.Script` or `vm` context. Keep the placeholders in the tracker and free them with it, and make the already-seen path in `Track(const CppgcMixin*)` add its edge to the wrapper's JS node like the first visit does, so the graph never refers to a placeholder. Refs: nodejs#56534 Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
`MemoryTracker::AddNode(const CppgcMixin*)` allocates a `MemoryRetainerNode` that only stands in for the wrapper's JS node while its `MemoryInfo()` runs; unlike the other node kinds it is not handed to the `EmbedderGraph`, and nothing freed it. Every heap snapshot (or other `BuildEmbedderGraph` call) leaked one node per live `vm.Script` or `vm` context. Keep the placeholders in the tracker and free them with it, and make the already-seen path in `Track(const CppgcMixin*)` add its edge to the wrapper's JS node like the first visit does, so the graph never refers to a placeholder. Refs: nodejs#56534 Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
`Realm::RunCleanup()` finalizes the cppgc-managed wrappers it tracks so that none of them touches the Realm once it is gone, but it reaches them through weak persistents, and the GC clears those as soon as it finds a wrapper dead. With lazy and concurrent sweeping the destructor can run much later, so a wrapper collected shortly before `FreeEnvironment()` and swept after it was skipped by the cleanup and kept its `realm_`: `~CppgcMixin()` then wrote `should_purge_empty_cppgc_wrappers_` into the freed Realm, and a subclass destructor calling `Finalize()` as documented would have called `Clean()` with a dangling Realm. A Worker that compiles a few `vm.Script`s, gets a full GC from external memory pressure and calls `process.exit()` is enough to hit the first case. Move the Realm pointer into the list node, which the wrapper now owns and deletes in its destructor. `CppgcWrapperList::Cleanup()` unlinks every node, finalizing the wrappers that are still alive and clearing the Realm pointer for the collected ones, which only their own destructor may still touch. `Realm::PendingCleanup()` accounts for the list so it is always drained. The purge flag, its GC epilogue callback and `PurgeEmpty()` are no longer needed, and removing them also stops the list nodes of wrappers that are alive at `FreeEnvironment()` from leaking. Refs: #56534 Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #65778 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
`MemoryTracker::AddNode(const CppgcMixin*)` allocates a `MemoryRetainerNode` that only stands in for the wrapper's JS node while its `MemoryInfo()` runs; unlike the other node kinds it is not handed to the `EmbedderGraph`, and nothing freed it. Every heap snapshot (or other `BuildEmbedderGraph` call) leaked one node per live `vm.Script` or `vm` context. Keep the placeholders in the tracker and free them with it, and make the already-seen path in `Track(const CppgcMixin*)` add its edge to the wrapper's JS node like the first visit does, so the graph never refers to a placeholder. Refs: nodejs#56534 Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
`MemoryTracker::AddNode(const CppgcMixin*)` allocates a `MemoryRetainerNode` that only stands in for the wrapper's JS node while its `MemoryInfo()` runs; unlike the other node kinds it is not handed to the `EmbedderGraph`, and nothing freed it. Every heap snapshot (or other `BuildEmbedderGraph` call) leaked one node per live `vm.Script` or `vm` context. Keep the placeholders in the tracker and free them with it, and make the already-seen path in `Track(const CppgcMixin*)` add its edge to the wrapper's JS node like the first visit does, so the graph never refers to a placeholder. Refs: nodejs#56534 Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
`MemoryTracker::AddNode(const CppgcMixin*)` allocates a `MemoryRetainerNode` that only stands in for the wrapper's JS node while its `MemoryInfo()` runs; unlike the other node kinds it is not handed to the `EmbedderGraph`, and nothing freed it. Every heap snapshot (or other `BuildEmbedderGraph` call) leaked one node per live `vm.Script` or `vm` context. Keep the placeholders in the tracker and free them with it, and make the already-seen path in `Track(const CppgcMixin*)` add its edge to the wrapper's JS node like the first visit does, so the graph never refers to a placeholder. Refs: nodejs#56534 Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
`Realm::RunCleanup()` finalizes the cppgc-managed wrappers it tracks so that none of them touches the Realm once it is gone, but it reaches them through weak persistents, and the GC clears those as soon as it finds a wrapper dead. With lazy and concurrent sweeping the destructor can run much later, so a wrapper collected shortly before `FreeEnvironment()` and swept after it was skipped by the cleanup and kept its `realm_`: `~CppgcMixin()` then wrote `should_purge_empty_cppgc_wrappers_` into the freed Realm, and a subclass destructor calling `Finalize()` as documented would have called `Clean()` with a dangling Realm. A Worker that compiles a few `vm.Script`s, gets a full GC from external memory pressure and calls `process.exit()` is enough to hit the first case. Move the Realm pointer into the list node, which the wrapper now owns and deletes in its destructor. `CppgcWrapperList::Cleanup()` unlinks every node, finalizing the wrappers that are still alive and clearing the Realm pointer for the collected ones, which only their own destructor may still touch. `Realm::PendingCleanup()` accounts for the list so it is always drained. The purge flag, its GC epilogue callback and `PurgeEmpty()` are no longer needed, and removing them also stops the list nodes of wrappers that are alive at `FreeEnvironment()` from leaking. Refs: #56534 Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #65778 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
src: track cppgc wrappers with a list in Realm
This allows us to perform cleanups of cppgc wrappers that rely
on a living Realm during Realm shutdown. Otherwise
the cleanup may happen during object destruction, which can
be triggered by GC after Realm shutdown, leading to invalid
access to Realm.
The general pattern for this type of non-trivial destruction is
designed to be:
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.