diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs index 103af2ed2d..b8e38978b9 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs @@ -765,17 +765,29 @@ public override uint Send(SNIPacket packet) } /// - /// Receive a packet synchronously + /// Receives a packet synchronously. /// - /// SNI packet - /// Timeout in Milliseconds - /// SNI error code + /// The received SNI packet. + /// + /// Timeout in milliseconds: + /// - If greater than 0, sets the socket's receive timeout to the specified value. + /// - If equal to -1, represents an infinite timeout (socket timeout is set to 0). + /// - If less than -1 or equal to 0, results in a timeout error. + /// + /// SNI error code indicating the result of the operation. public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) { SNIPacket errorPacket; lock (this) { packet = null; + + if (_socket == null) + { + SqlClientEventSource.Log.TrySNITraceEvent(nameof(SNITCPHandle), EventType.ERR, "Connection Id {0}, Socket is null.", args0: _connectionId); + return ReportTcpSNIError(0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_10); + } + try { if (timeoutInMilliseconds > 0) @@ -784,8 +796,7 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) } else if (timeoutInMilliseconds == -1) { - // SqlClient internally represents infinite timeout by -1, and for TcpClient this is translated to a timeout of 0 - _socket.ReceiveTimeout = 0; + _socket.ReceiveTimeout = Timeout.Infinite; } else { @@ -840,7 +851,9 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) } finally { - _socket.ReceiveTimeout = 0; + // Reset the socket timeout to Timeout.Infinite after the receive operation is done + // to avoid blocking the thread in case of a timeout error. + _socket.ReceiveTimeout = Timeout.Infinite; } } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.Designer.cs b/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.Designer.cs index bba9987634..67a2aa5922 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.Designer.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.Designer.cs @@ -1446,6 +1446,15 @@ internal static string SNI_ERROR_1 { } } + /// + /// Looks up a localized string similar to Socket is null. + /// + internal static string SNI_ERROR_10 { + get { + return ResourceManager.GetString("SNI_ERROR_10", resourceCulture); + } + } + /// /// Looks up a localized string similar to Timeout error. /// diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx b/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx index 28999773e0..70afac4e7d 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx +++ b/src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx @@ -927,6 +927,9 @@ Associating port with I/O completion mechanism failed + + Socket is null + Timeout error @@ -1944,4 +1947,4 @@ SQL Network Interfaces - \ No newline at end of file + diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.Designer.cs b/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.Designer.cs index b30f54f8e5..f049de08f3 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.Designer.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.Designer.cs @@ -8182,7 +8182,7 @@ internal static string SNI_ERROR_1 { } /// - /// Looks up a localized string similar to . + /// Looks up a localized string similar to Socket is null. /// internal static string SNI_ERROR_10 { get { diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx b/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx index 85627eae31..88318d6452 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx +++ b/src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx @@ -3805,7 +3805,7 @@ Associating port with I/O completion mechanism failed - + Socket is null Timeout error