Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 8cc3ffb

Browse files
saurabh500stephentoub
authored andcommitted
Restrict the Long Running option to MARS (#27010)
* Restrict the Long Running option to MARS * Code review comments
1 parent 7406c4c commit 8cc3ffb

File tree

9 files changed

+22
-13
lines changed

9 files changed

+22
-13
lines changed

src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNIHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ internal abstract class SNIHandle
5656
/// </summary>
5757
/// <param name="packet">SNI packet</param>
5858
/// <returns>SNI error code</returns>
59-
public abstract uint ReceiveAsync(ref SNIPacket packet);
59+
public abstract uint ReceiveAsync(ref SNIPacket packet, bool isMars = false);
6060

6161
/// <summary>
6262
/// Enable SSL

src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNIMarsConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public uint ReceiveAsync(ref SNIPacket packet)
108108
{
109109
lock (this)
110110
{
111-
return _lowerHandle.ReceiveAsync(ref packet);
111+
return _lowerHandle.ReceiveAsync(ref packet, isMars: true);
112112
}
113113
}
114114

src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNIMarsHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = nul
273273
/// </summary>
274274
/// <param name="packet">SNI packet</param>
275275
/// <returns>SNI error code</returns>
276-
public override uint ReceiveAsync(ref SNIPacket packet)
276+
public override uint ReceiveAsync(ref SNIPacket packet, bool isMars = true)
277277
{
278278
lock (_receivedPacketQueue)
279279
{

src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNINpHandle.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public override uint Receive(out SNIPacket packet, int timeout)
177177
}
178178
}
179179

180-
public override uint ReceiveAsync(ref SNIPacket packet)
180+
public override uint ReceiveAsync(ref SNIPacket packet, bool isMars = false)
181181
{
182182
lock (this)
183183
{
@@ -186,7 +186,7 @@ public override uint ReceiveAsync(ref SNIPacket packet)
186186

187187
try
188188
{
189-
packet.ReadFromStreamAsync(_stream, _receiveCallback);
189+
packet.ReadFromStreamAsync(_stream, _receiveCallback, isMars);
190190
return TdsEnums.SNI_SUCCESS_IO_PENDING;
191191
}
192192
catch (ObjectDisposedException ode)
@@ -356,4 +356,4 @@ public override void KillConnection()
356356
}
357357
#endif
358358
}
359-
}
359+
}

src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNIPacket.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,18 @@ public void Reset()
236236
/// </summary>
237237
/// <param name="stream">Stream to read from</param>
238238
/// <param name="callback">Completion callback</param>
239-
public void ReadFromStreamAsync(Stream stream, SNIAsyncCallback callback)
239+
public void ReadFromStreamAsync(Stream stream, SNIAsyncCallback callback, bool isMars)
240240
{
241241
bool error = false;
242+
TaskContinuationOptions options = TaskContinuationOptions.DenyChildAttach;
243+
// MARS operations during Sync ADO.Net API calls are Sync over Async. Each API call can request
244+
// threads to execute the async reads. MARS operations do not get the threads quickly enough leading to timeout
245+
// To fix the MARS thread exhaustion issue LongRunning continuation option is a temporary solution with its own drawbacks,
246+
// and should be removed after evaluating how to fix MARS threading issues efficiently
247+
if (isMars)
248+
{
249+
options |= TaskContinuationOptions.LongRunning;
250+
}
242251

243252
stream.ReadAsync(_data, 0, _capacity).ContinueWith(t =>
244253
{
@@ -267,7 +276,7 @@ public void ReadFromStreamAsync(Stream stream, SNIAsyncCallback callback)
267276
callback(this, error ? TdsEnums.SNI_ERROR : TdsEnums.SNI_SUCCESS);
268277
},
269278
CancellationToken.None,
270-
TaskContinuationOptions.DenyChildAttach | TaskContinuationOptions.LongRunning,
279+
options,
271280
TaskScheduler.Default);
272281
}
273282

src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNIProxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ private SNINpHandle CreateNpHandle(DataSource details, long timerExpire, object
424424
/// <param name="handle">SNI handle</param>
425425
/// <param name="packet">Packet</param>
426426
/// <returns>SNI error status</returns>
427-
public uint ReadAsync(SNIHandle handle, out SNIPacket packet)
427+
public uint ReadAsync(SNIHandle handle, out SNIPacket packet, bool isMars = false)
428428
{
429429
packet = new SNIPacket(null);
430430

src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNITcpHandle.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = nul
577577
/// </summary>
578578
/// <param name="packet">SNI packet</param>
579579
/// <returns>SNI error code</returns>
580-
public override uint ReceiveAsync(ref SNIPacket packet)
580+
public override uint ReceiveAsync(ref SNIPacket packet, bool isMars = false)
581581
{
582582
lock (this)
583583
{
@@ -586,7 +586,7 @@ public override uint ReceiveAsync(ref SNIPacket packet)
586586

587587
try
588588
{
589-
packet.ReadFromStreamAsync(_stream, _receiveCallback);
589+
packet.ReadFromStreamAsync(_stream, _receiveCallback, isMars);
590590
return TdsEnums.SNI_SUCCESS_IO_PENDING;
591591
}
592592
catch (ObjectDisposedException ode)

src/System.Data.SqlClient/src/System/Data/SqlClient/TdsParserStateObjectFactory.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal sealed class TdsParserStateObjectFactory
1919
//private static bool shouldUseLegacyNetorking;
2020
//public static bool UseManagedSNI { get; } = AppContext.TryGetSwitch(UseLegacyNetworkingOnWindows, out shouldUseLegacyNetorking) ? !shouldUseLegacyNetorking : true;
2121

22-
public static bool UseManagedSNI { get; } = false;
22+
public static bool UseManagedSNI { get; } = true;
2323

2424
public EncryptionOptions EncryptionOptions
2525
{

src/System.Data.SqlClient/src/System/Data/SqlClient/TdsParserStateObjectManaged.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ internal override uint CheckConnection()
160160
internal override object ReadAsync(out uint error, ref object handle)
161161
{
162162
SNIPacket packet;
163-
error = SNIProxy.Singleton.ReadAsync((SNIHandle)handle, out packet);
163+
error = SNIProxy.Singleton.ReadAsync((SNIHandle)handle, out packet, isMars: _parser.MARSOn);
164164
return packet;
165165
}
166166

0 commit comments

Comments
 (0)