Bug #101507 MySqlCommand.Cancel throws NullReferenceException for a Closed connection
Submitted: 8 Nov 2020 2:06 Modified: 12 Oct 2022 22:09
Reporter: Bradley Grainger (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:8.0.22 OS:Windows (10)
Assigned to: CPU Architecture:Any

[8 Nov 2020 2:06] Bradley Grainger
Description:
When MySqlCommand.Cancel is called for a command associated with a closed (or disposed) MySqlConnection, it throws a NullReferenceException. I would expect it to silently succeed. If an exception must be thrown, ObjectDisposedException or InvalidOperationException would be more appropriate than NullReferenceException.

System.NullReferenceException
   at MySql.Data.MySqlClient.MySqlConnection.get_ServerThread()
   at MySql.Data.MySqlClient.MySqlConnection.CancelQuery(Int32 timeout)
   at MySql.Data.MySqlClient.MySqlCommand.Cancel()

How to repeat:
var connection = new MySqlConnection("...");
var command = connection.CreateCommand();
command.Cancel();

// *** OR ***

var connection = new MySqlConnection(csb.ConnectionString);
connection.Open();
var command = connection.CreateCommand();
connection.Close();
command.Cancel();
[8 Nov 2020 4:35] MySQL Verification Team
Hello Bradley,

Thank you for the report and test case.
Verified as described.

regards,
Umesh
[22 Sep 2022 20:25] Omar Chavez
Posted by developer:
 
Verified using Connector/Net version 8.0.30
[7 Oct 2022 16:30] Daniel Valdez
Posted by developer:
 
A verification was added in the MySqlCommand.Cancel() method that checks the connection state.
[12 Oct 2022 22:09] Christine Cole
Posted by developer:
 
Fixed as of the upcoming MySQL Connector/NET 8.0.32 release, and here's the proposed changelog entry from the documentation team:

An unexpected System.NullReferenceException message was returned when the
MySqlCommand.Cancel() method was called for a command associated with a
closed (or disposed) MySqlConnection object. The issue is resolved with
new validation in the method that now checks the connection state.

Thank you for the bug report.