From 46dedad8b60a9f4f6163916fd2c6f25a801e2da6 Mon Sep 17 00:00:00 2001 From: Elijah Burshtein Date: Sun, 17 Mar 2024 14:57:50 +0200 Subject: [PATCH] Separate the token used for starting/reconnecting and the token used when listening --- src/Websocket.Client/WebsocketClient.Reconnecting.cs | 6 ++++-- src/Websocket.Client/WebsocketClient.cs | 7 ++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Websocket.Client/WebsocketClient.Reconnecting.cs b/src/Websocket.Client/WebsocketClient.Reconnecting.cs index 3643f0c..2ed6c51 100644 --- a/src/Websocket.Client/WebsocketClient.Reconnecting.cs +++ b/src/Websocket.Client/WebsocketClient.Reconnecting.cs @@ -97,8 +97,10 @@ private async Task Reconnect(ReconnectionType type, bool failFast, Exception? ca } _logger.LogDebug(L("Reconnecting..."), Name); - _cancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellation); - await StartClient(_url, _cancellation.Token, type, failFast).ConfigureAwait(false); + _cancellation = new CancellationTokenSource(); + + using var cts = CancellationTokenSource.CreateLinkedTokenSource(_cancellation.Token, cancellation); + await StartClient(_url, cts.Token, type, failFast).ConfigureAwait(false); _reconnecting = false; } diff --git a/src/Websocket.Client/WebsocketClient.cs b/src/Websocket.Client/WebsocketClient.cs index 19ea4ba..f63d52a 100644 --- a/src/Websocket.Client/WebsocketClient.cs +++ b/src/Websocket.Client/WebsocketClient.cs @@ -317,10 +317,11 @@ private async Task StartInternal(bool failFast, CancellationToken cancellation) IsStarted = true; _logger.LogDebug(L("Starting.."), Name); - _cancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellation); + _cancellation = new CancellationTokenSource(); _cancellationTotal = new CancellationTokenSource(); - await StartClient(_url, _cancellation.Token, ReconnectionType.Initial, failFast).ConfigureAwait(false); + using var cts = CancellationTokenSource.CreateLinkedTokenSource(_cancellation.Token, cancellation); + await StartClient(_url, cts.Token, ReconnectionType.Initial, failFast).ConfigureAwait(false); StartBackgroundThreadForSendingText(); StartBackgroundThreadForSendingBinary(); @@ -393,7 +394,7 @@ private async Task StartClient(Uri uri, CancellationToken token, ReconnectionTyp try { _client = await _connectionFactory(uri, token).ConfigureAwait(false); - _ = Listen(_client, token); + _ = Listen(_client, _cancellation?.Token ?? CancellationToken.None); IsRunning = true; IsStarted = true; _reconnectionSubject.OnNext(ReconnectionInfo.Create(type));