Skip to content
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

Recursive calls in reconnection logic leads to memory leaks #124

Open
Discolai opened this issue Jan 11, 2023 · 1 comment
Open

Recursive calls in reconnection logic leads to memory leaks #124

Discolai opened this issue Jan 11, 2023 · 1 comment

Comments

@Discolai
Copy link

Hi,

I am using this library in an application where a client might stay in a reconnection state over several days. The built in reconnection logic calls the WebsocketClient.StartClient method recursively. This leads to a steady increase in memory allocation after each reconnect attempt

The following code snippet creates 10 000 clients which reconnects every 100 milliseconds. It is not a real life scenario, but it demonstrates the issue.
It managed to allocate 600MB in 30 sek

using Websocket.Client;

await Task.WhenAll(Enumerable.Range(0, 10_000).Select(i => CreateClient(i)).Select(client => client.Start()));

WebsocketClient CreateClient(int i)
{
    var websocketClient = new WebsocketClient(new Uri("ws://localhost:8080/ws"),  (_, _) =>
    {
        throw new Exception(i.ToString());
    })
    {
        ReconnectTimeout = TimeSpan.FromMilliseconds(100),
        ErrorReconnectTimeout = TimeSpan.FromMilliseconds(100),
        IsReconnectionEnabled = true
    };

    websocketClient.DisconnectionHappened.Subscribe(info =>
    {
        Console.WriteLine($"Disconnect - {info.Exception?.Message}");
    });
    return websocketClient;
}
@Marfusios
Copy link
Owner

Hey @Discolai ,

thank you for reporting this with the useful code snippet.
I have broken the recursive call by introducing an error timer, and it seems to solve the problem nicely here:
df2e007

Before:
image

After:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants