Skip to content

Task to AsyncResult transition wraps Exception into the AggregateException #154

Closed

Description

@xperiandri

Describe the bug

    member this.AsyncAcquireTokenInteractively (uiThread, correlationId) = asyncResult {
        do! Async.SwitchToContext uiThread
        let builder =
            pca.AcquireTokenInteractive(options.Scopes)
                .WithCorrelationId(correlationId)
        if not (isNull configureInteractive) then configureInteractive.Invoke(builder)
        try return! builder.ExecuteAsync() // FSharp.Control.FusionTasks is used not to write |> Async.AwaitTask
        with :? MsalClientException as ex -> return! Error ex.ErrorCode
    }

Matching by the MsalClientException does not happen as it is wrapped by the AggregateException.

To Reproduce
Steps to reproduce the behavior:

  1. Call Task returning method via return! in an asyncResult CE
  2. Catch exception
  3. You will see that it is always the AggregateException.

Expected behavior
A clear and concise description of what you expected to happen.

Desktop (please complete the following information):

  • Version 2.10.0

Activity

  1. TheAngryByrd commented on Nov 16, 2021

    @TheAngryByrd
    Collaborator

    I don't think this is a problem with FsToolkit as this is how exceptions are thrown with Tasks via .NET https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/exception-handling-task-parallel-library

  2. xperiandri commented on Nov 16, 2021

    @xperiandri
    Author

    Is it how async CE works itself?

  3. xperiandri commented on Nov 16, 2021

    @xperiandri
    Author

    It does not happen with await in C#

  4. xperiandri commented on Nov 17, 2021

    @xperiandri
    Author

    Ah, this is the issue fsharp/fslang-suggestions#840

  5. xperiandri commented on Nov 17, 2021

    @xperiandri
    Author

    As long as it is a FSharp.Core issue I propose to use a corrected way of awaiting tasks
    https://github.com/jet/FsKafka/blob/6c6186f822a221adf0ad06e8a44a392aa55aa8ec/src/FsKafka/Infrastructure.fs#L14 instead of Async.AwaitTask

  6. TheAngryByrd commented on Nov 23, 2021

    @TheAngryByrd
    Collaborator

    Hey @NinoFloris see any downsides to doing this?

  7. njlr commented on Mar 29, 2022

    @njlr
    Contributor

    I have this helper in various code-bases:

    let rec unpackException (exn : Exception) = 
      match exn with
      | :? AggregateException as agg -> 
        match Seq.tryExactlyOne agg.InnerExceptions with
        | Some x -> unpackException x
        | None -> exn
      | _ -> exn
  8. bartelink commented on May 19, 2022

    @bartelink
    Contributor

    As long as it is a FSharp.Core issue I propose to use a corrected way of awaiting tasks https://github.com/jet/FsKafka/blob/6c6186f822a221adf0ad06e8a44a392aa55aa8ec/src/FsKafka/Infrastructure.fs#L14 instead of Async.AwaitTask

    Note the edition therein in being synced with the canonical one in jet/FsKafka#92 (similarly fsprojects/FSharp.AWS.DynamoDB#49)

    The key specific diff in there is that cancellation will be guaranteed yield an exception, but the more important effect is that people debugging systems can do that based on being able to assume-but-verify identical behavior vs the needless variation having slight variations can cause (especially if we do manage to converge in the end).

  9. bartelink commented on Apr 8, 2024

    @bartelink
    Contributor

    @TheAngryByrd please excuse the atting....

    what would you say about a breaking change regarding this?
    I recently flipped some taskResult code to asyncResult and fell prey to the silent/implicit change in semantic (i.e. with no longer Doing What I Mean when awaiting something that returns Task<'T>
    Possible resolutions are one or more of:

    • make asyncResult not magically await tasks (force usage of Async.AwaitTask or something like AwaitTaskCorrect of your choice)
    • make the awaits of tasks in this lib do the right thing (inhibit the unhelpful AggregateException wrapping as ATC does)
    • put AwaitTaskCorrect into this library and use it internally (though the real answer is to solve it upstream - there's a placeholder issue here: TaskEx: AwaitTaskCorrect / Task.toAsync / Async.ofTask fsprojects/FSharp.Control.TaskSeq#141)

    I guess the simplest thing is to remove implicit awaiting? (Checking if there's any way I can inhibit this as a consumer without changing the lib...)

  10. TheAngryByrd commented on Apr 8, 2024

    @TheAngryByrd
    Collaborator

    @bartelink

    I did create an “asyncEx” in IcedTasks with the unwrapping aggregate semantics. Could you give that a once over and make sure it’s working the way you think it should be?

    If it does then I think it might be worth making another CE that can polyfill the current one.

    Breaking API changes are less scary usually because there’s obvious compile errors but runtime changes does kind of scare me.

  11. bartelink commented on Apr 8, 2024

    @bartelink
    Contributor

    Oh yeah, I remember that now 🤦
    Will have a peep (there's another semantic in ATC about honoring cancellation too)
    You're not wrong about compile vs runtime semantic changes being a very different level of concern

  12. bartelink commented on Apr 8, 2024

    @bartelink
    Contributor

    Added a comment https://github.com/TheAngryByrd/IcedTasks/pull/24/files#r1556182882

    TL;DR I don't think any deviations whatsoever from AwaitTaskCorrect are a good idea.

    I don't understand enough about what forces AwaitAwaiter is balancing to be able to comment on the specifics of its implementation - e.g. I don't understand the roles of the various exception mappings etc.

    Overall it boils down to two concerns:

    • having an implicit await behavior at all
    • how many flavours there are in the world of that

    For me, there can only be implicit behavior if there's a canonical expectation and/or spec for it. The canonical impl and the associated test snippets on fsssnip provide specific behavior under cancellation too, and that's pretty critical for me


    I guess this tells us we need to get this resolved in core because having TaskSeq, IcedTasks and/or here all have stuff that's not AwaitTaskCorrect and/or some equivalent with targeted improvements is bad news. I'd so love to stop having to cart it around github.com/jet and FsAwsDdb and/or ever have to compare implementations ever again!

    (Ironically, for my work project, having FsToolkit host AwaitTaskCorrect would be the perfect solution, but obviously having the world take a dependency on this entire lib for a correct awaittask impl is a step too far....)

  13. bartelink commented on Apr 9, 2024

    @bartelink
    Contributor

    I think my conclusion/proposal is thus that the thing I'd do is remove the pieces that are bracketed with #if !FABLE_COMPILER for V5:

    #if !FABLE_COMPILER
    /// <summary>
    /// Method lets us transform data types into our internal representation.
    /// </summary>
    member inline _.Source(task: Task<'ok>) : Async<Result<'ok, 'error>> =
    task
    |> Async.AwaitTask
    |> Async.map Ok
    /// <summary>
    /// Method lets us transform data types into our internal representation.
    /// </summary>
    member inline _.Source(task: Task) : Async<Result<unit, 'error>> =
    task
    |> Async.AwaitTask
    |> Async.map Ok
    #endif

    #if !FABLE_COMPILER
    /// <summary>
    /// Method lets us transform data types into our internal representation.
    /// </summary>
    member inline _.Source(task: Task<Result<'ok, 'error>>) : Async<Result<'ok, 'error>> =
    task
    |> Async.AwaitTask
    #endif

    Luckily I'm not the maintainer of the library though, as I'd anticipate people immediately asking me: OK, so where it this wonderful AwaitTaskCorrect thing of which you speak so?

    There's also the question of what to replace the usage of Async.AwaitTask in the TryFinallyAsync impl

    #if NETSTANDARD2_1
    member inline _.TryFinallyAsync
    (
    computation: Async<Result<'ok, 'error>>,
    [<InlineIfLambda>] compensation: unit -> ValueTask
    ) : Async<Result<'ok, 'error>> =
    let compensation =
    async {
    let vTask = compensation ()
    if vTask.IsCompletedSuccessfully then
    return ()
    else
    return!
    vTask.AsTask()
    |> Async.AwaitTask
    }
    Async.TryFinallyAsync(computation, compensation)
    member inline this.Using
    (
    resource: 'ok :> IAsyncDisposable,
    [<InlineIfLambda>] binder: 'ok -> Async<Result<'U, 'error>>
    ) : Async<Result<'U, 'error>> =
    this.TryFinallyAsync(
    binder resource,
    (fun () ->
    if not (isNull (box resource)) then
    resource.DisposeAsync()
    else
    ValueTask()
    )
    )
    #endif

    On the other hand, having a free-standing net6.0 lib with an asyncEx including neat support for starting cancellableTasks (and awaiting Task/Task<'T>) would be very nice indeed....

    And then it begins... you add an asyncExResult eh - this stuff keeps giving eh ;) @TheAngryByrd

  14. bartelink commented on Mar 5, 2025

    @bartelink
    Contributor

    @TheAngryByrd It would be a substantial sized PR, but I could stick AwaitTaskCorrect into the lib, and replace usage of AwaitTask with same for the v5 release line if you have the appetite

    At the present time I dont see anyone out there with bandwidth for an AsyncEx lib (and my desire for this to be solved in a much smaller library to own this as any library cannot realistically pull in a library the size of this one as a dependency remains)

    As hinted at above, the real answer is for the upstream support to get into FSharp.Core, but I guess ultimately there's going to be a need for two flavors in there, i.e. original AwaitTask with usage of AwaitTaskCorrect replacing that over time.

    IMO at least having a common await function used internally would allow better solutions for async stacktraces etc to be experimented on over time.

    Potentially a global lazy could allow one to opt out of AwaitTaskCorrect and have all awaits instead be plain AwaitTask

    No pressure, but let me know if you have a view as to what's appropriate for v5

  15. bartelink commented on Sep 14, 2026

    @bartelink
    Contributor

    FYI #373 will put an Async.Await shim that passes all FSharp.Core 11 equivalent tests in and use that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions