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

ConnectClientAsync - Cannot access a disposed object. #30

Closed
JulanDeAlb opened this issue Dec 12, 2023 · 1 comment
Closed

ConnectClientAsync - Cannot access a disposed object. #30

JulanDeAlb opened this issue Dec 12, 2023 · 1 comment

Comments

@JulanDeAlb
Copy link
Contributor

Error:
I encounter the following Error using the WebSocketClient:
After a internet connection loss, the Client try to reconnect to Twitch BUT after the first attempt it will just straight up crash because the .NET ClientWebSocket disposes its inner WebSocket if it couldn't connect to the URL.

Client = CreateClient();
var first = true;
Options.ReconnectionPolicy.Reset(isReconnect);
while (!IsConnected &&
!Options.ReconnectionPolicy.AreAttemptsComplete())
{
Logger?.TraceAction(GetType(), "try to connect");
if (!first)
{
await Task.Delay(Options.ReconnectionPolicy.GetReconnectInterval(), CancellationToken.None);
}
await ConnectClientAsync();
Options.ReconnectionPolicy.ProcessValues();
first = false;
}

In the given code above, every attempt to run OpenPrivateAsync() will lead to create a new Client, but then enters the loop where it stays even if it didn't connected successfully, trying to connect with a disposed inner WebSocket.

Easy Reproduction:
Disconnect your PC from the Internet and run

var client = new TwitchClient(loggerFactory: factory);
client.Initialize(new ConnectionCredentials(...));

client.ConnectAsync();
Console.ReadKey();

Possible Solution:
Move line 241 into the while loop

while (!IsConnected &&  !Options.ReconnectionPolicy.AreAttemptsComplete())
{
                Logger?.TraceAction(GetType(), "try to connect");
                
                // Always create new client when opening new connection
                Client = CreateClient();
                
                if (!first)
                {
                    await Task.Delay(Options.ReconnectionPolicy.GetReconnectInterval(), CancellationToken.None);
                }
Hampo added a commit to Hampo/TwitchLib.Communication that referenced this issue Dec 12, 2023
@AoshiW
Copy link
Contributor

AoshiW commented Jan 3, 2024

this issue has been resolved by #32 , so it can be closed

@Syzuna Syzuna closed this as completed Jan 3, 2024
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

3 participants