Address deadlock and race conditions in WebSocketConnection - #55651
Open
tmat wants to merge 4 commits into
Open
Conversation
|
Azure Pipelines: Successfully started running 1 pipeline(s). 2 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the Aspire out-of-proc service (Dotnet.Watch/AspireService) to avoid a lock-based deadlock during WebSocket connection teardown, and bumps the Visual Studio service target framework to net10.0.
Changes:
- Reworks
SocketConnectionManagerto use anImmutableDictionarywith interlocked updates instead of an explicit lock. - Refactors
WebSocketConnectionto a primary-constructor form (stillIDisposable). - Simplifies shared-items compilation to include all
*.csunder the AspireService directory and updates VS service TFM tonet10.0.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Dotnet.Watch/AspireService/Microsoft.WebTools.AspireService.projitems | Switches from explicit file list to wildcard include for C# sources. |
| src/Dotnet.Watch/AspireService/Microsoft.WebTools.AspireService.Package.csproj | Updates package comment; continues targeting $(VisualStudioServiceTargetFramework). |
| src/Dotnet.Watch/AspireService/Helpers/WebSocketConnection.cs | Refactors the connection holder type (primary constructor) and keeps disposal behavior. |
| src/Dotnet.Watch/AspireService/Helpers/SocketConnectionManager.cs | Replaces lock+Dictionary with lock-free immutable dictionary updates for connection tracking. |
| src/Dotnet.Watch/AspireService/Helpers/ImmutableInterlockedExtensions.cs | Adds a helper for interlocked transform updates on immutable state. |
| Directory.Build.props | Updates VisualStudioServiceTargetFramework to net10.0 and adds clarifying comment. |
Suppressed comments (2)
src/Dotnet.Watch/AspireService/Helpers/SocketConnectionManager.cs:63
- RemoveSocketConnection removes by key only. If a stale callback calls RemoveSocketConnection(oldConnection) after a new connection is registered with the same DcpId, ImmutableInterlocked.TryRemove will remove the new connection, then the code disposes the old instance. This can drop the active connection and leak/dispose the wrong instance.
public void RemoveSocketConnection(WebSocketConnection connection)
{
// If the connection is not in the dictionary, then it has already been removed and disposed
if (ImmutableInterlocked.TryRemove(ref _webSocketConnections, connection.DcpId, out var _))
{
src/Dotnet.Watch/AspireService/Helpers/SocketConnectionManager.cs:69
- _webSocketConnections is updated via interlocked operations but read without a volatile read. On weak memory models this can observe a stale dictionary snapshot and miss an added/removed connection. Using Volatile.Read aligns the read side with the lock-free publication pattern.
public WebSocketConnection? GetSocketConnection(string dcpId)
=> _webSocketConnections.GetValueOrDefault(dcpId);
Fixes deadlock dotnet#55560 Also update VS .NET TFM to net10
Member
Author
|
@karolz-ms ptal |
karolz-ms
approved these changes
Aug 6, 2026
karolz-ms
left a comment
Member
There was a problem hiding this comment.
LGTM but please consider my questions regarding exception handling
karolz-ms
approved these changes
Aug 7, 2026
tmat
enabled auto-merge (squash)
August 7, 2026 16:58
Member
Author
|
/azp run |
|
Azure Pipelines: Successfully started running 2 pipeline(s). 2 pipeline(s) were filtered out due to trigger conditions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes deadlock #55560
Also update VS .NET TFM to net10