Skip to content

Commit

Permalink
Separate the token used for starting/reconnecting and the token used …
Browse files Browse the repository at this point in the history
…when listening
  • Loading branch information
AvenDonn committed Mar 17, 2024
1 parent 805b68f commit 46dedad
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/Websocket.Client/WebsocketClient.Reconnecting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
7 changes: 4 additions & 3 deletions src/Websocket.Client/WebsocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 46dedad

Please sign in to comment.