You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SqlException within concurrent call to ExecuteReader returns bad connections to the pool #1887
Under certain conditions, it's possible for a call to ExecuteReader to return results and/or errors from another instance of SqlCommand. For context, we're seeing this bug when calling Dapper's Query method. The following conditions must be met for this bug to occur:
A connection is being used in a thread-unsafe way
The two threads using the connection must be calling ExecuteReader, and at least one of the commands must throw a SqlException
A third, unrelated thread, using its own isolated SqlConnection, should run its own query at roughly the same time
Under these conditions, the third thread mentioned above may receive the error or result set from one of the first two threads' queries.
The bug occurs much more often if ExecuteReader(CommandBehavior.CloseConnection) is used.
Errors coming from thread-unsafe usage of SqlConnection shouldn't cause errors in other, well-behaving parts of the application.
Further technical details
Microsoft.Data.SqlClient version: 5.0.1
.NET target: .NET 6.0
SQL Server version: SQL Server Express, Azure SQL Managed Instance as well
Operating system: Windows 10
I was also able to reproduce this with System.Data.SqlClient.
Thanks @burns47 for bringing this issue to our attention and providing a repro sample. We're currently in the middle of a release. We'll get back to you soon.
I agree that we shouldn't expect to be able to use a connection from multiple threads - however I was hoping that the resulting undefined behavior would be isolated to the connection that you were using in an unsafe way. That doesn't seem to be the case. If you're using a SqlConnection in a thread-unsafe manner, errors can appear in any part of the application using SqlConnections, even if they're using their own connection safely. I was hoping there would be a way to limit the blast radius of such a bug!
FWIW I do agree that it's important for buggy user code to not cause completely undefined/unpredictable behavior, as much as possible; such behavior makes the bug that much harder to track down. Developers do occasionally introduce bugs in code, and it's important for components to take that into account and be as friendly/safe as possible.
Reacted by Javad Rahnama, Lawrence LCI, Cheena Malhotra and Eric Livergood
I have been looking into it, tried capturing traces and it seems to be related to the 'stateObj' re-assignment (you can run the repro in debug and notice a debug assert failing..), which seems to be causing exceptions/results left unread and flow into the pool from where next connection picks up.
I agree, this should be fixed to avoid any surprises for unintentional designs.
This issue describes a serious connection-pool corruption bug: when a SqlConnection is used unsafely across two threads and one of the concurrent ExecuteReader calls throws a SqlException, an unrelated third thread using its own separate SqlConnection can receive the leaked result set or error from the misused connection. A maintainer (@cheenamalhotra) already identified a likely root cause related to TdsParserStateObject re-assignment causing unread results/exceptions to be left on the parser and subsequently returned to the pool in a corrupted state, evidenced by a failing debug assert in the repro. Given the risk of returning "poisoned" connections from the pool to unrelated callers — impacting arbitrary parts of an application even when they use connections correctly — this should be treated as P1: high severity due to blast-radius/data-integrity concerns, though it requires the specific thread-unsafe misuse pattern to trigger.
Next Steps
Assign this issue to Copilot coding agent to investigate TdsParser/TdsParserStateObject state re-assignment during concurrent ExecuteReader calls on the same SqlConnection, specifically around how state objects are associated with a connection when an in-flight command throws a SqlException. Focus on TdsParserStateObject.AssignPendingDataStateObject/TryProcessHeader paths and SqlInternalConnectionTds/ConnectionPool return-to-pool validation (IsConnectionAlive/ValidateAndReclaim equivalents) to ensure a connection with unread/mismatched TDS state is never returned to the pool for reuse. Correlate with the difference in behavior between ExecuteReader (sync) and ExecuteReaderAsync, since the async path reportedly does not reproduce the issue — the state-object handling divergence between these paths is likely key to the fix.
Since the underlying trigger is thread-unsafe use of a single SqlConnection, the fix goal should be defensive: detect and discard corrupted connections at pool-return time rather than attempting to "support" concurrent use of one connection.
Note: This triage summary is auto-generated by an AI agent. The analysis and suggestions above have not been verified by a human maintainer. Please treat as preliminary guidance only.
Activity
lcheunglci commentedon Jan 16, 2023
Thanks @burns47 for bringing this issue to our attention and providing a repro sample. We're currently in the middle of a release. We'll get back to you soon.
JRahnama commentedon Jan 18, 2023
@burns47 I have not looked into the details of this issue, but in general Connections are not thread safe. We have had several issues such as #1758.
burns47 commentedon Jan 18, 2023
I agree that we shouldn't expect to be able to use a connection from multiple threads - however I was hoping that the resulting undefined behavior would be isolated to the connection that you were using in an unsafe way. That doesn't seem to be the case. If you're using a SqlConnection in a thread-unsafe manner, errors can appear in any part of the application using SqlConnections, even if they're using their own connection safely. I was hoping there would be a way to limit the blast radius of such a bug!
Wraith2 commentedon Jan 18, 2023
There is. Don't ever use connections in an unsafe and unsupported way and you won't have a problem.
roji commentedon Jan 18, 2023
FWIW I do agree that it's important for buggy user code to not cause completely undefined/unpredictable behavior, as much as possible; such behavior makes the bug that much harder to track down. Developers do occasionally introduce bugs in code, and it's important for components to take that into account and be as friendly/safe as possible.
lcheunglci commentedon Jan 18, 2023
Hi @burns47 , this seems like a bug, we'll investigate.
cheenamalhotra commentedon Jan 18, 2023
👀 (sneaking in..)
I have been looking into it, tried capturing traces and it seems to be related to the 'stateObj' re-assignment (you can run the repro in debug and notice a debug assert failing..), which seems to be causing exceptions/results left unread and flow into the pool from where next connection picks up.
I agree, this should be fixed to avoid any surprises for unintentional designs.
lcheunglci commentedon Jan 19, 2023
Thanks @cheenamalhotra for your investigation. We'll look into a fix after the release.
priyankatiwari08 commentedon Sep 17, 2026
/triage
github-actions commentedon Sep 17, 2026
🔍 Triage Summary (on-demand re-triage)
Area\Connection PoolingAnalysis
This issue describes a serious connection-pool corruption bug: when a
SqlConnectionis used unsafely across two threads and one of the concurrentExecuteReadercalls throws aSqlException, an unrelated third thread using its own separateSqlConnectioncan receive the leaked result set or error from the misused connection. A maintainer (@cheenamalhotra) already identified a likely root cause related toTdsParserStateObjectre-assignment causing unread results/exceptions to be left on the parser and subsequently returned to the pool in a corrupted state, evidenced by a failing debug assert in the repro. Given the risk of returning "poisoned" connections from the pool to unrelated callers — impacting arbitrary parts of an application even when they use connections correctly — this should be treated as P1: high severity due to blast-radius/data-integrity concerns, though it requires the specific thread-unsafe misuse pattern to trigger.Next Steps
TdsParser/TdsParserStateObjectstate re-assignment during concurrentExecuteReadercalls on the sameSqlConnection, specifically around how state objects are associated with a connection when an in-flight command throws aSqlException. Focus onTdsParserStateObject.AssignPendingDataStateObject/TryProcessHeaderpaths andSqlInternalConnectionTds/ConnectionPoolreturn-to-pool validation (IsConnectionAlive/ValidateAndReclaimequivalents) to ensure a connection with unread/mismatched TDS state is never returned to the pool for reuse. Correlate with the difference in behavior betweenExecuteReader(sync) andExecuteReaderAsync, since the async path reportedly does not reproduce the issue — the state-object handling divergence between these paths is likely key to the fix.SqlConnection, the fix goal should be defensive: detect and discard corrupted connections at pool-return time rather than attempting to "support" concurrent use of one connection.