Skip to content

fix for mutex slim being released too early during flush operation #1585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/StackExchange.Redis/PhysicalConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -875,14 +875,28 @@ private async ValueTask<WriteResult> FlushAsync_Awaited(PhysicalConnection conne
[Obsolete("this is an anti-pattern; work to reduce reliance on this is in progress")]
internal WriteResult FlushSync(bool throwOnFailure, int millisecondsTimeout)
{
var flush = FlushAsync(throwOnFailure);
if (!flush.IsCompletedSuccessfully)
using (var source = new CancellationTokenSource())
{
// here lies the evil
if (!flush.AsTask().Wait(millisecondsTimeout)) ThrowTimeout();
source.CancelAfter(TimeSpan.FromMilliseconds(millisecondsTimeout));
var flush = FlushAsync(throwOnFailure, source.Token);
if (!flush.IsCompletedSuccessfully)
{
try
{
// here lies the evil
flush.AsTask().Wait();
}
catch (AggregateException ex)
{
if (ex.InnerExceptions.Any(e => e is TaskCanceledException))
{
ThrowTimeout();
}
throw;
}
}
return flush.Result;
}
return flush.Result;

void ThrowTimeout()
{
#if DEBUG
Expand All @@ -891,7 +905,7 @@ void ThrowTimeout()
throw new TimeoutException("timeout while synchronously flushing");
}
}
internal ValueTask<WriteResult> FlushAsync(bool throwOnFailure)
internal ValueTask<WriteResult> FlushAsync(bool throwOnFailure, CancellationToken cancellationToken = default)
{
var tmp = _ioPipe?.Output;
if (tmp == null) return new ValueTask<WriteResult>(WriteResult.NoConnectionAvailable);
Expand All @@ -903,7 +917,7 @@ internal ValueTask<WriteResult> FlushAsync(bool throwOnFailure)
long flushBytes = -1;
if (_ioPipe is SocketConnection sc) flushBytes = sc.GetCounters().BytesWaitingToBeSent;
#endif
var flush = tmp.FlushAsync();
var flush = tmp.FlushAsync(cancellationToken);
if (!flush.IsCompletedSuccessfully) return FlushAsync_Awaited(this, flush, throwOnFailure
#if DEBUG
, startFlush, flushBytes
Expand Down