Skip to content

Fix managed TCP sends after connection disposal - #5067

Open
mdaigle wants to merge 1 commit into
mainfrom
dev/mdaigle/fix-disposed-tcp-send
Open

mdaigle wants to merge 1 commit into
mainfrom
dev/mdaigle/fix-disposed-tcp-send

Conversation

@mdaigle

@mdaigle mdaigle commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

🤖 This pull request was created by a fully automated workflow.

Description

Fix a managed-SNI send-side teardown error exposed when a streaming MARS reader sends an ACK after the physical TCP handle has been disposed. Dispose clears _stream; Send previously passed that null reference to SniPacket.WriteToStream, leaking NullReferenceException instead of reporting a connection error.

Capture and validate the stream inside the existing send exception handler. A disposed transport now follows the existing ObjectDisposedException tracing and SNI error path. Locking, attention handling, asynchronous send behavior, packet ownership and public APIs are unchanged. No dependencies, localization, pipeline definitions or test categories change.

Issues

Addresses the send-side failure reported in #49, closed for lack of a reproduction. No duplicate issue was opened. The current individual CI failure is MarsReceiveTeardownTest.FatalError_WhileMarsReaderStreams_DoesNotFaultReceivePump(async: False, pooling: True, delay: 29) on Linux/net10.0 managed SNI.

Testing

Added a controlled disposal-before-send regression for ordinary and out-of-band packets, plus a healthy-send control. Before correction, both disposed cases failed with the same SniPacket.WriteToStream null dereference; the healthy control passed. This reproduces the transport ownership violation without relying on the integration test's scheduling.

dotnet test src\Microsoft.Data.SqlClient\tests\UnitTests\Microsoft.Data.SqlClient.UnitTests.csproj --no-restore -f net10.0 -c Release --filter "FullyQualifiedName~SniTcpHandleSendTests|FullyQualifiedName~SniReceiveTests|FullyQualifiedName~SniTcpHandleParallelConnectTests" -v:q

Passed: 33 tests, zero failures/skips, on Windows/.NET 10 in both Release and Debug (omit -c Release). --list-tests verified all three new cases. Existing receive tests cover synchronous and asynchronous teardown paths.

$env:TEST_MDS_CONFIG = Join-Path (Get-Location) 'mars-send-validation.jsonc'
dotnet test src\Microsoft.Data.SqlClient\tests\ManualTests\Microsoft.Data.SqlClient.ManualTests.csproj --no-restore -f net10.0 --filter "FullyQualifiedName~MarsReceiveTeardownTest" -v:q --blame-hang-timeout 2m

Passed: all eight original integration cases, zero skipped, with Windows managed SNI and SQL Server 17.0.1155.3. Temporary configuration selected localhost/master, integrated authentication and UseManagedSNIOnWindows=true; no credentials were stored.

dotnet build src\Microsoft.Data.SqlClient\src\Microsoft.Data.SqlClient.csproj --no-restore -f net462 -c Release -v:q — passed, zero warnings/errors. The .NET Framework/native SNI implementations are unchanged.

Scoped Linux CI succeeded on the exact repair SHA below, Debug/Project, .NET 10 managed SNI, attempt 1 (2026-11-13 UTC). Individually verified all three new unit cases and all eight original integration cases, including the previously failing case. Result totals reconcile: 2,560 unit and 1,239 integration passes; 18/127 unrelated not-executed results are not counted as coverage. One passing matrix run does not establish indefinite race stability.

Guidelines

Please review the contribution guidelines before submitting a pull request:

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 13, 2026 18:13
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Nov 13, 2026
@mdaigle mdaigle added this to the 8.1.0-preview1 milestone Nov 13, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the disposed-connection send fix and its associated regression tests.

No open issues 🎉

What changed in this PR

This PR fixes managed-SNI synchronous TCP sends after connection disposal, preventing a null-reference exception during MARS teardown and reporting a connection error instead.

Changes:

  • Capture and validate the stream within the existing send exception handler.
  • Add disposal-before-send tests for ordinary and out-of-band packets, plus a healthy-send control that checks data delivery and packet ownership.
File Description
src/​Microsoft.Data.SqlClient/​tests/​UnitTests/​Microsoft/​Data/​SqlClient/​ManagedSni/​SniTcpHandleSendTests.cs Adds disposed-send regressions and healthy-send coverage.
src/​Microsoft.Data.SqlClient/​src/​Microsoft/​Data/​SqlClient/​ManagedSni/​SniTcpHandle.netcore.cs Routes a cleared stream through existing disposed-connection error handling.

Review effort: Balanced

@mdaigle
mdaigle marked this pull request as ready for review November 13, 2026 18:54
@mdaigle
mdaigle requested a review from a team as a code owner November 13, 2026 18:54
@mdaigle mdaigle added the Area\Managed SNI Issues that are targeted to the Managed SNI codebase. label Nov 13, 2026
@codecov

codecov Bot commented Nov 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.96%. Comparing base (17bef7b) to head (11ab65d).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5067      +/-   ##
==========================================
+ Coverage   68.99%   69.96%   +0.97%     
==========================================
  Files         314      314              
  Lines       45857    69822   +23965     
==========================================
+ Hits        31638    48854   +17216     
- Misses      14219    20968    +6749     
Flag Coverage Δ
CI-SqlClient ?
sqlclient-pr 69.96% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

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

Labels

Area\Managed SNI Issues that are targeted to the Managed SNI codebase.

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

4 participants