Skip to content

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

Merged
merged 5 commits into from
Jun 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Aspire.Cli/Projects/ProjectLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Member

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.

Copy link
Contributor Author

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?

logger.LogDebug("Searching for project files in {SearchDirectory}", searchDirectory.FullName);
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
{
Expand Down Expand Up @@ -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
{
Expand Down
Loading