Skip to content

SqlException within concurrent call to ExecuteReader returns bad connections to the pool  #1887

Open

Description

@burns47

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:

  1. A connection is being used in a thread-unsafe way
  2. The two threads using the connection must be calling ExecuteReader, and at least one of the commands must throw a SqlException
  3. 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.

The bug does not occur if:

  1. Neither of the two threads throws an error
  2. ExecuteReaderAsync is used instead

To reproduce

See attached zip solution/project
ConnectionPoolPoisoning.zip

Expected behavior

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.

Activity

  1. lcheunglci commented on Jan 16, 2023

    @lcheunglci
    Contributor

    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.

  2. JRahnama commented on Jan 18, 2023

    @JRahnama
    Contributor

    @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.

  3. burns47 commented on Jan 18, 2023

    @burns47
    Author

    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!

  4. Wraith2 commented on Jan 18, 2023

    @Wraith2
    Contributor

    There is. Don't ever use connections in an unsafe and unsupported way and you won't have a problem.

  5. roji commented on Jan 18, 2023

    @roji
    Member

    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.

  6. lcheunglci commented on Jan 18, 2023

    @lcheunglci
    Contributor

    Hi @burns47 , this seems like a bug, we'll investigate.

  7. cheenamalhotra commented on Jan 18, 2023

    @cheenamalhotra
    Member

    👀 (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.

  8. lcheunglci commented on Jan 19, 2023

    @lcheunglci
    Contributor

    Thanks @cheenamalhotra for your investigation. We'll look into a fix after the release.

  9. moved this from To triage to Investigating in SqlClient Boardon Nov 20, 2025
  10. self-assigned this
    on Feb 17, 2026
  11. priyankatiwari08 commented on Sep 17, 2026

    @priyankatiwari08
    Contributor

    /triage

  12. github-actions commented on Sep 17, 2026

    @github-actions

    🔍 Triage Summary (on-demand re-triage)

    Check Result
    Issue type Bug
    Environment All required environment details provided for investigation
    Area Area\Connection Pooling
    Duplicates Potentially related: #1758
    Regression Not indicated

    Analysis

    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.
    • Recommend reviewing Starting multiple transactions in separate threads causes unexpected behavior #1758 for related thread-safety/connection-reuse corruption patterns before starting the fix, as it may share root cause or provide additional repro context.
    • 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.

    Generated by SqlClient Issue Auto-Triage for #1887 · copilot · auto · 32 AIC · ⌖ 14.5 AIC · ⊞ 11.9K · ◷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions