internal void FailureCleanup(TdsParserStateObject stateObj, Exception e)
{
int old_outputPacketNumber = stateObj._outputPacketNumber;
if (stateObj.HasOpenResult)
{ // Need to decrement openResultCount if operation failed.
stateObj.DecrementOpenResultCount();
}
// be sure to wipe out our buffer if we started sending stuff
stateObj.ResetBuffer();
stateObj._outputPacketNumber = 1; // end of message - reset to 1 - per ramas
if (old_outputPacketNumber != 1 && _state == TdsParserState.OpenLoggedIn)
{
Debug.Assert(_connHandler._parserLock.ThreadMayHaveLock(), "Should not be calling into FailureCleanup without first taking the parser lock");
bool originalThreadHasParserLock = _connHandler.ThreadHasParserLockForClose;
try
{
// Need to set this to true such that if we have an error sending\processing the attention, we won't deadlock ourselves
_connHandler.ThreadHasParserLockForClose = true;
// If _outputPacketNumber prior to ResetBuffer was not equal to 1, a packet was already
// sent to the server and so we need to send an attention and process the attention ack.
stateObj.SendAttention();
ProcessAttention(stateObj);
}
finally
{
// Reset the ThreadHasParserLock value in case our caller expects it to be set\not set
_connHandler.ThreadHasParserLockForClose = originalThreadHasParserLock;
}
}
}