You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Task to AsyncResult transition wraps Exception into the AggregateException #154
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:
Call Task returning method via return! in an asyncResult CE
Catch exception
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):
let recunpackException(exn :Exception)=match exn with|:? AggregateException as agg ->match Seq.tryExactlyOne agg.InnerExceptions with| Some x -> unpackException x
| None -> exn
|_-> exn
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).
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)
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.
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
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....)
/// 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
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
@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
Activity
TheAngryByrd commentedon Nov 16, 2021
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
xperiandri commentedon Nov 16, 2021
Is it how
asyncCE works itself?xperiandri commentedon Nov 16, 2021
It does not happen with
awaitin C#xperiandri commentedon Nov 17, 2021
Ah, this is the issue fsharp/fslang-suggestions#840
xperiandri commentedon Nov 17, 2021
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.AwaitTaskTheAngryByrd commentedon Nov 23, 2021
Hey @NinoFloris see any downsides to doing this?
njlr commentedon Mar 29, 2022
I have this helper in various code-bases:
bartelink commentedon May 19, 2022
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).
bartelink commentedon Apr 8, 2024
@TheAngryByrd please excuse the atting....
what would you say about a breaking change regarding this?
I recently flipped some
taskResultcode toasyncResultand fell prey to the silent/implicit change in semantic (i.e.withno longer Doing What I Mean when awaiting something that returnsTask<'T>Possible resolutions are one or more of:
asyncResultnot magically await tasks (force usage ofAsync.AwaitTaskor something likeAwaitTaskCorrectof your choice)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...)
TheAngryByrd commentedon Apr 8, 2024
@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.
bartelink commentedon Apr 8, 2024
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
bartelink commentedon Apr 8, 2024
Added a comment https://github.com/TheAngryByrd/IcedTasks/pull/24/files#r1556182882
TL;DR I don't think any deviations whatsoever from
AwaitTaskCorrectare a good idea.I don't understand enough about what forces
AwaitAwaiteris 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:
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
AwaitTaskCorrectand/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....)
bartelink commentedon Apr 9, 2024
I think my conclusion/proposal is thus that the thing I'd do is remove the pieces that are bracketed with
#if !FABLE_COMPILERfor V5:FsToolkit.ErrorHandling/src/FsToolkit.ErrorHandling/AsyncResultCE.fs
Lines 201 to 217 in dccdbbc
FsToolkit.ErrorHandling/src/FsToolkit.ErrorHandling/AsyncResultCE.fs
Lines 135 to 142 in dccdbbc
Luckily I'm not the maintainer of the library though, as I'd anticipate people immediately asking me: OK, so where it this wonderful
AwaitTaskCorrectthing of which you speak so?There's also the question of what to replace the usage of
Async.AwaitTaskin theTryFinallyAsyncimplFsToolkit.ErrorHandling/src/FsToolkit.ErrorHandling/AsyncResultCE.fs
Lines 57 to 93 in dccdbbc
On the other hand, having a free-standing
net6.0lib with anasyncExincluding neat support for startingcancellableTasks(and awaitingTask/Task<'T>) would be very nice indeed....And then it begins... you add an
asyncExResulteh - this stuff keeps giving eh ;) @TheAngryByrdbartelink commentedon Mar 5, 2025
@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
bartelink commentedon Sep 14, 2026
FYI #373 will put an Async.Await shim that passes all FSharp.Core 11 equivalent tests in and use that