Skip to content

Commit

Permalink
RavenDB-22096 handle aggregate exception same way on all execution paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ppekrol committed Mar 11, 2024
1 parent 5039e68 commit 57f83d1
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions src/Raven.Client/Util/AsyncHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -32,6 +33,7 @@ public void Reset(ExclusiveSynchronizationContext value)
public static void RunSync(Func<Task> task)
{
var oldContext = SynchronizationContext.Current;
var sw = Stopwatch.StartNew();

// Do we have an active synchronization context?
if (UseTaskAwaiterWhenNoSynchronizationContextIsAvailable && oldContext == null)
Expand All @@ -43,13 +45,11 @@ public static void RunSync(Func<Task> task)
}
catch (AggregateException ex)
{
var exception = ex.ExtractSingleInnerException();
ExceptionDispatchInfo.Capture(exception).Throw();
HandleException(ex, sw);
}
return;
}

var sw = Stopwatch.StartNew();
var synch = _pool.Allocate();

SynchronizationContext.SetSynchronizationContext(synch);
Expand All @@ -75,10 +75,7 @@ public static void RunSync(Func<Task> task)
}
catch (AggregateException ex)
{
var exception = ex.ExtractSingleInnerException();
if (exception is OperationCanceledException)
throw new TimeoutException("Operation timed out after: " + sw.Elapsed, ex);
ExceptionDispatchInfo.Capture(exception).Throw();
HandleException(ex, sw);
}
finally
{
Expand All @@ -91,17 +88,23 @@ public static void RunSync(Func<Task> task)
public static T RunSync<T>(Func<Task<T>> task)
{
var oldContext = SynchronizationContext.Current;
var sw = Stopwatch.StartNew();

// Do we have an active synchronization context?
if (UseTaskAwaiterWhenNoSynchronizationContextIsAvailable && oldContext == null)
{
// We can run synchronously without any issue.
return task().GetAwaiter().GetResult();
try
{
return task().GetAwaiter().GetResult();
}
catch (AggregateException ex)
{
HandleException(ex, sw);
}
}

var result = default(T);

var sw = Stopwatch.StartNew();
var synch = _pool.Allocate();

SynchronizationContext.SetSynchronizationContext(synch);
Expand All @@ -128,10 +131,7 @@ public static T RunSync<T>(Func<Task<T>> task)
}
catch (AggregateException ex)
{
var exception = ex.ExtractSingleInnerException();
if (exception is OperationCanceledException)
throw new TimeoutException("Operation timed out after: " + sw.Elapsed, ex);
ExceptionDispatchInfo.Capture(exception).Throw();
HandleException(ex, sw);
}
finally
{
Expand All @@ -146,19 +146,25 @@ public static T RunSync<T>(Func<Task<T>> task)
internal static T RunSync<T>(Func<ValueTask<T>> taskFactory)
{
var oldContext = SynchronizationContext.Current;
var sw = Stopwatch.StartNew();

var task = taskFactory();

// Do we have an active synchronization context?
if (UseTaskAwaiterWhenNoSynchronizationContextIsAvailable && oldContext == null && task.IsCompleted)
{
// We can run synchronously without any issue.
return task.GetAwaiter().GetResult();
try
{
return task.GetAwaiter().GetResult();
}
catch (AggregateException ex)
{
HandleException(ex, sw);
}
}

var result = default(T);

var sw = Stopwatch.StartNew();
var synch = _pool.Allocate();

SynchronizationContext.SetSynchronizationContext(synch);
Expand All @@ -185,10 +191,7 @@ internal static T RunSync<T>(Func<ValueTask<T>> taskFactory)
}
catch (AggregateException ex)
{
var exception = ex.ExtractSingleInnerException();
if (exception is OperationCanceledException)
throw new TimeoutException("Operation timed out after: " + sw.Elapsed, ex);
ExceptionDispatchInfo.Capture(exception).Throw();
HandleException(ex, sw);
}
finally
{
Expand All @@ -200,6 +203,18 @@ internal static T RunSync<T>(Func<ValueTask<T>> taskFactory)
return result;
}

#if !NETSTANDARD2_0
[DoesNotReturn]
#endif
private static void HandleException(AggregateException ex, Stopwatch sw)
{
var exception = ex.ExtractSingleInnerException();
if (exception is OperationCanceledException)
throw new TimeoutException("Operation timed out after: " + sw.Elapsed, ex);

ExceptionDispatchInfo.Capture(exception).Throw();
}

private sealed class ExclusiveSynchronizationContext : SynchronizationContext
{
private readonly AutoResetEvent _workItemsWaiting = new AutoResetEvent(false);
Expand Down

0 comments on commit 57f83d1

Please sign in to comment.