Skip to content

Commit db1f182

Browse files
github-actions[bot]mdaigleCopilot
authored
Report connection errors for TCP sends after disposal (#5067) (#5076)
Co-authored-by: Malcolm Daigle <mdaigle@microsoft.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 4972634 commit db1f182

2 files changed

Lines changed: 122 additions & 1 deletion

File tree

‎src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniTcpHandle.netcore.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,8 @@ public override uint Send(SniPacket packet)
877877
{
878878
try
879879
{
880-
packet.WriteToStream(_stream);
880+
Stream stream = _stream ?? throw new ObjectDisposedException(nameof(SniTcpHandle));
881+
packet.WriteToStream(stream);
881882
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniTcpHandle), EventType.INFO, "Connection Id {0}, Data sent to stream synchronously", args0: _connectionId);
882883
return TdsEnums.SNI_SUCCESS;
883884
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#if NET
6+
7+
using System;
8+
using System.Net;
9+
using System.Net.Sockets;
10+
using Microsoft.Data.ProviderBase;
11+
using Microsoft.Data.SqlClient.ManagedSni;
12+
using Microsoft.Data.SqlClient.Parser;
13+
using Xunit;
14+
15+
namespace Microsoft.Data.SqlClient.UnitTests.ManagedSni
16+
{
17+
/// <summary>
18+
/// Verifies TCP sends retain their error and packet ownership contracts during teardown.
19+
/// </summary>
20+
public sealed class SniTcpHandleSendTests
21+
{
22+
/// <summary>
23+
/// A send ordered after transport disposal must report closure instead of dereferencing
24+
/// the cleared stream, including the out-of-band path used by attention packets.
25+
/// </summary>
26+
[Theory]
27+
[InlineData(false)]
28+
[InlineData(true)]
29+
public void Send_AfterDispose_ReturnsConnectionError(bool outOfBand)
30+
{
31+
// Arrange
32+
using TcpListener listener = new(IPAddress.Loopback, 0);
33+
listener.Start();
34+
SniTcpHandle handle = CreateHandle(listener);
35+
using Socket peer = listener.AcceptSocket();
36+
SniPacket packet = handle.RentPacket(0, 1);
37+
packet.AppendData(new byte[] { 42 }, 1);
38+
packet.IsOutOfBand = outOfBand;
39+
handle.Dispose();
40+
41+
try
42+
{
43+
// Act
44+
uint result = handle.Send(packet);
45+
46+
// Assert
47+
Assert.Equal(TdsEnums.SNI_ERROR, result);
48+
Assert.Equal(SniProviders.TCP_PROV, SniLoadHandle.LastError.provider);
49+
Assert.IsType<ObjectDisposedException>(SniLoadHandle.LastError.exception);
50+
Assert.False(packet.IsInvalid);
51+
}
52+
finally
53+
{
54+
// Cleanup
55+
handle.ReturnPacket(packet);
56+
handle.Dispose();
57+
}
58+
}
59+
60+
/// <summary>
61+
/// A connected send still transmits its bytes and leaves the packet owned by its caller.
62+
/// </summary>
63+
[Fact]
64+
public void Send_Connected_WritesPacket()
65+
{
66+
// Arrange
67+
using TcpListener listener = new(IPAddress.Loopback, 0);
68+
listener.Start();
69+
SniTcpHandle handle = CreateHandle(listener);
70+
using Socket peer = listener.AcceptSocket();
71+
peer.ReceiveTimeout = 5000;
72+
SniPacket packet = handle.RentPacket(0, 1);
73+
packet.AppendData(new byte[] { 42 }, 1);
74+
75+
try
76+
{
77+
// Act
78+
uint result = handle.Send(packet);
79+
80+
// Assert
81+
Assert.Equal(TdsEnums.SNI_SUCCESS, result);
82+
byte[] received = new byte[1];
83+
Assert.Equal(1, peer.Receive(received));
84+
Assert.Equal(42, received[0]);
85+
Assert.False(packet.IsInvalid);
86+
}
87+
finally
88+
{
89+
// Cleanup
90+
handle.ReturnPacket(packet);
91+
handle.Dispose();
92+
}
93+
}
94+
95+
/// <summary>
96+
/// Connects a managed TCP handle to a local listener without a SQL Server handshake.
97+
/// </summary>
98+
/// <param name="listener">The listening loopback endpoint.</param>
99+
/// <returns>The connected handle owned by the caller.</returns>
100+
private static SniTcpHandle CreateHandle(TcpListener listener)
101+
{
102+
SQLDNSInfo? dnsInfo = null;
103+
SniTcpHandle handle = new(
104+
IPAddress.Loopback.ToString(),
105+
((IPEndPoint)listener.LocalEndpoint).Port,
106+
TimeoutTimer.StartNew(TimeSpan.FromSeconds(5)),
107+
parallel: false,
108+
SqlConnectionIPAddressPreference.IPv4First,
109+
cachedFQDN: nameof(SniTcpHandleSendTests),
110+
ref dnsInfo,
111+
tlsFirst: false,
112+
hostNameInCertificate: null,
113+
serverCertificateFilename: null);
114+
Assert.Equal(TdsEnums.SNI_SUCCESS, handle.Status);
115+
return handle;
116+
}
117+
}
118+
}
119+
120+
#endif

0 commit comments

Comments
 (0)