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
Show file tree
Hide file tree
Changes from 2 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
8 changes: 5 additions & 3 deletions src/Aspire.Cli/Projects/ProjectLocator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Concurrent;
using System.Diagnostics;
using System.Text.Json;
using Aspire.Cli.Interaction;
Expand All @@ -24,7 +25,7 @@ private async Task<List<FileInfo>> FindAppHostProjectFilesAsync(DirectoryInfo se

return await interactionService.ShowStatusAsync("Searching", async () =>
{
var appHostProjects = new List<FileInfo>();
var appHostProjects = new ConcurrentBag<FileInfo>();
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 @@ -62,9 +63,10 @@ await Parallel.ForEachAsync(projectFiles, async (projectFile, ct) =>

// This sort is done here to make results deterministic since we get all the app
// host information in parallel and the order may vary.
appHostProjects.Sort((x, y) => x.FullName.CompareTo(y.FullName));
var sortedProjects = appHostProjects.ToList();
sortedProjects.Sort((x, y) => x.FullName.CompareTo(y.FullName));

return appHostProjects;
return sortedProjects;
});
}

Expand Down
13 changes: 13 additions & 0 deletions tests/Aspire.Cli.Tests/Projects/ProjectLocatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,25 @@ public async Task UseOrFindAppHostProjectFilePromptsWhenMultipleFilesFound()
var projectFile2 = new FileInfo(Path.Combine(workspace.WorkspaceRoot.FullName, "AppHost2.csproj"));
await File.WriteAllTextAsync(projectFile2.FullName, "Not a real project file.");

// Verify both files were created
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop the changes in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted all changes to the test file as requested. e284fcd

Assert.True(projectFile1.Exists, $"Project file 1 should exist: {projectFile1.FullName}");
Assert.True(projectFile2.Exists, $"Project file 2 should exist: {projectFile2.FullName}");

// Verify string comparison works as expected
var comparison = projectFile1.FullName.CompareTo(projectFile2.FullName);
outputHelper.WriteLine($"File comparison result: {comparison}");
outputHelper.WriteLine($"File 1: {projectFile1.FullName}");
outputHelper.WriteLine($"File 2: {projectFile2.FullName}");

Assert.True(comparison < 0, "AppHost1.csproj should come before AppHost2.csproj alphabetically");

var runner = new TestDotNetCliRunner();
var interactionService = new TestInteractionService();
var projectLocator = new ProjectLocator(logger, runner, workspace.WorkspaceRoot, interactionService);

var selectedProjectFile = await projectLocator.UseOrFindAppHostProjectFileAsync(null);

outputHelper.WriteLine($"Selected: {selectedProjectFile?.FullName}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop this as the filename would be shown if the following Assert fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the debugging output line as requested. 2e45f79

Assert.Equal(projectFile1.FullName, selectedProjectFile!.FullName);
}

Expand Down
Loading