Skip to content

Commit f9d45b3

Browse files
Handle null socket when receiving packets of data (#3270)
1 parent f6c4a0a commit f9d45b3

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
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
@@ -809,17 +809,29 @@ public override uint Send(SNIPacket packet)
809809
}
810810

811811
/// <summary>
812-
/// Receive a packet synchronously
812+
/// Receives a packet synchronously.
813813
/// </summary>
814-
/// <param name="packet">SNI packet</param>
815-
/// <param name="timeoutInMilliseconds">Timeout in Milliseconds</param>
816-
/// <returns>SNI error code</returns>
814+
/// <param name="packet">The received SNI packet.</param>
815+
/// <param name="timeoutInMilliseconds">
816+
/// Timeout in milliseconds:
817+
/// - If greater than 0, sets the socket's receive timeout to the specified value.
818+
/// - If equal to -1, represents an infinite timeout (socket timeout is set to 0).
819+
/// - If less than -1 or equal to 0, results in a timeout error.
820+
/// </param>
821+
/// <returns>SNI error code indicating the result of the operation.</returns>
817822
public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
818823
{
819824
SNIPacket errorPacket;
820825
lock (this)
821826
{
822827
packet = null;
828+
829+
if (_socket == null)
830+
{
831+
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SNITCPHandle), EventType.ERR, "Connection Id {0}, Socket is null.", args0: _connectionId);
832+
return ReportTcpSNIError(0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_10);
833+
}
834+
823835
try
824836
{
825837
if (timeoutInMilliseconds > 0)
@@ -828,8 +840,7 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
828840
}
829841
else if (timeoutInMilliseconds == -1)
830842
{
831-
// SqlClient internally represents infinite timeout by -1, and for TcpClient this is translated to a timeout of 0
832-
_socket.ReceiveTimeout = 0;
843+
_socket.ReceiveTimeout = Timeout.Infinite;
833844
}
834845
else
835846
{
@@ -884,7 +895,9 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
884895
}
885896
finally
886897
{
887-
_socket.ReceiveTimeout = 0;
898+
// Reset the socket timeout to Timeout.Infinite after the receive operation is done
899+
// to avoid blocking the thread in case of a timeout error.
900+
_socket.ReceiveTimeout = Timeout.Infinite;
888901
}
889902
}
890903
}

src/Microsoft.Data.SqlClient/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/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)