Skip to content

SignalR Client client-to-server streaming is memory leaking in ConnectionState.UploadStreamToken #50913

@higor-hernandes

Description

@higor-hernandes

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When using ASP.NET Core SignalR Client to stream data from client to server, the HubConnection is memory leaking CancellationToken Callbacks in the CancellationTokenSource of the ConnectionState.UploadStreamToken. For each call of HubConnection.SendAsync it registers a new CancellationToken Callback in this CancellationTokenSource and does not remove it after completing the upload. I don’t know if it is just a missuse of the SignalR client or it really has a memory leak while uploading streams.

image
image

Expected Behavior

Does not indefinitely increase memory usage when sending a ChannelReader in the HubConnection.SendAsync. Unregister the cancellation callbacks after completing each SendAsync request.

Steps To Reproduce

Keep uploading data using the HubConnection.SendAsync and Channels
For each call of the SendAsync it leaks in the CancellationTokenSource described.

repo: https://github.com/higor-hernandes/signalr-sample-tests
build and run the SignalRHubSample project.
build and run the SignalRHubClientSample project.
In a few seconds is possible to see the CancellationToken Callbacks being register and accumulating in the CancellationTokenSource from ConnectionState.UploadStreamToken created in the HubConnection.ReceiveLoop method

Client implementation of the how to reproduce:
image

Hub implementation of the how to reproduce:
image

Exceptions (if any)

No response

.NET Version

7.0.202

Anything else?

Microsoft Visual Studio Professional 2022 (64-bit) - Current
Version 17.5.3

.NET SDK:
Version: 7.0.202
Commit: 6c74320bc3
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.202
Host:
Version: 7.0.4
Architecture: x64
Commit: 0a396acafe
.NET SDKs installed:
7.0.202 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.19 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
Not found

Client Sample Packages:
Microsoft.AspNetCore.SignalR.Client 7.0.11
Hub Sample Packages:
Microsoft.AspNetCore.SignalR.Protocols.Json 7.0.11

Where in SignalR Client the leak might be coming from:
image
image

Metadata

Metadata

Assignees

No one assigned

    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