From d088be729da2c74def8fd3f76f1060169b202012 Mon Sep 17 00:00:00 2001 From: Malcolm Daigle Date: Mon, 16 Nov 2026 08:26:44 -0800 Subject: [PATCH] Report connection errors for TCP sends after disposal (#5067) Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../ManagedSni/SniTcpHandle.netcore.cs | 3 +- .../ManagedSni/SniTcpHandleSendTests.cs | 120 ++++++++++++++++++ 2 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandleSendTests.cs diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandle.netcore.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandle.netcore.cs index ef8e10256a..68b42c4aef 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandle.netcore.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandle.netcore.cs @@ -866,7 +866,8 @@ public override uint Send(SniPacket packet) { try { - packet.WriteToStream(_stream); + Stream stream = _stream ?? throw new ObjectDisposedException(nameof(SniTcpHandle)); + packet.WriteToStream(stream); SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniTcpHandle), EventType.INFO, "Connection Id {0}, Data sent to stream synchronously", args0: _connectionId); return TdsEnums.SNI_SUCCESS; } diff --git a/src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandleSendTests.cs b/src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandleSendTests.cs new file mode 100644 index 0000000000..8e1c11f0a8 --- /dev/null +++ b/src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandleSendTests.cs @@ -0,0 +1,120 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#if NET + +using System; +using System.Net; +using System.Net.Sockets; +using Microsoft.Data.ProviderBase; +using Microsoft.Data.SqlClient.ManagedSni; +using Microsoft.Data.SqlClient.Parser; +using Xunit; + +namespace Microsoft.Data.SqlClient.UnitTests.ManagedSni +{ + /// + /// Verifies TCP sends retain their error and packet ownership contracts during teardown. + /// + public sealed class SniTcpHandleSendTests + { + /// + /// A send ordered after transport disposal must report closure instead of dereferencing + /// the cleared stream, including the out-of-band path used by attention packets. + /// + [Theory] + [InlineData(false)] + [InlineData(true)] + public void Send_AfterDispose_ReturnsConnectionError(bool outOfBand) + { + // Arrange + using TcpListener listener = new(IPAddress.Loopback, 0); + listener.Start(); + SniTcpHandle handle = CreateHandle(listener); + using Socket peer = listener.AcceptSocket(); + SniPacket packet = handle.RentPacket(0, 1); + packet.AppendData(new byte[] { 42 }, 1); + packet.IsOutOfBand = outOfBand; + handle.Dispose(); + + try + { + // Act + uint result = handle.Send(packet); + + // Assert + Assert.Equal(TdsEnums.SNI_ERROR, result); + Assert.Equal(SniProviders.TCP_PROV, SniLoadHandle.LastError.provider); + Assert.IsType(SniLoadHandle.LastError.exception); + Assert.False(packet.IsInvalid); + } + finally + { + // Cleanup + handle.ReturnPacket(packet); + handle.Dispose(); + } + } + + /// + /// A connected send still transmits its bytes and leaves the packet owned by its caller. + /// + [Fact] + public void Send_Connected_WritesPacket() + { + // Arrange + using TcpListener listener = new(IPAddress.Loopback, 0); + listener.Start(); + SniTcpHandle handle = CreateHandle(listener); + using Socket peer = listener.AcceptSocket(); + peer.ReceiveTimeout = 5000; + SniPacket packet = handle.RentPacket(0, 1); + packet.AppendData(new byte[] { 42 }, 1); + + try + { + // Act + uint result = handle.Send(packet); + + // Assert + Assert.Equal(TdsEnums.SNI_SUCCESS, result); + byte[] received = new byte[1]; + Assert.Equal(1, peer.Receive(received)); + Assert.Equal(42, received[0]); + Assert.False(packet.IsInvalid); + } + finally + { + // Cleanup + handle.ReturnPacket(packet); + handle.Dispose(); + } + } + + /// + /// Connects a managed TCP handle to a local listener without a SQL Server handshake. + /// + /// The listening loopback endpoint. + /// The connected handle owned by the caller. + private static SniTcpHandle CreateHandle(TcpListener listener) + { + SQLDNSInfo? dnsInfo = null; + SniTcpHandle handle = new( + IPAddress.Loopback.ToString(), + ((IPEndPoint)listener.LocalEndpoint).Port, + TimeoutTimer.StartNew(TimeSpan.FromSeconds(5)), + parallel: false, + SqlConnectionIPAddressPreference.IPv4First, + cachedFQDN: nameof(SniTcpHandleSendTests), + ref dnsInfo, + tlsFirst: false, + hostNameInCertificate: null, + serverCertificateFilename: null); + Assert.Equal(TdsEnums.SNI_SUCCESS, handle.Status); + return handle; + } + } +} + +#endif