Skip to content

Commit a1672c3

Browse files
author
Javad Rahnama
authored
[5.2] Fix | Fix the issue with Socke.Connect in managed SNI (#2777) (#2779)
1 parent 3fee17d commit a1672c3

File tree

2 files changed

+13
-36
lines changed

2 files changed

+13
-36
lines changed

Diff for: src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs

+6-12
Original file line numberDiff line numberDiff line change
@@ -334,20 +334,14 @@ internal static bool ValidateSslServerCertificate(X509Certificate clientCert, X5
334334

335335
internal static IPAddress[] GetDnsIpAddresses(string serverName, TimeoutTimer timeout)
336336
{
337-
using (TrySNIEventScope.Create(nameof(GetDnsIpAddresses)))
337+
IPAddress[] ipAddresses = GetDnsIpAddresses(serverName);
338+
339+
// We cannot timeout accurately in sync code above, so throw TimeoutException if we've now exceeded the timeout.
340+
if (timeout.IsExpired)
338341
{
339-
int remainingTimeout = timeout.MillisecondsRemainingInt;
340-
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SNICommon), EventType.INFO,
341-
"Getting DNS host entries for serverName {0} within {1} milliseconds.",
342-
args0: serverName,
343-
args1: remainingTimeout);
344-
using CancellationTokenSource cts = new CancellationTokenSource(remainingTimeout);
345-
// using this overload to support netstandard
346-
Task<IPAddress[]> task = Dns.GetHostAddressesAsync(serverName);
347-
task.ConfigureAwait(false);
348-
task.Wait(cts.Token);
349-
return task.Result;
342+
throw new TimeoutException();
350343
}
344+
return ipAddresses;
351345
}
352346

353347
internal static IPAddress[] GetDnsIpAddresses(string serverName)

Diff for: src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs

+7-24
Original file line numberDiff line numberDiff line change
@@ -426,33 +426,16 @@ private static Socket Connect(string serverName, int port, TimeoutTimer timeout,
426426
bool isConnected;
427427
try // catching SocketException with SocketErrorCode == WouldBlock to run Socket.Select
428428
{
429-
if (isInfiniteTimeout)
429+
socket.Connect(ipAddress, port);
430+
if (!isInfiniteTimeout)
430431
{
431-
socket.Connect(ipAddress, port);
432-
}
433-
else
434-
{
435-
if (timeout.IsExpired)
436-
{
437-
return null;
438-
}
439-
// Socket.Connect does not support infinite timeouts, so we use Task to simulate it
440-
Task socketConnectTask = new Task(() => socket.Connect(ipAddress, port));
441-
socketConnectTask.ConfigureAwait(false);
442-
socketConnectTask.Start();
443-
int remainingTimeout = timeout.MillisecondsRemainingInt;
444-
if (!socketConnectTask.Wait(remainingTimeout))
445-
{
446-
throw ADP.TimeoutException($"The socket couldn't connect during the expected {remainingTimeout} remaining time.");
447-
}
448432
throw SQL.SocketDidNotThrow();
449433
}
450434

451435
isConnected = true;
452436
}
453-
catch (AggregateException aggregateException) when (!isInfiniteTimeout
454-
&& aggregateException.InnerException is SocketException socketException
455-
&& socketException.SocketErrorCode == SocketError.WouldBlock)
437+
catch (SocketException socketException) when (!isInfiniteTimeout &&
438+
socketException.SocketErrorCode == SocketError.WouldBlock)
456439
{
457440
// https://github.com/dotnet/SqlClient/issues/826#issuecomment-736224118
458441
// Socket.Select is used because it supports timeouts, while Socket.Connect does not
@@ -509,11 +492,11 @@ private static Socket Connect(string serverName, int port, TimeoutTimer timeout,
509492
return socket;
510493
}
511494
}
512-
catch (AggregateException aggregateException) when (aggregateException.InnerException is SocketException socketException)
495+
catch (SocketException e)
513496
{
514-
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SNITCPHandle), EventType.ERR, "THIS EXCEPTION IS BEING SWALLOWED: {0}", args0: socketException?.Message);
497+
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SNITCPHandle), EventType.ERR, "THIS EXCEPTION IS BEING SWALLOWED: {0}", args0: e?.Message);
515498
SqlClientEventSource.Log.TryAdvancedTraceEvent(
516-
$"{nameof(SNITCPHandle)}.{nameof(Connect)}{EventType.ERR}THIS EXCEPTION IS BEING SWALLOWED: {socketException}");
499+
$"{nameof(SNITCPHandle)}.{nameof(Connect)}{EventType.ERR}THIS EXCEPTION IS BEING SWALLOWED: {e}");
517500
}
518501
finally
519502
{

0 commit comments

Comments
 (0)