Skip to content

Commit 917b776

Browse files
authored
Merge pull request #1183 from maicodio/Fix-issue-1177
Fix NullReferenceException in Cancel.
2 parents dcf4913 + eadb98c commit 917b776

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/MySqlConnector/MySqlConnection.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -746,13 +746,14 @@ internal ServerSession Session
746746

747747
internal void Cancel(ICancellableCommand command, int commandId, bool isCancel)
748748
{
749-
if (m_session is null || State != ConnectionState.Open || !m_session.TryStartCancel(command))
749+
var session = m_session;
750+
if (session is null || State != ConnectionState.Open || !session.TryStartCancel(command))
750751
{
751752
Log.Trace("Ignoring cancellation for closed connection or invalid CommandId {0}", commandId);
752753
return;
753754
}
754755

755-
Log.Debug("CommandId {0} for Session{1} has been canceled via {2}.", commandId, m_session.Id, isCancel ? "Cancel()" : "command timeout");
756+
Log.Debug("CommandId {0} for Session{1} has been canceled via {2}.", commandId, session.Id, isCancel ? "Cancel()" : "command timeout");
756757

757758
try
758759
{
@@ -762,29 +763,29 @@ internal void Cancel(ICancellableCommand command, int commandId, bool isCancel)
762763
AutoEnlist = false,
763764
Pooling = false,
764765
};
765-
if (m_session.IPAddress is not null)
766-
csb.Server = m_session.IPAddress.ToString();
766+
if (session.IPAddress is not null)
767+
csb.Server = session.IPAddress.ToString();
767768
var cancellationTimeout = GetConnectionSettings().CancellationTimeout;
768769
csb.ConnectionTimeout = cancellationTimeout < 1 ? 3u : (uint) cancellationTimeout;
769770

770771
using var connection = new MySqlConnection(csb.ConnectionString);
771772
connection.Open();
772773
using var killCommand = new MySqlCommand("KILL QUERY {0}".FormatInvariant(command.Connection!.ServerThread), connection);
773774
killCommand.CommandTimeout = cancellationTimeout < 1 ? 3 : cancellationTimeout;
774-
m_session.DoCancel(command, killCommand);
775+
m_session?.DoCancel(command, killCommand);
775776
}
776777
catch (InvalidOperationException ex)
777778
{
778779
// ignore a rare race condition where the connection is open at the beginning of the method, but closed by the time
779780
// KILL QUERY is executed: https://github.com/mysql-net/MySqlConnector/issues/1002
780-
Log.Info(ex, "Session{0} ignoring cancellation for closed connection.", m_session!.Id);
781-
m_session.AbortCancel(command);
781+
Log.Info(ex, "Session{0} ignoring cancellation for closed connection.", session.Id);
782+
m_session?.AbortCancel(command);
782783
}
783784
catch (MySqlException ex)
784785
{
785786
// cancelling the query failed; setting the state back to 'Querying' will allow another call to 'Cancel' to try again
786-
Log.Info(ex, "Session{0} cancelling CommandId {1} failed", m_session!.Id, command.CommandId);
787-
m_session.AbortCancel(command);
787+
Log.Info(ex, "Session{0} cancelling CommandId {1} failed", session.Id, command.CommandId);
788+
m_session?.AbortCancel(command);
788789
}
789790
}
790791

0 commit comments

Comments
 (0)