diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs
index 2957881405..75cb5dd82b 100644
--- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs
+++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs
@@ -146,6 +146,7 @@ public override void Dispose()
public override uint Receive(out SNIPacket packet, int timeout)
{
+ SNIPacket errorPacket;
lock (this)
{
packet = null;
@@ -156,17 +157,23 @@ public override uint Receive(out SNIPacket packet, int timeout)
if (packet.Length == 0)
{
+ errorPacket = packet;
+ packet = null;
var e = new Win32Exception();
- return ReportErrorAndReleasePacket(packet, (uint)e.NativeErrorCode, 0, e.Message);
+ return ReportErrorAndReleasePacket(errorPacket, (uint)e.NativeErrorCode, 0, e.Message);
}
}
catch (ObjectDisposedException ode)
{
- return ReportErrorAndReleasePacket(packet, ode);
+ errorPacket = packet;
+ packet = null;
+ return ReportErrorAndReleasePacket(errorPacket, ode);
}
catch (IOException ioe)
{
- return ReportErrorAndReleasePacket(packet, ioe);
+ errorPacket = packet;
+ packet = null;
+ return ReportErrorAndReleasePacket(errorPacket, ioe);
}
return TdsEnums.SNI_SUCCESS;
@@ -175,6 +182,7 @@ public override uint Receive(out SNIPacket packet, int timeout)
public override uint ReceiveAsync(ref SNIPacket packet)
{
+ SNIPacket errorPacket;
packet = new SNIPacket(headerSize: 0, dataSize: _bufferSize);
try
@@ -184,11 +192,15 @@ public override uint ReceiveAsync(ref SNIPacket packet)
}
catch (ObjectDisposedException ode)
{
- return ReportErrorAndReleasePacket(packet, ode);
+ errorPacket = packet;
+ packet = null;
+ return ReportErrorAndReleasePacket(errorPacket, ode);
}
catch (IOException ioe)
{
- return ReportErrorAndReleasePacket(packet, ioe);
+ errorPacket = packet;
+ packet = null;
+ return ReportErrorAndReleasePacket(errorPacket, ioe);
}
}
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 91ebec426a..be6423871b 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
@@ -462,6 +462,7 @@ public override uint Send(SNIPacket packet)
/// SNI error code
public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
{
+ SNIPacket errorPacket;
lock (this)
{
packet = null;
@@ -487,24 +488,32 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
if (packet.Length == 0)
{
+ errorPacket = packet;
+ packet = null;
var e = new Win32Exception();
- return ReportErrorAndReleasePacket(packet, (uint)e.NativeErrorCode, 0, e.Message);
+ return ReportErrorAndReleasePacket(errorPacket, (uint)e.NativeErrorCode, 0, e.Message);
}
return TdsEnums.SNI_SUCCESS;
}
catch (ObjectDisposedException ode)
{
- return ReportErrorAndReleasePacket(packet, ode);
+ errorPacket = packet;
+ packet = null;
+ return ReportErrorAndReleasePacket(errorPacket, ode);
}
catch (SocketException se)
{
- return ReportErrorAndReleasePacket(packet, se);
+ errorPacket = packet;
+ packet = null;
+ return ReportErrorAndReleasePacket(errorPacket, se);
}
catch (IOException ioe)
{
- uint errorCode = ReportErrorAndReleasePacket(packet, ioe);
- if (ioe.InnerException is SocketException && ((SocketException)(ioe.InnerException)).SocketErrorCode == SocketError.TimedOut)
+ errorPacket = packet;
+ packet = null;
+ uint errorCode = ReportErrorAndReleasePacket(errorPacket, ioe);
+ if (ioe.InnerException is SocketException socketException && socketException.SocketErrorCode == SocketError.TimedOut)
{
errorCode = TdsEnums.SNI_WAIT_TIMEOUT;
}
@@ -553,6 +562,7 @@ public override uint SendAsync(SNIPacket packet, bool disposePacketAfterSendAsyn
/// SNI error code
public override uint ReceiveAsync(ref SNIPacket packet)
{
+ SNIPacket errorPacket;
packet = new SNIPacket(headerSize: 0, dataSize: _bufferSize);
try
@@ -562,7 +572,9 @@ public override uint ReceiveAsync(ref SNIPacket packet)
}
catch (Exception e) when (e is ObjectDisposedException || e is SocketException || e is IOException)
{
- return ReportErrorAndReleasePacket(packet, e);
+ errorPacket = packet;
+ packet = null;
+ return ReportErrorAndReleasePacket(errorPacket, e);
}
}