@@ -4642,63 +4642,84 @@ internal int GetCodePage(SqlCollation collation, TdsParserStateObject stateObj)
46424642
46434643 internal void DrainData(TdsParserStateObject stateObj)
46444644 {
4645+ #if NETFRAMEWORK
4646+ RuntimeHelpers.PrepareConstrainedRegions();
4647+ #endif
46454648 try
46464649 {
4647- SqlDataReader.SharedState sharedState = stateObj._readerState;
4648- if (sharedState != null && sharedState._dataReady)
4650+ try
46494651 {
4650- _SqlMetaDataSet metadata = stateObj._cleanupMetaData ;
4651- if (stateObj._partialHeaderBytesRead > 0 )
4652+ SqlDataReader.SharedState sharedState = stateObj._readerState ;
4653+ if (sharedState != null && sharedState._dataReady )
46524654 {
4653- if (stateObj.TryProcessHeader() != TdsOperationStatus.Done)
4655+ _SqlMetaDataSet metadata = stateObj._cleanupMetaData;
4656+ if (stateObj._partialHeaderBytesRead > 0)
46544657 {
4655- throw SQL.SynchronousCallMayNotPend();
4658+ if (stateObj.TryProcessHeader() != TdsOperationStatus.Done)
4659+ {
4660+ throw SQL.SynchronousCallMayNotPend();
4661+ }
46564662 }
4657- }
4658- if (0 == sharedState._nextColumnHeaderToRead)
4659- {
4660- // i. user called read but didn't fetch anything
4661- if (stateObj.Parser.TrySkipRow(stateObj._cleanupMetaData, stateObj) != TdsOperationStatus.Done)
4663+ if (0 == sharedState._nextColumnHeaderToRead)
46624664 {
4663- throw SQL.SynchronousCallMayNotPend();
4665+ // i. user called read but didn't fetch anything
4666+ if (stateObj.Parser.TrySkipRow(stateObj._cleanupMetaData, stateObj) != TdsOperationStatus.Done)
4667+ {
4668+ throw SQL.SynchronousCallMayNotPend();
4669+ }
46644670 }
4665- }
4666- else
4667- {
4668- // iia. if we still have bytes left from a partially read column, skip
4669- if (sharedState._nextColumnDataToRead < sharedState._nextColumnHeaderToRead)
4671+ else
46704672 {
4671- if ((sharedState._nextColumnHeaderToRead > 0) && (metadata[sharedState._nextColumnHeaderToRead - 1].metaType.IsPlp))
4673+ // iia. if we still have bytes left from a partially read column, skip
4674+ if (sharedState._nextColumnDataToRead < sharedState._nextColumnHeaderToRead)
46724675 {
4673- if (stateObj._longlen != 0 )
4676+ if ((sharedState._nextColumnHeaderToRead > 0) && (metadata[sharedState._nextColumnHeaderToRead - 1].metaType.IsPlp) )
46744677 {
4675- if (TrySkipPlpValue(ulong.MaxValue, stateObj, out _) != TdsOperationStatus.Done )
4678+ if (stateObj._longlen != 0 )
46764679 {
4677- throw SQL.SynchronousCallMayNotPend();
4680+ if (TrySkipPlpValue(ulong.MaxValue, stateObj, out _) != TdsOperationStatus.Done)
4681+ {
4682+ throw SQL.SynchronousCallMayNotPend();
4683+ }
46784684 }
46794685 }
4680- }
46814686
4682- else if (0 < sharedState._columnDataBytesRemaining)
4683- {
4684- if (stateObj.TrySkipLongBytes(sharedState._columnDataBytesRemaining) != TdsOperationStatus.Done)
4687+ else if (0 < sharedState._columnDataBytesRemaining)
46854688 {
4686- throw SQL.SynchronousCallMayNotPend();
4689+ if (stateObj.TrySkipLongBytes(sharedState._columnDataBytesRemaining) != TdsOperationStatus.Done)
4690+ {
4691+ throw SQL.SynchronousCallMayNotPend();
4692+ }
46874693 }
46884694 }
4689- }
46904695
46914696
4692- // Read the remaining values off the wire for this row
4693- if (stateObj.Parser.TrySkipRow(metadata, sharedState._nextColumnHeaderToRead, stateObj) != TdsOperationStatus.Done)
4694- {
4695- throw SQL.SynchronousCallMayNotPend();
4697+ // Read the remaining values off the wire for this row
4698+ if (stateObj.Parser.TrySkipRow(metadata, sharedState._nextColumnHeaderToRead, stateObj) != TdsOperationStatus.Done)
4699+ {
4700+ throw SQL.SynchronousCallMayNotPend();
4701+ }
46964702 }
46974703 }
4704+ Run(RunBehavior.Clean, null, null, null, stateObj);
46984705 }
4699- Run(RunBehavior.Clean, null, null, null, stateObj);
4706+ catch
4707+ {
4708+ _connHandler.DoomThisConnection();
4709+ throw;
4710+ }
4711+ }
4712+ catch (OutOfMemoryException)
4713+ {
4714+ _connHandler.DoomThisConnection();
4715+ throw;
4716+ }
4717+ catch (StackOverflowException)
4718+ {
4719+ _connHandler.DoomThisConnection();
4720+ throw;
47004721 }
4701- catch
4722+ catch (ThreadAbortException)
47024723 {
47034724 _connHandler.DoomThisConnection();
47044725 throw;
@@ -10092,7 +10113,28 @@ private void FinalizeExecuteRPC(TdsParserStateObject stateObj)
1009210113
1009310114 private void TdsExecuteRPC_OnFailure(Exception exc, TdsParserStateObject stateObj)
1009410115 {
10095- FailureCleanup(stateObj, exc);
10116+ #if NETFRAMEWORK
10117+ RuntimeHelpers.PrepareConstrainedRegions();
10118+ #endif
10119+ try
10120+ {
10121+ FailureCleanup(stateObj, exc);
10122+ }
10123+ catch (OutOfMemoryException)
10124+ {
10125+ _connHandler.DoomThisConnection();
10126+ throw;
10127+ }
10128+ catch (StackOverflowException)
10129+ {
10130+ _connHandler.DoomThisConnection();
10131+ throw;
10132+ }
10133+ catch (ThreadAbortException)
10134+ {
10135+ _connHandler.DoomThisConnection();
10136+ throw;
10137+ }
1009610138 }
1009710139
1009810140 private void ExecuteFlushTaskCallback(Task tsk, TdsParserStateObject stateObj, TaskCompletionSource<object> completion, bool releaseConnectionLock)
@@ -10103,10 +10145,32 @@ private void ExecuteFlushTaskCallback(Task tsk, TdsParserStateObject stateObj, T
1010310145 if (tsk.Exception != null)
1010410146 {
1010510147 Exception exc = tsk.Exception.InnerException;
10148+
10149+ #if NETFRAMEWORK
10150+ RuntimeHelpers.PrepareConstrainedRegions();
10151+ #endif
1010610152 try
1010710153 {
1010810154 FailureCleanup(stateObj, tsk.Exception);
1010910155 }
10156+ catch (OutOfMemoryException e)
10157+ {
10158+ _connHandler.DoomThisConnection();
10159+ completion.SetException(e);
10160+ throw;
10161+ }
10162+ catch (StackOverflowException e)
10163+ {
10164+ _connHandler.DoomThisConnection();
10165+ completion.SetException(e);
10166+ throw;
10167+ }
10168+ catch (ThreadAbortException e)
10169+ {
10170+ _connHandler.DoomThisConnection();
10171+ completion.SetException(e);
10172+ throw;
10173+ }
1011010174 catch (Exception e)
1011110175 {
1011210176 exc = e;
@@ -11767,14 +11831,35 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
1176711831
1176811832 StripPreamble(buffer, ref offset, ref count);
1176911833
11770- Task task = null;
11771- if (count > 0)
11834+ #if NETFRAMEWORK
11835+ RuntimeHelpers.PrepareConstrainedRegions();
11836+ #endif
11837+ try
1177211838 {
11773- _parser.WriteInt(count, _stateObj); // write length of chunk
11774- task = _stateObj.WriteByteArray(buffer, count, offset, canAccumulate: false);
11775- }
11839+ Task task = null;
11840+ if (count > 0)
11841+ {
11842+ _parser.WriteInt(count, _stateObj); // write length of chunk
11843+ task = _stateObj.WriteByteArray(buffer, count, offset, canAccumulate: false);
11844+ }
1177611845
11777- return task ?? Task.CompletedTask;
11846+ return task ?? Task.CompletedTask;
11847+ }
11848+ catch (OutOfMemoryException)
11849+ {
11850+ _parser._connHandler.DoomThisConnection();
11851+ throw;
11852+ }
11853+ catch (StackOverflowException)
11854+ {
11855+ _parser._connHandler.DoomThisConnection();
11856+ throw;
11857+ }
11858+ catch (ThreadAbortException)
11859+ {
11860+ _parser._connHandler.DoomThisConnection();
11861+ throw;
11862+ }
1177811863 }
1177911864
1178011865 internal static void ValidateWriteParameters(byte[] buffer, int offset, int count)
0 commit comments