Skip to content

Commit c95b0b3

Browse files
Wraith2cheenamalhotra
authored andcommitted
Change "Recieve" methods so that failures return null packets (#350)
1 parent 30db14c commit c95b0b3

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public override void Dispose()
146146

147147
public override uint Receive(out SNIPacket packet, int timeout)
148148
{
149+
SNIPacket errorPacket;
149150
lock (this)
150151
{
151152
packet = null;
@@ -156,17 +157,23 @@ public override uint Receive(out SNIPacket packet, int timeout)
156157

157158
if (packet.Length == 0)
158159
{
160+
errorPacket = packet;
161+
packet = null;
159162
var e = new Win32Exception();
160-
return ReportErrorAndReleasePacket(packet, (uint)e.NativeErrorCode, 0, e.Message);
163+
return ReportErrorAndReleasePacket(errorPacket, (uint)e.NativeErrorCode, 0, e.Message);
161164
}
162165
}
163166
catch (ObjectDisposedException ode)
164167
{
165-
return ReportErrorAndReleasePacket(packet, ode);
168+
errorPacket = packet;
169+
packet = null;
170+
return ReportErrorAndReleasePacket(errorPacket, ode);
166171
}
167172
catch (IOException ioe)
168173
{
169-
return ReportErrorAndReleasePacket(packet, ioe);
174+
errorPacket = packet;
175+
packet = null;
176+
return ReportErrorAndReleasePacket(errorPacket, ioe);
170177
}
171178

172179
return TdsEnums.SNI_SUCCESS;
@@ -175,6 +182,7 @@ public override uint Receive(out SNIPacket packet, int timeout)
175182

176183
public override uint ReceiveAsync(ref SNIPacket packet)
177184
{
185+
SNIPacket errorPacket;
178186
packet = new SNIPacket(headerSize: 0, dataSize: _bufferSize);
179187

180188
try
@@ -184,11 +192,15 @@ public override uint ReceiveAsync(ref SNIPacket packet)
184192
}
185193
catch (ObjectDisposedException ode)
186194
{
187-
return ReportErrorAndReleasePacket(packet, ode);
195+
errorPacket = packet;
196+
packet = null;
197+
return ReportErrorAndReleasePacket(errorPacket, ode);
188198
}
189199
catch (IOException ioe)
190200
{
191-
return ReportErrorAndReleasePacket(packet, ioe);
201+
errorPacket = packet;
202+
packet = null;
203+
return ReportErrorAndReleasePacket(errorPacket, ioe);
192204
}
193205
}
194206

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ public override uint Send(SNIPacket packet)
462462
/// <returns>SNI error code</returns>
463463
public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
464464
{
465+
SNIPacket errorPacket;
465466
lock (this)
466467
{
467468
packet = null;
@@ -487,24 +488,32 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds)
487488

488489
if (packet.Length == 0)
489490
{
491+
errorPacket = packet;
492+
packet = null;
490493
var e = new Win32Exception();
491-
return ReportErrorAndReleasePacket(packet, (uint)e.NativeErrorCode, 0, e.Message);
494+
return ReportErrorAndReleasePacket(errorPacket, (uint)e.NativeErrorCode, 0, e.Message);
492495
}
493496

494497
return TdsEnums.SNI_SUCCESS;
495498
}
496499
catch (ObjectDisposedException ode)
497500
{
498-
return ReportErrorAndReleasePacket(packet, ode);
501+
errorPacket = packet;
502+
packet = null;
503+
return ReportErrorAndReleasePacket(errorPacket, ode);
499504
}
500505
catch (SocketException se)
501506
{
502-
return ReportErrorAndReleasePacket(packet, se);
507+
errorPacket = packet;
508+
packet = null;
509+
return ReportErrorAndReleasePacket(errorPacket, se);
503510
}
504511
catch (IOException ioe)
505512
{
506-
uint errorCode = ReportErrorAndReleasePacket(packet, ioe);
507-
if (ioe.InnerException is SocketException && ((SocketException)(ioe.InnerException)).SocketErrorCode == SocketError.TimedOut)
513+
errorPacket = packet;
514+
packet = null;
515+
uint errorCode = ReportErrorAndReleasePacket(errorPacket, ioe);
516+
if (ioe.InnerException is SocketException socketException && socketException.SocketErrorCode == SocketError.TimedOut)
508517
{
509518
errorCode = TdsEnums.SNI_WAIT_TIMEOUT;
510519
}
@@ -553,6 +562,7 @@ public override uint SendAsync(SNIPacket packet, bool disposePacketAfterSendAsyn
553562
/// <returns>SNI error code</returns>
554563
public override uint ReceiveAsync(ref SNIPacket packet)
555564
{
565+
SNIPacket errorPacket;
556566
packet = new SNIPacket(headerSize: 0, dataSize: _bufferSize);
557567

558568
try
@@ -562,7 +572,9 @@ public override uint ReceiveAsync(ref SNIPacket packet)
562572
}
563573
catch (Exception e) when (e is ObjectDisposedException || e is SocketException || e is IOException)
564574
{
565-
return ReportErrorAndReleasePacket(packet, e);
575+
errorPacket = packet;
576+
packet = null;
577+
return ReportErrorAndReleasePacket(errorPacket, e);
566578
}
567579
}
568580

0 commit comments

Comments
 (0)