❔ This is not a .NET bug, right? (async void)
Hi! Is this by design?
Neither catch nor finally is getting hit. I know that exceptions which are being raised in async void methods are being swallowed but didn't think that those in a try block would get swallowed aswell.
For reference:
https://stackoverflow.com/a/5383408
https://stackoverflow.com/a/16158374
^ Those answers state that the exceptions are getting catched
50 Replies
you're not awaiting
DoStuff()
Yeah, because you can't
Don't use
async void
That's your fixasync void is "fine" for event handlers
No it's not
Don't use
async void
everi mean it's "fine" in the sense that there is literally no other option
GitHub
AspNetCoreDiagnosticScenarios/AsyncGuidance.md at master · davidfow...
This repository has examples of broken patterns in ASP.NET Core applications - AspNetCoreDiagnosticScenarios/AsyncGuidance.md at master · davidfowl/AspNetCoreDiagnosticScenarios
like for winforms for example
you have to use async void for those events
Weird how there is an exception for a feature that is so error-prone
But this does not look like an event handler
What is the alternative here? I can't change the signature btw
My question is rather if this is a .NET bug or not
it's not
Cause the SO posts are stating that it works for them
the flow leaves
Main
before anything in it is executed
so the program terminatesSo the SO posts are wrong?
I mean, SO posts are wrong all the time
Why can't you change the signature into a Task?
Makes sense, was just wondering! ty!
Ero#1111
REPL Result: Success
Compile: 613.317ms | Execution: 67.114ms | React with ❌ to remove this embed.
Ero#1111
REPL Result: Success
Console Output
Compile: 559.535ms | Execution: 1107.793ms | React with ❌ to remove this embed.
It is not my signature
3rd party code
Clone the repo and fix their mistake 😄
that's a breaking change
Easier said than done ;D (in a real world)
I didn't feel like typing out "Clone their repo and create a second proper asynchronous method and make the current one obsolete"
So tl;dr is: Exceptions in async void methods are getting swallowed no matter what
makes sense to why the code works fine for me on my piece of shit laptop, as I'm getting an output
That's why it's bad
No doubt
When in doubt about asynchronous practices, consult this: https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md
GitHub
AspNetCoreDiagnosticScenarios/AsyncGuidance.md at master · davidfow...
This repository has examples of broken patterns in ASP.NET Core applications - AspNetCoreDiagnosticScenarios/AsyncGuidance.md at master · davidfowl/AspNetCoreDiagnosticScenarios
Very interesting explanation on many things
It does not cover event handlers tho, right?
don't use events
;D
Wait a second. The exception is getting catched if you give it enough time (added a delay to the
Main()
). Which makes sense
The question is why it is not catched in our app then. It is long-running and not immediately exitingI mean, adding a delay to the main is basically the same as awaiting the task if it had one, without actually handling any exception
I would really just update the library you're trying to use
What is it anyway?
Okay, what could be the scenario here then? <:PB_peepo_think:769654147041067028>
App is not exiting immediately, but exception is not getting catched
That's prob why the SO posts seem to be somewhat correct (because it works under circumstances)
I mean, I don't think you will find a fix for this
You can add a boolean that your
async void
method handles and your main method will block until it is changed?
Can I see the library plsAn explanation would be more than enough Current fix idea is to use
ContinueWith(...)
But we are caring about the reason because that has an influence on the fixWhat
ContinueWith()
requires a Task
Which you don't haveI have one in the async void method
Which is why the method is marked with async in the first place
I thought you could not change the method
Or the signature
One can always add
async
in this case
Don't have to touch the 3rd party codeOh, like that
You have an interface to implement
So the whole problem is that you have an interface to abide to, but you have async behaviour
You should just keep the method void, not async void, and then store the Task in memory
Or, you know, block the whole thing
But don't fix it with
async void
lolWdym by store the task in memory?
You store the Task retrieved from
DoAsyncStuff
in memory
Or discard it and use ContinueWith to get the result of the task and catch errors
There is literally no point in using await here if the whole method is async void
Oh yeah, for sure
Got ya
Still, the fact that the library does not allow an asynchronous alternative is just bad
EventHandler's in a nutshell I guess? ;D
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.