Skip to content

XAML server: in-flight project loads can cache a pre-invalidation graph #215

Closed

Description

Found while reviewing PR #50 (originally review finding M4).

XamlProjectResolver.Invalidate (server/src/WinUiXaml.Workspace/XamlProjectResolver.cs:204-239) evicts:

  • the cache entry keyed by the changed project, and
  • completed entries whose Result.ContainsProject(key) is true.

The gap: a load for a different root that is still in flight cannot be tested with ContainsProject, because the project graph does not exist until the load completes. If that pending load turns out to reference the invalidated project, it is cached afterwards with a pre-invalidation graph and is never evicted. In practice this can leave IntelliSense stale until the next edit to that project.

An attempted fix — adding || !pair.Value.IsCompleted to the predicate — was reverted, because it is not scoped to key and therefore evicts every in-flight load across all roots. That caused worse problems: DisposeWhenComplete would dispose a RoslynProjectWorkspace that other callers were already awaiting in ResolveAsync, plus duplicated MSBuild design-time loads, plus a possible no-progress loop where saves during a cold load repeatedly restart it.

Suggested work:

  • Record each pending load's candidate project set (e.g. the project paths known at load kickoff) so pending entries can be tested against key without waiting.
  • Alternatively, re-check ContainsProject on completion and self-evict, rather than eviction at invalidate time.
  • Either way, do not dispose a cached workspace task that active ResolveAsync callers may still be awaiting — that hazard exists today even for the same-key path.

Activity

  1. chiaramooney commented on Sep 8, 2026

    @chiaramooney
    CollaboratorAuthor

    Fixed in PR #50 (commits d684a72, 1709b23). One correction to the record, since it affects future work here.

    The core report is accurate and is what got fixed: an in-flight load for a different root cannot be tested with ContainsProject, so it could be cached with a pre-invalidation graph and never evicted. Invalidate now records the key on the pending entry and OnLoadCompleted re-tests it against the finished graph and self-evicts. Verified by reverting the recording line and confirming the regression test fails.

    The third suggested-work bullet is incorrect and should not be acted on. It states:

    do not dispose a cached workspace task that active ResolveAsync callers may still be awaiting -- that hazard exists today even for the same-key path.

    I implemented a lease for this before testing the premise. It does not hold. RoslynProjectWorkspace captures an immutable Roslyn Project snapshot in its constructor and serves every read from it (GetCompilationAsync, GetFrameworkCompilation, ContainsProject, XamlFiles, ApplicationDefinitionPath). Only LoadDiagnostics touches the MSBuildWorkspace that Dispose() shuts down. Disposing an evicted workspace therefore cannot break a resolve already in flight.

    Two experiments confirmed it: removing the lease broke no test, and a direct test disposing a workspace and then reading its compilation passes. The lease was removed in 1709b23, and DisposedWorkspaceStillServesCompilationReads now pins the premise so it fails loudly if a read ever starts depending on the live workspace.

    Remaining limitation: there is no multi-threaded stress test covering concurrent resolves against concurrent invalidations. The _gate discipline is reviewed, not empirically proven under contention.

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions