Skip to content

Commit 85a6bee

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

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
@@ -781,17 +781,29 @@ public override uint Send(SNIPacket packet)
781781
}
782782

783783
/// <summary>
784-
/// Receive a packet synchronously
784+
/// Receives a packet synchronously.
785785
/// </summary>
786-
/// <param name="packet">SNI packet</param>
787-
/// <param name="timeoutInMilliseconds">Timeout in Milliseconds</param>
788-
/// <returns>SNI error code</returns>
786+
/// <param name="packet">The received SNI packet.</param>
787+
/// <param name="timeoutInMilliseconds">
788+
/// Timeout in milliseconds:
789+
/// - If greater than 0, sets the socket's receive timeout to the specified value.
790+
/// - If equal to -1, represents an infinite timeout (socket timeout is set to 0).
791+
/// - If less than -1 or equal to 0, results in a timeout error.
792+
/// </param>
793+
/// <returns>SNI error code indicating the result of the operation.</returns>
789794
public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
790795
{
791796
SNIPacket errorPacket;
792797
lock (this)
793798
{
794799
packet = null;
800+
801+
if (_socket == null)
802+
{
803+
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SNITCPHandle), EventType.ERR, "Connection Id {0}, Socket is null.", args0: _connectionId);
804+
return ReportTcpSNIError(0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_10);
805+
}
806+
795807
try
796808
{
797809
if (timeoutInMilliseconds > 0)
@@ -800,8 +812,7 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
800812
}
801813
else if (timeoutInMilliseconds == -1)
802814
{
803-
// SqlClient internally represents infinite timeout by -1, and for TcpClient this is translated to a timeout of 0
804-
_socket.ReceiveTimeout = 0;
815+
_socket.ReceiveTimeout = Timeout.Infinite;
805816
}
806817
else
807818
{
@@ -856,7 +867,9 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
856867
}
857868
finally
858869
{
859-
_socket.ReceiveTimeout = 0;
870+
// Reset the socket timeout to Timeout.Infinite after the receive operation is done
871+
// to avoid blocking the thread in case of a timeout error.
872+
_socket.ReceiveTimeout = Timeout.Infinite;
860873
}
861874
}
862875
}

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)