Commit bc9c219
committed
Related:
* oxc-project/oxc#10627
The resolver was experiencing memory leaks due to circular strong references between `CachedPath` objects. Even after the resolver was dropped, `CachedPath` instances (and their associated `PackageJson` data) remained in memory due to reference cycles created by Arc (reference-counted) pointers.
The memory leak was caused by circular references in three fields of `CachedPathImpl`:
1. `parent: Option<CachedPath>` - Child paths held strong references to parent paths
2. `canonicalized: OnceLock<Result<CachedPath, ResolveError>>` - Paths held strong references to their canonicalized versions
3. `node_modules: OnceLock<Option<CachedPath>>` - Parent directories held strong references to their node_modules subdirectories
These created reference cycles preventing Arc reference counts from reaching zero.
Converted all fields that could create circular references to use `Weak` references:
- `parent: Option<Weak<CachedPathImpl>>`
- `canonicalized: OnceLock<Result<Weak<CachedPathImpl>, ResolveError>>`
- `node_modules: OnceLock<Option<Weak<CachedPathImpl>>>`
Updated all accessor methods to upgrade Weak references when needed:
- `parent()` - Upgrades weak parent reference
- `cached_node_modules()` - Stores and retrieves weak references
- `canonicalize_impl()` - Stores canonicalized paths as weak references
Passes `test_memory_leak_arc_cycles` that failed in the previous stack.
1 parent 645164b commit bc9c219
File tree
4 files changed
+42
-27
lines changed- src
- cache
- tests
4 files changed
+42
-27
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| 57 | + | |
57 | 58 | | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
62 | | - | |
| 63 | + | |
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
| |||
240 | 241 | | |
241 | 242 | | |
242 | 243 | | |
243 | | - | |
| 244 | + | |
244 | 245 | | |
245 | 246 | | |
246 | 247 | | |
| |||
269 | 270 | | |
270 | 271 | | |
271 | 272 | | |
272 | | - | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
273 | 282 | | |
274 | | - | |
275 | 283 | | |
276 | 284 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
77 | | - | |
| 76 | + | |
| 77 | + | |
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
104 | 109 | | |
105 | 110 | | |
106 | 111 | | |
| |||
114 | 119 | | |
115 | 120 | | |
116 | 121 | | |
117 | | - | |
| 122 | + | |
118 | 123 | | |
119 | | - | |
120 | | - | |
| 124 | + | |
| 125 | + | |
121 | 126 | | |
122 | 127 | | |
123 | 128 | | |
124 | 129 | | |
125 | 130 | | |
126 | 131 | | |
127 | 132 | | |
128 | | - | |
| 133 | + | |
129 | 134 | | |
130 | 135 | | |
131 | | - | |
| 136 | + | |
132 | 137 | | |
133 | 138 | | |
134 | 139 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
304 | 304 | | |
305 | 305 | | |
306 | 306 | | |
307 | | - | |
| 307 | + | |
308 | 308 | | |
309 | 309 | | |
310 | 310 | | |
311 | | - | |
| 311 | + | |
312 | 312 | | |
313 | | - | |
| 313 | + | |
314 | 314 | | |
315 | 315 | | |
316 | 316 | | |
| |||
852 | 852 | | |
853 | 853 | | |
854 | 854 | | |
855 | | - | |
| 855 | + | |
| 856 | + | |
856 | 857 | | |
857 | | - | |
| 858 | + | |
858 | 859 | | |
859 | 860 | | |
860 | 861 | | |
861 | | - | |
| 862 | + | |
862 | 863 | | |
863 | 864 | | |
864 | 865 | | |
| |||
884 | 885 | | |
885 | 886 | | |
886 | 887 | | |
887 | | - | |
| 888 | + | |
888 | 889 | | |
889 | 890 | | |
890 | 891 | | |
| |||
1530 | 1531 | | |
1531 | 1532 | | |
1532 | 1533 | | |
1533 | | - | |
| 1534 | + | |
| 1535 | + | |
1534 | 1536 | | |
1535 | | - | |
| 1537 | + | |
1536 | 1538 | | |
1537 | 1539 | | |
1538 | 1540 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| |||
0 commit comments