Skip to content

Commit 8b91dbe

Browse files
committed
Add more logging for cancellation.
1 parent 9b400ed commit 8b91dbe

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,22 @@ public bool TryStartCancel(ICancellableCommand command)
113113

114114
public void DoCancel(ICancellableCommand commandToCancel, MySqlCommand killCommand)
115115
{
116-
Log.Info("Session{0} canceling CommandId {1}: CommandText: {2}", m_logArguments[0], commandToCancel.CommandId, (commandToCancel as MySqlCommand)?.CommandText);
116+
Log.Info("Session{0} canceling CommandId {1} from Session{2}; CommandText: {3}", m_logArguments[0], commandToCancel.CommandId, killCommand.Connection!.Session.Id, (commandToCancel as MySqlCommand)?.CommandText);
117117
lock (m_lock)
118118
{
119119
if (ActiveCommandId != commandToCancel.CommandId)
120+
{
121+
Log.Info("Session{0} ActiveCommandId {1} is not the CommandId {2} being canceled; ignoring cancellation.", m_logArguments[0], ActiveCommandId, commandToCancel.CommandId);
120122
return;
123+
}
121124

122125
// NOTE: This command is executed while holding the lock to prevent race conditions during asynchronous cancellation.
123126
// For example, if the lock weren't held, the current command could finish and the other thread could set ActiveCommandId
124127
// to zero, then start executing a new command. By the time this "KILL QUERY" command reached the server, the wrong
125128
// command would be killed (because "KILL QUERY" specifies the connection whose command should be killed, not
126129
// a unique identifier of the command itself). As a mitigation, we set the CommandTimeout to a low value to avoid
127130
// blocking the other thread for an extended duration.
131+
Log.Info("Session{0} canceling CommandId {1} with CommandText {2}", killCommand.Connection!.Session.Id, commandToCancel.CommandId, killCommand.CommandText);
128132
killCommand.ExecuteNonQuery();
129133
}
130134
}

src/MySqlConnector/MySqlCommand.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading;
77
using System.Threading.Tasks;
88
using MySqlConnector.Core;
9+
using MySqlConnector.Logging;
910
using MySqlConnector.Protocol.Serialization;
1011
using MySqlConnector.Utilities;
1112

@@ -94,7 +95,11 @@ private MySqlCommand(MySqlCommand other)
9495
public new MySqlParameter CreateParameter() => (MySqlParameter) base.CreateParameter();
9596

9697
/// <inheritdoc/>
97-
public override void Cancel() => Connection?.Cancel(this);
98+
public override void Cancel()
99+
{
100+
Log.Info("CommandId {0} for Session{1} has been canceled via Cancel().", m_commandId, Connection?.Session.Id);
101+
Connection?.Cancel(this);
102+
}
98103

99104
/// <inheritdoc/>
100105
public override int ExecuteNonQuery() => ExecuteNonQueryAsync(IOBehavior.Synchronous, CancellationToken.None).GetAwaiter().GetResult();
@@ -387,8 +392,9 @@ void ICancellableCommand.SetTimeout(int milliseconds)
387392

388393
private void CancelCommandForTimeout()
389394
{
395+
Log.Info("CommandId {0} for Session{1} has been canceled via command timeout.", m_commandId, Connection?.Session.Id);
390396
Volatile.Write(ref m_commandTimedOut, true);
391-
Cancel();
397+
Connection?.Cancel(this);
392398
}
393399

394400
private bool IsValid([NotNullWhen(false)] out Exception? exception)
@@ -414,6 +420,8 @@ private bool IsValid([NotNullWhen(false)] out Exception? exception)
414420
MySqlParameterCollection? IMySqlCommand.OutParameters { get; set; }
415421
MySqlParameter? IMySqlCommand.ReturnParameter { get; set; }
416422

423+
static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager.CreateLogger(nameof(MySqlCommand));
424+
417425
readonly int m_commandId;
418426
bool m_isDisposed;
419427
MySqlConnection? m_connection;

0 commit comments

Comments
 (0)