@@ -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