Skip to content

Signalr AddToGroupAsync possible race condition #38531

@maknapp

Description

@maknapp

Describe the bug

I was just looking at how groups are managed in Signalr and have not actually seen this happen. If a connection was disconnecting while something else was adding it to a group, would the following be possible?

Lines 47-51 are executed - The connection to add exists
Lines 299-300 are executed on another thread - The connection is removed from connection list and all groups
Line 53 is executed - The connection is added to the group but that connection is no longer in the connection list

var connection = _connections[connectionId];
if (connection == null)
{
return Task.CompletedTask;
}
_groups.Add(connection, groupName);

public override Task OnDisconnectedAsync(HubConnectionContext connection)
{
_connections.Remove(connection);
_groups.RemoveDisconnectedConnection(connection.ConnectionId);
return Task.CompletedTask;
}

If this is an issue, would another check for _connections[connectionId] is null after adding the connection to the group (after line 53) solve the issue?

To Reproduce

None

Exceptions (if any)

None

Further technical details

  • ASP.NET Core version: 6

Metadata

Metadata

Assignees

Labels

area-signalrIncludes: SignalR clients and serversbugThis issue describes a behavior which is not expected - a bug.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions