Skip to content

CmdPal: Harden ListViewModel fetch synchronization#46429

Open
jiripolasek wants to merge 3 commits intomainfrom
dev/jpolasek/b/46331-cmdpal-fix-listviewmodel
Open

CmdPal: Harden ListViewModel fetch synchronization#46429
jiripolasek wants to merge 3 commits intomainfrom
dev/jpolasek/b/46331-cmdpal-fix-listviewmodel

Conversation

@jiripolasek
Copy link
Copy Markdown
Collaborator

@jiripolasek jiripolasek commented Mar 23, 2026

Summary of the Pull Request

This PR improves fetching of list items in ListViewModel:

  • Fixes _vmCache concurrency with copy-on-write cache publication.
  • Preserves latest-fetch-wins behavior across overlapping RPC GetItems() calls.
  • Prevents stale or canceled fetches from publishing and makes them abort promptly.
  • Improves cancellation cleanup for abandoned item view models and replaced token sources.
  • Updates empty-state tracking to follow overlapping fetch activity correctly.
  • Reduces hot-path cache overhead by removing per-item cache locking and full cache rebuilds.
  • Adds guard against re-entry, to prevent situations like CmdPal: StackOverflow in Time&Date #46329:
    • Defers ItemsChanged-triggered fetches raised during GetItems() until the call unwinds;
    • Uses a thread-local reentry guard so unrelated cross-thread fetches are not delayed;
    • Adds a regression test covering recursive GetItems() refresh behavior.
    • Make sure we never invoke FetchItems on UI thread, and be loud in debug when we are.

PR Checklist

  • Communication: I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected
  • Tests: Added/updated and all pass
  • Localization: All end-user-facing strings can be localized
  • Dev docs: Added/updated
  • New binaries: Added on the required places
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

Validation Steps Performed

- Fixes _vmCache concurrency with copy-on-write cache publication.
- Preserves latest-fetch-wins behavior across overlapping RPC GetItems() calls.
- Prevents stale or canceled fetches from publishing and makes them abort promptly.
- Improves cancellation cleanup for abandoned item view models and replaced token sources.
- Updates empty-state tracking to follow overlapping fetch activity correctly.
- Reduces hot-path cache overhead by removing per-item cache locking and full cache rebuilds.
@jiripolasek jiripolasek added the Product-Command Palette Refers to the Command Palette utility label Mar 23, 2026
- Defers ItemsChanged-triggered fetches raised during GetItems() until the call unwinds;
- Uses a thread-local reentry guard so unrelated cross-thread fetches are not delayed;
- Adds a regression test covering recursive GetItems() refresh behavior.
- Make sure we never invoke FetchItems on UI thread, and be loud in debug when we are.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens CmdPal ListViewModel item fetching to better handle overlapping refreshes, cancellation, and re-entrant ItemsChanged scenarios (notably the recursive GetItems() stack overflow pattern in #46329/#46429).

Changes:

  • Reworks fetch synchronization: generation-based “latest fetch wins”, copy-on-write VM cache publication, and more robust cancellation/cleanup.
  • Adds a thread-local re-entrancy guard to defer ItemsChanged-triggered fetches raised during GetItems() until unwinding.
  • Adds a regression unit test covering recursive refresh behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ListViewModel.cs Implements new fetch synchronization model (locks, generation tracking, deferred fetch), VM cache copy-on-write, and CTS cleanup helper.
src/modules/cmdpal/Tests/Microsoft.CmdPal.UI.ViewModels.UnitTests/ListViewModelTests.cs Adds a regression test ensuring ItemsChanged during GetItems() triggers a deferred (non-recursive) subsequent fetch.

@michaeljolley
Copy link
Copy Markdown
Contributor

In testing, I continually get missing icons in the All Apps list.
image

@jiripolasek
Copy link
Copy Markdown
Collaborator Author

jiripolasek commented Mar 27, 2026

In testing, I continually get missing icons in the All Apps list

I'll investigate I'm kidding, a squad of highly trained squirrels was dispatched to do that...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Product-Command Palette Refers to the Command Palette utility

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CmdPal: Corrupted view model cache on a list

3 participants