From 226217d97239cf9bb40004d639169fa23ebd0a0e Mon Sep 17 00:00:00 2001 From: Jason Malinowski Date: Tue, 17 Jun 2025 16:38:57 -0700 Subject: [PATCH] Avoid acquiring the workspace lock on the UI thread if unneeded If the project system tells us about a file that's open and we are told about it on a background thread, we connect the file to the workspace on that background thread but then schedule work to the UI thread to ensure we correctly connect it to the right context (linked files, shared projects, multitargeting, etc.) That code on the UI thread then acquires the workspace lock, and we're seeing cases in the wild where that lock acquisition is taking longer than we'd like. Although there are deeper fixes we can do there, there's one trivial fix we can make: don't schedule the work to the UI thread unless the file is in at least two projects. Otherwise, we don't actually need to figure out the context in the first place and it's just wasting time. An entirely unscientific analysis of a handful of traces from users hitting this, there weren't any cases where the file was actually in more than one project, so this should have a nice improvement overall. Closes https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2471561 --- .../VisualStudioWorkspaceImpl.OpenFileTracker.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/VisualStudio/Core/Def/ProjectSystem/VisualStudioWorkspaceImpl.OpenFileTracker.cs b/src/VisualStudio/Core/Def/ProjectSystem/VisualStudioWorkspaceImpl.OpenFileTracker.cs index 364b9c2c4203a..9b04338e8d99e 100644 --- a/src/VisualStudio/Core/Def/ProjectSystem/VisualStudioWorkspaceImpl.OpenFileTracker.cs +++ b/src/VisualStudio/Core/Def/ProjectSystem/VisualStudioWorkspaceImpl.OpenFileTracker.cs @@ -369,9 +369,15 @@ public Task CheckForAddedFileBeingOpenMaybeAsync(bool useAsync, ImmutableArray= 2) + { + // Update the context on the UI thread as necessary. + var token = _asynchronousOperationListener.BeginAsyncOperation(nameof(UpdateContextAfterOpenAsync)); + UpdateContextAfterOpenAsync(newFileName).CompletesAsyncOperation(token); + } } } }