Skip to content

Commit 5415469

Browse files
committed
Limit command cancellation attempts.
In a load-balanced environment, we may not connect to the same server when attempting to cancel an existing command, which means that cancelling will fail. Similarly, in a proxied environment, we may not have the right thread ID to cancel. Since cancellation (which opens another connection to the server) executes synchronously when the CancellationToken is checked, enforce a limit on the number of times it happens so that the canceled reader is not perpetually trying to cancel and failing.
1 parent be44bd9 commit 5415469

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public bool TryStartCancel(MySqlCommand command)
7777
VerifyState(State.Querying, State.CancelingQuery, State.Failed);
7878
if (m_state != State.Querying)
7979
return false;
80+
if (command.CancelAttemptCount++ >= 10)
81+
return false;
8082
m_state = State.CancelingQuery;
8183
}
8284

@@ -119,6 +121,7 @@ public void StartQuerying(MySqlCommand command)
119121

120122
VerifyState(State.Connected);
121123
m_state = State.Querying;
124+
command.CancelAttemptCount = 0;
122125
m_activeCommandId = command.CommandId;
123126
}
124127
}

src/MySqlConnector/MySql.Data.MySqlClient/MySqlCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ internal IDisposable RegisterCancel(CancellationToken token)
230230

231231
internal int CommandId { get; }
232232

233+
internal int CancelAttemptCount { get; set; }
234+
233235
/// <summary>
234236
/// Causes the effective command timeout to be reset back to the value specified by <see cref="CommandTimeout"/>.
235237
/// </summary>

0 commit comments

Comments
 (0)