@@ -746,13 +746,14 @@ internal ServerSession Session
746
746
747
747
internal void Cancel ( ICancellableCommand command , int commandId , bool isCancel )
748
748
{
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 ) )
750
751
{
751
752
Log . Trace ( "Ignoring cancellation for closed connection or invalid CommandId {0}" , commandId ) ;
752
753
return ;
753
754
}
754
755
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" ) ;
756
757
757
758
try
758
759
{
@@ -762,29 +763,29 @@ internal void Cancel(ICancellableCommand command, int commandId, bool isCancel)
762
763
AutoEnlist = false ,
763
764
Pooling = false ,
764
765
} ;
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 ( ) ;
767
768
var cancellationTimeout = GetConnectionSettings ( ) . CancellationTimeout ;
768
769
csb . ConnectionTimeout = cancellationTimeout < 1 ? 3u : ( uint ) cancellationTimeout ;
769
770
770
771
using var connection = new MySqlConnection ( csb . ConnectionString ) ;
771
772
connection . Open ( ) ;
772
773
using var killCommand = new MySqlCommand ( "KILL QUERY {0}" . FormatInvariant ( command . Connection ! . ServerThread ) , connection ) ;
773
774
killCommand . CommandTimeout = cancellationTimeout < 1 ? 3 : cancellationTimeout ;
774
- m_session . DoCancel ( command , killCommand ) ;
775
+ m_session ? . DoCancel ( command , killCommand ) ;
775
776
}
776
777
catch ( InvalidOperationException ex )
777
778
{
778
779
// ignore a rare race condition where the connection is open at the beginning of the method, but closed by the time
779
780
// 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 ) ;
782
783
}
783
784
catch ( MySqlException ex )
784
785
{
785
786
// 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 ) ;
788
789
}
789
790
}
790
791
0 commit comments