Skip to content

Commit fadf127

Browse files
committed
Raise Component.Disposed from DisposeAsync. Fixes #1235
1 parent 08cbea2 commit fadf127

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/MySqlConnector/MySqlConnection.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,11 @@ public async Task DisposeAsync()
694694
finally
695695
{
696696
m_isDisposed = true;
697+
698+
// Component implements the Dispose pattern, with some core logic implemented in Dispose(bool disposing). DbConnection
699+
// adds DisposeAsync but doesn't implement the full DisposeAsyncCore pattern. Thus, although DisposeAsync is supposed
700+
// to call Dispose(false), we call Dispose(true) here to execute that base class logic in both the sync and async paths.
701+
base.Dispose(true);
697702
}
698703
}
699704

tests/IntegrationTests/ConnectAsync.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,17 @@ public async Task UnixDomainSocket()
524524
Assert.Equal(ConnectionState.Open, connection.State);
525525
}
526526

527+
[Fact]
528+
public async Task DisposeAsyncRaisesDisposed()
529+
{
530+
var disposedCount = 0;
531+
var connection = new MySqlConnection(AppConfig.ConnectionString);
532+
connection.Disposed += (sender, args) => disposedCount++;
533+
await connection.OpenAsync().ConfigureAwait(false);
534+
await connection.DisposeAsync().ConfigureAwait(false);
535+
Assert.Equal(1, disposedCount);
536+
}
537+
527538
readonly DatabaseFixture m_database;
528539
}
529540

tests/IntegrationTests/ConnectSync.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,5 +589,16 @@ public void UnixDomainSocket()
589589
Assert.Equal(ConnectionState.Open, connection.State);
590590
}
591591

592+
[Fact]
593+
public void DisposeRaisesDisposed()
594+
{
595+
var disposedCount = 0;
596+
var connection = new MySqlConnection(AppConfig.ConnectionString);
597+
connection.Disposed += (sender, args) => disposedCount++;
598+
connection.Open();
599+
connection.Dispose();
600+
Assert.Equal(1, disposedCount);
601+
}
602+
592603
readonly DatabaseFixture m_database;
593604
}

0 commit comments

Comments
 (0)