Skip to content

Commit bd44051

Browse files
Handle null socket when receiving packets of data (#3270) (#3285)
Co-authored-by: Cheena Malhotra <13396919+cheenamalhotra@users.noreply.github.com>
1 parent d47c51d commit bd44051

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -765,17 +765,29 @@ public override uint Send(SNIPacket packet)
765765
}
766766

767767
/// <summary>
768-
/// Receive a packet synchronously
768+
/// Receives a packet synchronously.
769769
/// </summary>
770-
/// <param name="packet">SNI packet</param>
771-
/// <param name="timeoutInMilliseconds">Timeout in Milliseconds</param>
772-
/// <returns>SNI error code</returns>
770+
/// <param name="packet">The received SNI packet.</param>
771+
/// <param name="timeoutInMilliseconds">
772+
/// Timeout in milliseconds:
773+
/// - If greater than 0, sets the socket's receive timeout to the specified value.
774+
/// - If equal to -1, represents an infinite timeout (socket timeout is set to 0).
775+
/// - If less than -1 or equal to 0, results in a timeout error.
776+
/// </param>
777+
/// <returns>SNI error code indicating the result of the operation.</returns>
773778
public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
774779
{
775780
SNIPacket errorPacket;
776781
lock (this)
777782
{
778783
packet = null;
784+
785+
if (_socket == null)
786+
{
787+
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SNITCPHandle), EventType.ERR, "Connection Id {0}, Socket is null.", args0: _connectionId);
788+
return ReportTcpSNIError(0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_10);
789+
}
790+
779791
try
780792
{
781793
if (timeoutInMilliseconds > 0)
@@ -784,8 +796,7 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
784796
}
785797
else if (timeoutInMilliseconds == -1)
786798
{
787-
// SqlClient internally represents infinite timeout by -1, and for TcpClient this is translated to a timeout of 0
788-
_socket.ReceiveTimeout = 0;
799+
_socket.ReceiveTimeout = Timeout.Infinite;
789800
}
790801
else
791802
{
@@ -840,7 +851,9 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
840851
}
841852
finally
842853
{
843-
_socket.ReceiveTimeout = 0;
854+
// Reset the socket timeout to Timeout.Infinite after the receive operation is done
855+
// to avoid blocking the thread in case of a timeout error.
856+
_socket.ReceiveTimeout = Timeout.Infinite;
844857
}
845858
}
846859
}

src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.Data.SqlClient/netcore/src/Resources/Strings.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,9 @@
927927
<data name="SNI_ERROR_9" xml:space="preserve">
928928
<value>Associating port with I/O completion mechanism failed</value>
929929
</data>
930+
<data name="SNI_ERROR_10" xml:space="preserve">
931+
<value>Socket is null</value>
932+
</data>
930933
<data name="SNI_ERROR_11" xml:space="preserve">
931934
<value>Timeout error</value>
932935
</data>
@@ -1944,4 +1947,4 @@
19441947
<data name="SNI_PN11" xml:space="preserve">
19451948
<value>SQL Network Interfaces</value>
19461949
</data>
1947-
</root>
1950+
</root>

src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.Data.SqlClient/netfx/src/Resources/Strings.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3805,7 +3805,7 @@
38053805
<value>Associating port with I/O completion mechanism failed</value>
38063806
</data>
38073807
<data name="SNI_ERROR_10" xml:space="preserve">
3808-
<value />
3808+
<value>Socket is null</value>
38093809
</data>
38103810
<data name="SNI_ERROR_11" xml:space="preserve">
38113811
<value>Timeout error</value>

0 commit comments

Comments
 (0)