Skip to content

Instantly share code, notes, and snippets.

@jtmueller
Created December 15, 2015 00:51
Show Gist options
  • Save jtmueller/7502efa7e4c0c08dcaac to your computer and use it in GitHub Desktop.
Save jtmueller/7502efa7e4c0c08dcaac to your computer and use it in GitHub Desktop.
// Akka F# API
type ICanTell with
member this.Ask(msg: obj, timeout: TimeSpan): Async<'Response> =
this.Ask<'Response>(msg, Nullable(timeout)) |> Async.AwaitTask
// If we do this.Ask().ContinueWith(..) after shutting down the host for a remote actor,
// the task passed into ContinueWith has IsFaulted = false and Exception = null, but throws
// an AggregateException when the Result property is accessed.
// Knowing this, I wrote the following replacement to the Akka F# API extension method above.
// This version simply accesses the Result property in a ContinueWith call, ensuring that if
// an exception is going to be thrown, the task passed to Async.AwaitTask is in an already-faulted state.
// With this version, F# async code gets an exception at the expected place instead of hanging.
// This is a work-around; it would be better if the Task were in a valid state to begin with.
// replacement
type ICanTell with
member x.Ask<'TResult>(msg:obj, ?timeout:TimeSpan) =
let timeout = match timeout with Some t -> Nullable(t) | None -> Nullable()
x.Ask<'TResult>(msg, timeout).ContinueWith(fun (task:Task<_>) -> task.Result) |> Async.AwaitTask
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment