From 6bfb43df48d9f03ad838f8f0c65da9daa05eb03e Mon Sep 17 00:00:00 2001 From: Jason Malinowski Date: Sat, 16 Mar 2019 09:25:16 -0700 Subject: [PATCH] Fix race in GetOpenDocumentInCurrentContextWithChanges This was reading Workspace.CurrentSolution twice, so if the document was removed between two reads it'd end up throwing exceptions or triggering asserts. --- src/Workspaces/Core/Portable/Workspace/TextExtensions.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Workspaces/Core/Portable/Workspace/TextExtensions.cs b/src/Workspaces/Core/Portable/Workspace/TextExtensions.cs index 61500749daee4..ee583cb1ee2fa 100644 --- a/src/Workspaces/Core/Portable/Workspace/TextExtensions.cs +++ b/src/Workspaces/Core/Portable/Workspace/TextExtensions.cs @@ -33,14 +33,15 @@ public static Document GetOpenDocumentInCurrentContextWithChanges(this SourceTex { if (Workspace.TryGetWorkspace(text.Container, out var workspace)) { + var solution = workspace.CurrentSolution; var id = workspace.GetDocumentIdInCurrentContext(text.Container); - if (id == null || !workspace.CurrentSolution.ContainsDocument(id)) + if (id == null || !solution.ContainsDocument(id)) { return null; } - var sol = workspace.CurrentSolution.WithDocumentText(id, text, PreservationMode.PreserveIdentity); - return sol.GetDocument(id); + return solution.WithDocumentText(id, text, PreservationMode.PreserveIdentity) + .GetDocument(id); } return null;