Skip to content

Commit 9e63572

Browse files
StopAsync resets state on inactive connection (#20083) (#23962)
1 parent b8a0fa7 commit 9e63572

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,11 @@ private async Task StopAsyncCore(bool disposing)
501501
{
502502
connectionState.Stopping = true;
503503
}
504+
else
505+
{
506+
// Reset StopCts if there isn't an active connection so that the next StartAsync wont immediately fail due to the token being canceled
507+
_state.StopCts = new CancellationTokenSource();
508+
}
504509

505510
if (disposing)
506511
{

src/SignalR/clients/csharp/Client/test/UnitTests/HubConnectionTests.ConnectionLifecycle.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,26 @@ await AsyncUsing(CreateHubConnection(testConnection), async connection =>
334334
});
335335
}
336336

337+
[Fact]
338+
public async Task StopAsyncOnInactiveConnectionDoesNotAffectNextStartAsync()
339+
{
340+
// Regression test:
341+
// If there wasn't an active underlying connection, StopAsync would leave a CTS canceled which would cause the next StartAsync to fail
342+
var testConnection = new TestConnection();
343+
await AsyncUsing(CreateHubConnection(testConnection), async connection =>
344+
{
345+
Assert.Equal(HubConnectionState.Disconnected, connection.State);
346+
347+
await connection.StopAsync().OrTimeout();
348+
Assert.False(testConnection.Disposed.IsCompleted);
349+
Assert.Equal(HubConnectionState.Disconnected, connection.State);
350+
351+
await connection.StartAsync().OrTimeout();
352+
Assert.True(testConnection.Started.IsCompleted);
353+
Assert.Equal(HubConnectionState.Connected, connection.State);
354+
});
355+
}
356+
337357
[Fact]
338358
public async Task CompletingTheTransportSideMarksConnectionAsClosed()
339359
{

0 commit comments

Comments
 (0)