Commit 38ff388
committed
shadow_realm: fix memory leak by removing strong reference tracking
Shadow Realm contexts were never being garbage collected due to the
Environment keeping strong C++ pointer references in the shadow_realms_
unordered_set. This created a circular dependency:
1. Environment holds strong pointer to ShadowRealm in shadow_realms_ set
2. ShadowRealm cannot be deleted because Environment holds it
3. V8 context is weak, but depends on ShadowRealm C++ object lifecycle
4. Circular leak - realms accumulate ~1.285 MB each, never freed
The fix removes the TrackShadowRealm/UntrackShadowRealm mechanism entirely.
Shadow Realms are now managed solely via:
- Weak V8 context handle (context_.SetWeak())
- Cleanup hooks (AddCleanupHook/RemoveCleanupHook)
- Weak callback (ShadowRealm::WeakCallback)
This allows V8's garbage collector to properly reclaim Shadow Realm
contexts when they become unreachable from JavaScript, as the C++
ShadowRealm object is no longer kept alive by a strong reference
from the Environment.
The existing test (test/parallel/test-shadow-realm-gc.js) verifies
that Shadow Realms can be garbage collected with --expose-gc.
Fixes: #473531 parent bfc81ca commit 38ff388
3 files changed
+18
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
259 | 259 | | |
260 | 260 | | |
261 | 261 | | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
269 | 265 | | |
270 | 266 | | |
271 | 267 | | |
| |||
1044 | 1040 | | |
1045 | 1041 | | |
1046 | 1042 | | |
1047 | | - | |
1048 | | - | |
| 1043 | + | |
| 1044 | + | |
1049 | 1045 | | |
1050 | 1046 | | |
1051 | 1047 | | |
| |||
2232 | 2228 | | |
2233 | 2229 | | |
2234 | 2230 | | |
2235 | | - | |
| 2231 | + | |
2236 | 2232 | | |
2237 | 2233 | | |
2238 | 2234 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
691 | 691 | | |
692 | 692 | | |
693 | 693 | | |
694 | | - | |
695 | | - | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
696 | 697 | | |
697 | 698 | | |
698 | 699 | | |
| |||
1117 | 1118 | | |
1118 | 1119 | | |
1119 | 1120 | | |
1120 | | - | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
1121 | 1124 | | |
1122 | 1125 | | |
1123 | 1126 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | | - | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
83 | 86 | | |
84 | 87 | | |
85 | 88 | | |
| |||
88 | 91 | | |
89 | 92 | | |
90 | 93 | | |
91 | | - | |
| 94 | + | |
| 95 | + | |
92 | 96 | | |
93 | 97 | | |
94 | 98 | | |
| |||
0 commit comments