Skip to content

MARS Sessions Aren't Closed on Return To Pool #3682

Open

Description

@benrr101

Describe the bug

I've been working on re-enabling and rewriting some of the tests that were listed as having active issues. The most recent ones were in the MARSSessionPoolingTests. After picking apart the code and figuring out what each one was testing, I was able to boil it down pretty well. However, I noticed that all the tests began failing if I changed the helper method to always use the same connection string instead of passing it in. This led me down a rabbit hole...

These tests are structured to open a connection, open and execute a sequence of commands. There are effectively two kinds of tests:

  • Tests that execute then immediately close the results
    • These all use the same connection string
    • Test that only one MARS session is opened, and reused for the lifetime of the test
  • Tests that execute but do not close the results
    • These use separate connection strings for each test
    • Test that a new MARS session is opened for each new command that is executed.

For this latter class of tests, if they are reconfigured to use the same connection string as the other tests, all the tests begin to fail. They fail on the assertion that only one MARS session is open. Since the tests all use the same connection string, it means they're pulling from the same connection back out of the pool. This means that even after the connection is returned to the pool, the MARS sessions stay open.

As an aside, the tests previously worked because they would tack one or two spaces onto the end of the connection string, ensuring they would get a new connection pool that hadn't been poisoned by other tests in the series.

To confirm this, I ran a test that only opens one reader, then closes it, closes the connection, reopens it, disposes it, but hits a breakpoint before ending the test. I did a packet capture while running this,: no MARS FIN packets are sent regardless of what happens to the connection, until the test completes (ie, the pool is closed).

According to @David-Engel and @saurabh500 this is not expected behavior!!

To reproduce

Use the attached, rewritten test file. Run all tests and see how they blow up. Run individually and see how they pass. Exclusively running both cases for ExecuteReader_NoCloses will show how the connection is poisoned.

Expected behavior

All tests should pass :) But expected behavior is also that when the connection is closed (ie, returned to the pool) all MARS sessions are closed.

Further technical details

Microsoft.Data.SqlClient v7.0.0-preview1, but likely has been an issue for much longer
.NET 8, 9, and 4.6.2
Windows 11 Enterprise
SQL Server 2022 developer edition, with localhost TCP connection

MARSSessionPoolingTest.cs.txt

Activity

  1. added theissue type on Oct 10, 2025
  2. David-Engel commented on Oct 10, 2025

    @David-Engel
    Contributor

    I have to take back what I said initially about MARS session cleanup. A MARS session is not the same as an open result set. (Obviously, but need to state this to connect the dots as even I was overlapping them in my head.) MARS sessions have to be opened on the server as a client holds open result sets. So leaving MARS sessions open on an existing connection that gets re-used is a perf enhancement. SqlClient can re-use those sessions, as needed. There is no harm in keeping them. There is documentation that talks about this session cache: Enabling Multiple Active Result Sets - ADO.NET Provider for SQL Server | Microsoft Learn

  3. changed the issue type fromtoon Oct 21, 2025
  4. moved this from To triage to Backlog in SqlClient Boardon Oct 21, 2025
  5. benrr101 commented on Nov 6, 2025

    @benrr101
    ContributorAuthor

    Alrighty, after looking over the code and behaviors with a fine tooth comb, I've come up with this matrix of expected behaviors:

    Method After Method Current Behavior Notes
    ExecuteScalar command.Dispose() Sessions remain constant Result is closed at end, releasing MARS session for re-use
    ExecuteScalar connection.Close() Sessions remain constant Same as above
    ExecuteNonQuery command.Dispose() Sessions remain constant Same as above
    ExecuteNonQuery connection.Close() Sessions remain constant Same as above
    ExecuteReader reader.Close() Sessions remain constant Closing reader closes the result, releaseing MARS session for re-use
    ExecuteReader reader.Dispose() Sessions remain constant Disposing reader calls reader.Close(), so same reason as above
    ExecuteReader GC Collection of reader Session count increases Finalizing data reader does not close it as there is no finalizer. So result is left open, causing subsequent requests to open new MARS sessions
    ExecuteReader command.Dispose() Session count increases Command does not own reader, so disposing command does not close the reader. Subsequent requests open new MARS sessions.
    ExecuteReader connection.Close() Sessions remain constant Closing connection completes pending requests, releaseing MARS session for re-use
    ExecuteReader Nothing Session count increases Doing nothing leaves the result open, causing subsequent requests to open new MARS sessions

    The only one I'm unsure about is the GC collection of the reader - it seems like we would want a finalizer that calls Dispose? But if we add that, would that be a breaking change? @David-Engel, @saurabh500

  6. changed the issue type fromtoon Nov 21, 2025
  7. priyankatiwari08 commented on Jun 11, 2026

    @priyankatiwari08
    Contributor

    /triage this issue

  8. github-actions commented on Jun 11, 2026

    @github-actions
  9. saurabh500 commented on Jun 11, 2026

    @saurabh500
    Collaborator

    /triage again (sorry I am just testing you)

  10. github-actions commented on Jun 11, 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 None found
    Regression Not indicated — reporter notes it has "likely been an issue for much longer"

    Analysis

    This issue has been significantly refined through discussion. The original concern — MARS sessions persisting after a connection is returned to the pool — was clarified by @David-Engel as expected behavior (a documented performance optimization that caches sessions for re-use). The real remaining bugs, identified by @benrr101 in their detailed behavior matrix, are: (1) SqlDataReader lacks a finalizer, so readers that are abandoned without explicit Close()/Dispose() never release their underlying MARS sessions, causing unbounded session growth; and (2) SqlCommand.Dispose() does not cascade to close associated open readers, meaning callers who rely on command disposal for cleanup inadvertently leave sessions open. These are resource-leak bugs affecting any MARS-enabled workload where readers are not explicitly closed, rated P2.

    Next Steps

    • Assign this issue to the Copilot coding agent to investigate and implement fixes for the two specific behaviors flagged in the behavior matrix (comment #3499871559):
      1. SqlDataReader finalizer — evaluate adding a finalizer that calls Dispose() on a GC'd reader; assess breaking-change risk and whether a SafeHandle-based approach is more appropriate.
      2. SqlCommand.Dispose() reader ownership — evaluate whether SqlCommand should track and close any SqlDataReader it spawned upon disposal; review the ADO.NET ownership model and assess impact.
    • Update MARSSessionPoolingTest with the complete verified behavior matrix from the reporter's comment to lock in expected behaviors as regression tests.
    • Once the fixes are scoped, check whether behavior changes need AppContext compatibility switches (per LocalAppContextSwitches.cs patterns in the codebase).

    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 issue #3682 · ● 2.3M · ◷

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions