Skip to content

Commit

Permalink
[2.1.6] | Fix async deadlock issue when sending attention fails due t…
Browse files Browse the repository at this point in the history
…o network errors (#1767)
  • Loading branch information
cheenamalhotra authored Apr 24, 2023
1 parent e32dc56 commit 872a9bc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public TimeoutState(int value)

private const int AttentionTimeoutSeconds = 5;

private static readonly ContextCallback s_readAdyncCallbackComplete = ReadAsyncCallbackComplete;
private static readonly ContextCallback s_readAsyncCallbackComplete = ReadAsyncCallbackComplete;

// Ticks to consider a connection "good" after a successful I/O (10,000 ticks = 1 ms)
// The resolution of the timer is typically in the range 10 to 16 milliseconds according to msdn.
Expand Down Expand Up @@ -2308,9 +2308,9 @@ private void OnTimeoutAsync(object state)
}
}

private bool OnTimeoutSync()
private bool OnTimeoutSync(bool asyncClose = false)
{
return OnTimeoutCore(TimeoutState.Running, TimeoutState.ExpiredSync);
return OnTimeoutCore(TimeoutState.Running, TimeoutState.ExpiredSync, asyncClose);
}

/// <summary>
Expand All @@ -2319,8 +2319,9 @@ private bool OnTimeoutSync()
/// </summary>
/// <param name="expectedState">the state that is the expected current state, state will change only if this is correct</param>
/// <param name="targetState">the state that will be changed to if the expected state is correct</param>
/// <param name="asyncClose">any close action to be taken by an async task to avoid deadlock.</param>
/// <returns>boolean value indicating whether the call changed the timeout state</returns>
private bool OnTimeoutCore(int expectedState, int targetState)
private bool OnTimeoutCore(int expectedState, int targetState, bool asyncClose = false)
{
Debug.Assert(targetState == TimeoutState.ExpiredAsync || targetState == TimeoutState.ExpiredSync, "OnTimeoutCore must have an expiry state as the targetState");

Expand Down Expand Up @@ -2353,7 +2354,7 @@ private bool OnTimeoutCore(int expectedState, int targetState)
{
try
{
SendAttention(mustTakeWriteLock: true);
SendAttention(mustTakeWriteLock: true, asyncClose);
}
catch (Exception e)
{
Expand Down Expand Up @@ -2893,7 +2894,7 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
// synchrnously and then call OnTimeoutSync to force an atomic change of state.
if (TimeoutHasExpired)
{
OnTimeoutSync();
OnTimeoutSync(true);
}

// try to change to the stopped state but only do so if currently in the running state
Expand Down Expand Up @@ -2924,7 +2925,7 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
{
if (_executionContext != null)
{
ExecutionContext.Run(_executionContext, s_readAdyncCallbackComplete, source);
ExecutionContext.Run(_executionContext, s_readAsyncCallbackComplete, source);
}
else
{
Expand Down Expand Up @@ -3407,7 +3408,7 @@ private void CancelWritePacket()

#pragma warning disable 0420 // a reference to a volatile field will not be treated as volatile

private Task SNIWritePacket(PacketHandle packet, out uint sniError, bool canAccumulate, bool callerHasConnectionLock)
private Task SNIWritePacket(PacketHandle packet, out uint sniError, bool canAccumulate, bool callerHasConnectionLock, bool asyncClose = false)
{
// Check for a stored exception
var delayedException = Interlocked.Exchange(ref _delayedWriteAsyncCallbackException, null);
Expand Down Expand Up @@ -3500,7 +3501,7 @@ private Task SNIWritePacket(PacketHandle packet, out uint sniError, bool canAccu
{
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.WritePacket|Info> write async returned error code {0}", (int)error);
AddError(_parser.ProcessSNIError(this));
ThrowExceptionAndWarning();
ThrowExceptionAndWarning(false, asyncClose);
}
AssertValidState();
completion.SetResult(null);
Expand Down Expand Up @@ -3535,7 +3536,7 @@ private Task SNIWritePacket(PacketHandle packet, out uint sniError, bool canAccu
{
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.WritePacket|Info> write async returned error code {0}", (int)sniError);
AddError(_parser.ProcessSNIError(this));
ThrowExceptionAndWarning(callerHasConnectionLock);
ThrowExceptionAndWarning(callerHasConnectionLock, asyncClose);
}
AssertValidState();
}
Expand All @@ -3547,7 +3548,7 @@ private Task SNIWritePacket(PacketHandle packet, out uint sniError, bool canAccu
internal abstract uint WritePacket(PacketHandle packet, bool sync);

// Sends an attention signal - executing thread will consume attn.
internal void SendAttention(bool mustTakeWriteLock = false)
internal void SendAttention(bool mustTakeWriteLock = false, bool asyncClose = false)
{
if (!_attentionSent)
{
Expand Down Expand Up @@ -3589,7 +3590,7 @@ internal void SendAttention(bool mustTakeWriteLock = false)

uint sniError;
_parser._asyncWrite = false; // stop async write
SNIWritePacket(attnPacket, out sniError, canAccumulate: false, callerHasConnectionLock: false);
SNIWritePacket(attnPacket, out sniError, canAccumulate: false, callerHasConnectionLock: false, asyncClose);
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.SendAttention|INFO> Send Attention ASync.");
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2390,9 +2390,9 @@ private void OnTimeoutAsync(object state)
}
}

private bool OnTimeoutSync()
private bool OnTimeoutSync(bool asyncClose = false)
{
return OnTimeoutCore(TimeoutState.Running, TimeoutState.ExpiredSync);
return OnTimeoutCore(TimeoutState.Running, TimeoutState.ExpiredSync, asyncClose);
}

/// <summary>
Expand All @@ -2401,8 +2401,9 @@ private bool OnTimeoutSync()
/// </summary>
/// <param name="expectedState">the state that is the expected current state, state will change only if this is correct</param>
/// <param name="targetState">the state that will be changed to if the expected state is correct</param>
/// <param name="asyncClose">any close action to be taken by an async task to avoid deadlock.</param>
/// <returns>boolean value indicating whether the call changed the timeout state</returns>
private bool OnTimeoutCore(int expectedState, int targetState)
private bool OnTimeoutCore(int expectedState, int targetState, bool asyncClose = false)
{
Debug.Assert(targetState == TimeoutState.ExpiredAsync || targetState == TimeoutState.ExpiredSync, "OnTimeoutCore must have an expiry state as the targetState");

Expand Down Expand Up @@ -2436,7 +2437,7 @@ private bool OnTimeoutCore(int expectedState, int targetState)
{
try
{
SendAttention(mustTakeWriteLock: true);
SendAttention(mustTakeWriteLock: true, asyncClose);
}
catch (Exception e)
{
Expand Down Expand Up @@ -2978,7 +2979,7 @@ public void ReadAsyncCallback(IntPtr key, IntPtr packet, UInt32 error)
// synchrnously and then call OnTimeoutSync to force an atomic change of state.
if (TimeoutHasExpired)
{
OnTimeoutSync();
OnTimeoutSync(asyncClose: true);
}

// try to change to the stopped state but only do so if currently in the running state
Expand Down Expand Up @@ -3464,7 +3465,7 @@ private void CancelWritePacket()

#pragma warning disable 420 // a reference to a volatile field will not be treated as volatile

private Task SNIWritePacket(SNIHandle handle, SNIPacket packet, out UInt32 sniError, bool canAccumulate, bool callerHasConnectionLock)
private Task SNIWritePacket(SNIHandle handle, SNIPacket packet, out UInt32 sniError, bool canAccumulate, bool callerHasConnectionLock, bool asyncClose = false)
{
// Check for a stored exception
var delayedException = Interlocked.Exchange(ref _delayedWriteAsyncCallbackException, null);
Expand Down Expand Up @@ -3555,7 +3556,7 @@ private Task SNIWritePacket(SNIHandle handle, SNIPacket packet, out UInt32 sniEr
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.WritePacket|Info> write async returned error code {0}", (int)error);

AddError(_parser.ProcessSNIError(this));
ThrowExceptionAndWarning();
ThrowExceptionAndWarning(false, asyncClose);
}
AssertValidState();
completion.SetResult(null);
Expand Down Expand Up @@ -3592,7 +3593,7 @@ private Task SNIWritePacket(SNIHandle handle, SNIPacket packet, out UInt32 sniEr
{
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.WritePacket|Info> write async returned error code {0}", (int)sniError);
AddError(_parser.ProcessSNIError(this));
ThrowExceptionAndWarning(callerHasConnectionLock);
ThrowExceptionAndWarning(callerHasConnectionLock, false);
}
AssertValidState();
}
Expand All @@ -3602,7 +3603,7 @@ private Task SNIWritePacket(SNIHandle handle, SNIPacket packet, out UInt32 sniEr
#pragma warning restore 420

// Sends an attention signal - executing thread will consume attn.
internal void SendAttention(bool mustTakeWriteLock = false)
internal void SendAttention(bool mustTakeWriteLock = false, bool asyncClose = false)
{
if (!_attentionSent)
{
Expand Down Expand Up @@ -3649,7 +3650,7 @@ internal void SendAttention(bool mustTakeWriteLock = false)

UInt32 sniError;
_parser._asyncWrite = false; // stop async write
SNIWritePacket(Handle, attnPacket, out sniError, canAccumulate: false, callerHasConnectionLock: false);
SNIWritePacket(Handle, attnPacket, out sniError, canAccumulate: false, callerHasConnectionLock: false, asyncClose);
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.SendAttention|{0}> Send Attention ASync.", "Info");
}
finally
Expand Down

0 comments on commit 872a9bc

Please sign in to comment.