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

fix: use WeakMap in DerivedStateProvider to separate user state caches #12866

Merged

Conversation

addisonbeck
Copy link
Contributor

@addisonbeck addisonbeck commented Jan 14, 2025

🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-15914

📔 Objective

When switching accounts derived state was still fetching from a cache for the
previous account, leading to UI elements being displayed with data from an
inactive account's cache. The DerivedStateProvider now uses a WeakMap to
maintain separate caches for each user's state Observable.

  • Modifies DefaultDerivedStateProvider to use WeakMap for caching
  • Each user's state Observable gets its own definition cache
  • Added test to verify correct behavior during account switching
  • Automatically cleans up inactive user caches on parentState change

⏰ Reminders before review

  • Contributor guidelines followed
  • All formatters and local linters executed and passed
  • Written new unit and / or integration tests where applicable
  • Protected functional changes with optionality (feature flags)
  • Used internationalization (i18n) for all UI strings
  • CI builds passed
  • Communicated to DevOps any deployment requirements
  • Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team

🦮 Reviewer guidelines

  • 👍 (:+1:) or similar for great changes
  • 📝 (:memo:) or ℹ️ (:information_source:) for notes or general info
  • ❓ (:question:) for questions
  • 🤔 (:thinking:) or 💭 (:thought_balloon:) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion
  • 🎨 (:art:) for suggestions / improvements
  • ❌ (:x:) or ⚠️ (:warning:) for more significant problems or concerns needing attention
  • 🌱 (:seedling:) or ♻️ (:recycle:) for future improvements or indications of technical debt
  • ⛏ (:pick:) for minor or nitpick changes

Copy link
Contributor

github-actions bot commented Jan 14, 2025

Logo
Checkmarx One – Scan Summary & Detailsb2b55602-12f5-439f-b6cc-5057d82bc363

Great job, no security vulnerabilities found in this Pull Request

Copy link

codecov bot commented Jan 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 34.29%. Comparing base (f44b36b) to head (2f0cee5).
Report is 9 commits behind head on main.

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #12866   +/-   ##
=======================================
  Coverage   34.28%   34.29%           
=======================================
  Files        2934     2934           
  Lines       90225    90229    +4     
  Branches    16944    16945    +1     
=======================================
+ Hits        30937    30943    +6     
+ Misses      56824    56822    -2     
  Partials     2464     2464           

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

@addisonbeck addisonbeck force-pushed the platform/addison/pm-15914/derived-state-caching-bug branch 2 times, most recently from e8346df to 6352cfa Compare January 14, 2025 18:08
…aches

Bug fix for PM-15914 where switching users would incorrectly share cached
derived states. The `DerivedStateProvider` now uses a `WeakMap` to maintain
separate caches for each user's state `Observable`.

- Modifies `DefaultDerivedStateProvider` to use `WeakMap` for caching
- Each user's state `Observable` gets its own definition cache
- Added test to verify correct behavior during user switching
- Allows proper garbage collection of unused state caches

This fixes issues where:
- Users would see other users' cached states after switching accounts
- Derived states weren't properly isolated between users
- Cache keys didn't distinguish between different user states
@addisonbeck addisonbeck force-pushed the platform/addison/pm-15914/derived-state-caching-bug branch from 6352cfa to 2f0cee5 Compare January 14, 2025 18:18
@addisonbeck addisonbeck marked this pull request as ready for review January 14, 2025 18:19
@addisonbeck addisonbeck requested a review from a team as a code owner January 14, 2025 18:19
@addisonbeck addisonbeck changed the title fix: Use WeakMap in DerivedStateProvider to separate user state caches fix: use WeakMap in DerivedStateProvider to separate user state caches Jan 14, 2025
@addisonbeck addisonbeck enabled auto-merge (squash) January 14, 2025 18:23
@addisonbeck addisonbeck merged commit 55e4b5e into main Jan 15, 2025
88 checks passed
@addisonbeck addisonbeck deleted the platform/addison/pm-15914/derived-state-caching-bug branch January 15, 2025 10:41
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.

2 participants