-
Notifications
You must be signed in to change notification settings - Fork 647
Fix thread safety issue in FindAppHostProjectFilesAsync by switching to ConcurrentBag #9655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5676167
e53bba6
2e45f79
e284fcd
9c8bc8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ private async Task<List<FileInfo>> FindAppHostProjectFilesAsync(DirectoryInfo se | |
return await interactionService.ShowStatusAsync("Searching", async () => | ||
{ | ||
var appHostProjects = new List<FileInfo>(); | ||
var lockObject = new object(); | ||
logger.LogDebug("Searching for project files in {SearchDirectory}", searchDirectory.FullName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot Change this back to List and instead of using ConcurrentBag synchronize access to the list inside the callback to Parallel.ForEachAsync and lock the collection when adding items to it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed back to List with locking as requested. Removed ConcurrentBag import, added lock object, and wrapped the list.Add call in a lock statement for thread safety. 9c8bc8c |
||
var enumerationOptions = new EnumerationOptions | ||
{ | ||
|
@@ -52,7 +53,10 @@ await Parallel.ForEachAsync(projectFiles, async (projectFile, ct) => | |
logger.LogDebug("Found AppHost project file {ProjectFile} in {SearchDirectory}", projectFile.FullName, searchDirectory.FullName); | ||
var relativePath = Path.GetRelativePath(currentDirectory.FullName, projectFile.FullName); | ||
interactionService.DisplaySubtleMessage(relativePath); | ||
appHostProjects.Add(projectFile); | ||
lock (lockObject) | ||
{ | ||
appHostProjects.Add(projectFile); | ||
} | ||
} | ||
else | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot you don't need this object, you can use SyncRoot on the list itself for this purpose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially tried using SyncRoot but found that for generic collections like List<T>, SyncRoot doesn't provide thread safety - it just returns the list itself. The documentation recommends using dedicated synchronization objects for thread-safe operations. I've reverted to using a lock object to ensure proper thread safety. Would you prefer a different approach?