Skip to content

Commit 6ff9177

Browse files
committed
Mark session as failed after exception.
This prevents the session from being returned to the pool with potentially-unread data on the network socket.
1 parent ad25cb4 commit 6ff9177

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/MySqlConnector/MySqlClient/MySqlDataReader.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,14 @@ private void ActivateResultSet(ResultSet resultSet)
7171
{
7272
if (resultSet.ReadResultSetHeaderException != null)
7373
{
74-
throw resultSet.ReadResultSetHeaderException is MySqlException mySqlException ?
74+
var mySqlException = resultSet.ReadResultSetHeaderException as MySqlException;
75+
76+
// for any exception not created from an ErrorPayload, mark the session as failed (because we can't guarantee that all data
77+
// has been read from the connection and that the socket is still usable)
78+
if (mySqlException?.SqlState == null)
79+
Command.Connection.Session.SetFailed();
80+
81+
throw mySqlException != null ?
7582
new MySqlException(mySqlException.Number, mySqlException.SqlState, mySqlException.Message, mySqlException) :
7683
resultSet.ReadResultSetHeaderException;
7784
}

src/MySqlConnector/Serialization/MySqlSession.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ public void FinishQuerying()
142142

143143
lock (m_lock)
144144
{
145-
VerifyState(State.Querying, State.ClearingPendingCancellation);
146-
m_state = State.Connected;
145+
if (m_state == State.Querying || m_state == State.ClearingPendingCancellation)
146+
m_state = State.Connected;
147+
else
148+
VerifyState(State.Failed);
147149
m_activeCommandId = 0;
148150
}
149151
}
@@ -788,7 +790,7 @@ private PayloadData TryAsyncContinuation(Task<ArraySegment<byte>> task)
788790
return payload;
789791
}
790792

791-
private void SetFailed()
793+
internal void SetFailed()
792794
{
793795
lock (m_lock)
794796
m_state = State.Failed;

0 commit comments

Comments
 (0)