Skip to content

Commit 1dc53c4

Browse files
committed
Reset state when opening is canceled. Fixes #1097
1 parent 73b3dde commit 1dc53c4

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/MySqlConnector/MySqlConnection.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,11 @@ internal async Task OpenAsync(IOBehavior? ioBehavior, CancellationToken cancella
408408
m_hasBeenOpened = true;
409409
SetState(ConnectionState.Open);
410410
}
411+
catch (OperationCanceledException)
412+
{
413+
SetState(ConnectionState.Closed);
414+
throw;
415+
}
411416
catch (MySqlException)
412417
{
413418
SetState(ConnectionState.Closed);

tests/SideBySide/ConnectAsync.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ public async Task ConnectBadPassword()
5151
Assert.Equal(ConnectionState.Closed, connection.State);
5252
}
5353

54+
[Fact]
55+
public async Task ConnectCanceled()
56+
{
57+
using var cts = new CancellationTokenSource();
58+
cts.Cancel();
59+
60+
using var connection = new MySqlConnection(AppConfig.ConnectionString);
61+
#if BASELINE
62+
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await connection.OpenAsync(cts.Token));
63+
#else
64+
await Assert.ThrowsAsync<OperationCanceledException>(async () => await connection.OpenAsync(cts.Token));
65+
#endif
66+
await connection.OpenAsync();
67+
Assert.Equal(ConnectionState.Open, connection.State);
68+
}
69+
5470
[Fact]
5571
public async Task State()
5672
{

0 commit comments

Comments
 (0)