From 28761421aca49d9c235542bf5ab6a795122d1841 Mon Sep 17 00:00:00 2001 From: Drew Noakes Date: Thu, 12 Dec 2024 22:37:17 +1100 Subject: [PATCH] Fix "collection modified" exception When signalling project close to an info bar entry, if there are no other remaining projects associated with that info bar, it will be removed from the collection. That triggers an exception. The fix is to make a defensive copy before enumerating, so that the original can be safely modified while the copy is enumerated. --- .../ProjectSystem/VS/UI/InfoBars/VsInfoBarService.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.VisualStudio.ProjectSystem.Managed.VS/ProjectSystem/VS/UI/InfoBars/VsInfoBarService.cs b/src/Microsoft.VisualStudio.ProjectSystem.Managed.VS/ProjectSystem/VS/UI/InfoBars/VsInfoBarService.cs index 076103eebd..80548f0c89 100644 --- a/src/Microsoft.VisualStudio.ProjectSystem.Managed.VS/ProjectSystem/VS/UI/InfoBars/VsInfoBarService.cs +++ b/src/Microsoft.VisualStudio.ProjectSystem.Managed.VS/ProjectSystem/VS/UI/InfoBars/VsInfoBarService.cs @@ -47,7 +47,10 @@ public VsInfoBarService( { lock (s_entries) { - foreach (InfoBar entry in s_entries) + // Copy the list to avoid "collection modified during enumeration" exceptions, + // as when the last project associated with an info bar is closed, we will + // remove that entry from the list. + foreach (InfoBar entry in s_entries.ToList()) { entry.OnProjectClosed(_project); }