Skip to content

Commit

Permalink
Merge pull request #905 from kekekeks/proposed_fix_for_886
Browse files Browse the repository at this point in the history
Proposed fix for #886 asp.net deadlock
  • Loading branch information
rogeralsing committed Apr 25, 2015
2 parents 45b6346 + 2ebc75d commit 9210ebb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/core/Akka/Actor/Futures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ public static Task<object> Ask(this ICanTell self, object message, TimeSpan? tim
return self.Ask<object>(message, timeout);
}

public static async Task<T> Ask<T>(this ICanTell self, object message, TimeSpan? timeout = null)
public static Task<T> Ask<T>(this ICanTell self, object message, TimeSpan? timeout = null)
{
IActorRefProvider provider = ResolveProvider(self);
if (provider == null)
throw new NotSupportedException("Unable to resolve the target Provider");

ResolveReplyTo();
var result = (T)await Ask(self, message, provider, timeout);
return result;
return Ask(self, message, provider, timeout).CastTask<object, T>();
}

internal static IActorRef ResolveReplyTo()
Expand Down
1 change: 1 addition & 0 deletions src/core/Akka/Akka.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
<Compile Include="Util\Internal\Collections\ImmutableAvlTreeMap.cs" />
<Compile Include="Util\Internal\Collections\ImmutableTreeSet.cs" />
<Compile Include="Util\Internal\StringBuilderExtensions.cs" />
<Compile Include="Util\Internal\TaskExtensions.cs" />
<Compile Include="Util\ISurrogate.cs" />
<Compile Include="Util\StandardOutWriter.cs" />
<Compile Include="Event\Subscription.cs" />
Expand Down
38 changes: 38 additions & 0 deletions src/core/Akka/Util/Internal/TaskExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Akka.Util.Internal
{
public static class TaskExtensions
{
public static Task<TResult> CastTask<TTask, TResult>(this Task<TTask> task)
{
if (task.IsCompleted)
return Task.FromResult((TResult) (object)task.Result);
var tcs = new TaskCompletionSource<TResult>();
if (task.IsFaulted)
tcs.SetException(task.Exception);
else
task.ContinueWith(_ =>
{
if (task.IsFaulted || task.Exception != null)
tcs.SetException(task.Exception);
else if (task.IsCanceled)
tcs.SetCanceled();
else
try
{
tcs.SetResult((TResult) (object) task.Result);
}
catch (Exception e)
{
tcs.SetException(e);
}
}, TaskContinuationOptions.ExecuteSynchronously);
return tcs.Task;
}
}
}

0 comments on commit 9210ebb

Please sign in to comment.