Skip to content

Commit

Permalink
only consider cancellation if it doesn't complete synchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell committed Nov 5, 2020
1 parent 3d43209 commit e1e7189
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/StackExchange.Redis/PhysicalConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ public void Dispose()
}
OnCloseEcho();
_arena.Dispose();
_reusableFlushSyncTokenSource?.Dispose();
GC.SuppressFinalize(this);
}

Expand Down Expand Up @@ -872,15 +873,17 @@ private async ValueTask<WriteResult> FlushAsync_Awaited(PhysicalConnection conne
}
}

CancellationTokenSource _reusableFlushSyncTokenSource;
[Obsolete("this is an anti-pattern; work to reduce reliance on this is in progress")]
internal WriteResult FlushSync(bool throwOnFailure, int millisecondsTimeout)
{
using (var source = new CancellationTokenSource())
{
source.CancelAfter(TimeSpan.FromMilliseconds(millisecondsTimeout));
var flush = FlushAsync(throwOnFailure, source.Token);
var cts = _reusableFlushSyncTokenSource ??= new CancellationTokenSource();
var flush = FlushAsync(throwOnFailure, cts.Token);
if (!flush.IsCompletedSuccessfully)
{
// only schedule cancellation if it doesn't complete synchronously; at this point, it is doomed
_reusableFlushSyncTokenSource = null;
cts.CancelAfter(TimeSpan.FromMilliseconds(millisecondsTimeout));
try
{
// here lies the evil
Expand All @@ -894,9 +897,13 @@ internal WriteResult FlushSync(bool throwOnFailure, int millisecondsTimeout)
}
throw;
}
finally
{
cts.Dispose();
}
}
return flush.Result;
}

void ThrowTimeout()
{
#if DEBUG
Expand Down

0 comments on commit e1e7189

Please sign in to comment.