Skip to content

Commit 8fae1c7

Browse files
committed
Ignore closed connection in Cancel. Fixes #820
1 parent 885f788 commit 8fae1c7

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

docs/content/tutorials/migrating-from-connector-net.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,4 @@ The following bugs in Connector/NET are fixed by switching to MySqlConnector. (~
262262
* [#101252](https://bugs.mysql.com/bug.php?id=101252): Can't query `CHAR(36)` column containing `NULL`
263263
* [#101253](https://bugs.mysql.com/bug.php?id=101253): Default value for `MySqlParameter.Value` changed from null to `0`
264264
* [#101302](https://bugs.mysql.com/bug.php?id=101302): Stored Procedure `BOOL` parameter can only be mapped to `MySqlDbType.Byte`
265+
* [#101507](https://bugs.mysql.com/bug.php?id=101507): `MySqlCommand.Cancel` throws `NullReferenceException` for a closed connection

src/MySqlConnector/MySqlConnection.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -687,29 +687,28 @@ internal ServerSession Session
687687

688688
internal void Cancel(ICancellableCommand command)
689689
{
690-
var session = Session;
691-
if (!session.TryStartCancel(command))
690+
if (m_session is null || State != ConnectionState.Open || !m_session.TryStartCancel(command))
692691
return;
693692

694693
try
695694
{
696695
// open a dedicated connection to the server to kill the active query
697696
var csb = new MySqlConnectionStringBuilder(m_connectionString);
698697
csb.Pooling = false;
699-
if (m_session!.IPAddress is not null)
698+
if (m_session.IPAddress is not null)
700699
csb.Server = m_session.IPAddress.ToString();
701700
csb.ConnectionTimeout = 3u;
702701

703702
using var connection = new MySqlConnection(csb.ConnectionString);
704703
connection.Open();
705704
using var killCommand = new MySqlCommand("KILL QUERY {0}".FormatInvariant(command.Connection!.ServerThread), connection);
706-
session.DoCancel(command, killCommand);
705+
m_session.DoCancel(command, killCommand);
707706
}
708707
catch (MySqlException ex)
709708
{
710709
// cancelling the query failed; setting the state back to 'Querying' will allow another call to 'Cancel' to try again
711710
Log.Warn(ex, "Session{0} cancelling command {1} failed", m_session!.Id, command.CommandId);
712-
session.AbortCancel(command);
711+
m_session.AbortCancel(command);
713712
}
714713
}
715714

tests/SideBySide/CommandTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,26 @@ public void CancelEmptyCommandIsNoop()
283283
cmd.Cancel();
284284
}
285285

286+
[SkippableFact(Baseline = "https://bugs.mysql.com/bug.php?id=101507")]
287+
public void CancelCommandForClosedConnectionIsNoop()
288+
{
289+
using var connection = new MySqlConnection(AppConfig.ConnectionString);
290+
connection.Open();
291+
using var cmd = connection.CreateCommand();
292+
connection.Close();
293+
cmd.Cancel();
294+
}
295+
296+
[SkippableFact(Baseline = "https://bugs.mysql.com/bug.php?id=101507")]
297+
public void CancelCommandForDisposedConnectionIsNoop()
298+
{
299+
using var connection = new MySqlConnection(AppConfig.ConnectionString);
300+
connection.Open();
301+
using var cmd = connection.CreateCommand();
302+
connection.Dispose();
303+
cmd.Cancel();
304+
}
305+
286306
[Fact]
287307
public void CommandsAreIndependent()
288308
{

0 commit comments

Comments
 (0)