Skip to content

fix: use Weak references for CachedPath to enable proper drop#727

Merged
graphite-app[bot] merged 1 commit intomainfrom
09-23-fix_use_weak_references_for_cachedpath_to_enable_proper_drop
Sep 23, 2025
Merged

fix: use Weak references for CachedPath to enable proper drop#727
graphite-app[bot] merged 1 commit intomainfrom
09-23-fix_use_weak_references_for_cachedpath_to_enable_proper_drop

Conversation

@Boshen
Copy link
Member

@Boshen Boshen commented Sep 23, 2025

Related:

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.

@Boshen Boshen marked this pull request as draft September 23, 2025 10:55
Copy link
Member Author

Boshen commented Sep 23, 2025


How to use the Graphite Merge Queue

Add the label merge to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codspeed-hq

This comment was marked as off-topic.

@Boshen Boshen force-pushed the 09-23-fix_use_weak_references_for_cachedpath_to_enable_proper_drop branch from a471b41 to a7be55f Compare September 23, 2025 11:09
@codecov
Copy link

codecov bot commented Sep 23, 2025

Codecov Report

❌ Patch coverage is 94.11765% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.63%. Comparing base (e03b08a) to head (bc9c219).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/cache/cache_impl.rs 81.81% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #727      +/-   ##
==========================================
- Coverage   94.68%   94.63%   -0.05%     
==========================================
  Files          15       15              
  Lines        2896     2908      +12     
==========================================
+ Hits         2742     2752      +10     
- Misses        154      156       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Boshen Boshen marked this pull request as ready for review September 23, 2025 11:11
Base automatically changed from 09-23-test_add_memory_leak_test to main September 23, 2025 11:14
@Boshen Boshen force-pushed the 09-23-fix_use_weak_references_for_cachedpath_to_enable_proper_drop branch from a7be55f to 65a5375 Compare September 23, 2025 11:15
@graphite-app
Copy link

graphite-app bot commented Sep 23, 2025

Merge activity

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.
@graphite-app graphite-app bot force-pushed the 09-23-fix_use_weak_references_for_cachedpath_to_enable_proper_drop branch from 65a5375 to bc9c219 Compare September 23, 2025 11:19
@graphite-app graphite-app bot merged commit bc9c219 into main Sep 23, 2025
15 checks passed
@graphite-app graphite-app bot deleted the 09-23-fix_use_weak_references_for_cachedpath_to_enable_proper_drop branch September 23, 2025 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant