From 2e921d61dcf70ca698e151d8af40d78293831cbf Mon Sep 17 00:00:00 2001 From: BrennanConroy Date: Mon, 23 Mar 2020 14:02:01 -0700 Subject: [PATCH] StopAsync resets state on inactive connection --- .../csharp/Client.Core/src/HubConnection.cs | 5 +++++ .../HubConnectionTests.ConnectionLifecycle.cs | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs b/src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs index 4d975d5d7e2f..8738545ffbde 100644 --- a/src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs +++ b/src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs @@ -500,6 +500,11 @@ private async Task StopAsyncCore(bool disposing) { connectionState.Stopping = true; } + else + { + // Reset StopCts if there isn't an active connection so that the next StartAsync wont immediately fail due to the token being canceled + _state.StopCts = new CancellationTokenSource(); + } if (disposing) { diff --git a/src/SignalR/clients/csharp/Client/test/UnitTests/HubConnectionTests.ConnectionLifecycle.cs b/src/SignalR/clients/csharp/Client/test/UnitTests/HubConnectionTests.ConnectionLifecycle.cs index 3c669ef94dd2..f1d191ee8ca8 100644 --- a/src/SignalR/clients/csharp/Client/test/UnitTests/HubConnectionTests.ConnectionLifecycle.cs +++ b/src/SignalR/clients/csharp/Client/test/UnitTests/HubConnectionTests.ConnectionLifecycle.cs @@ -334,6 +334,26 @@ await AsyncUsing(CreateHubConnection(testConnection), async connection => }); } + [Fact] + public async Task StopAsyncOnInactiveConnectionDoesNotAffectNextStartAsync() + { + // Regression test: + // If there wasn't an active underlying connection, StopAsync would leave a CTS canceled which would cause the next StartAsync to fail + var testConnection = new TestConnection(); + await AsyncUsing(CreateHubConnection(testConnection), async connection => + { + Assert.Equal(HubConnectionState.Disconnected, connection.State); + + await connection.StopAsync().OrTimeout(); + Assert.False(testConnection.Disposed.IsCompleted); + Assert.Equal(HubConnectionState.Disconnected, connection.State); + + await connection.StartAsync().OrTimeout(); + Assert.True(testConnection.Started.IsCompleted); + Assert.Equal(HubConnectionState.Connected, connection.State); + }); + } + [Fact] public async Task CompletingTheTransportSideMarksConnectionAsClosed() {