-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Process will terminate when the async event throw any exception #76367
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Unhandled exceptions crash the process, by design. It's the same for exceptions going unhandled in queued thread pool work items, for example. With |
@stephentoub Thank you. Can I ask the best practice about handle async event exception? Should try-catch be used for all async events? I'm worried that some unexpected exception occurs in an unimportant module, causing the process to exit. Or some code executed in the plugin also encountered this problem, causing the process to exit. |
The first question to ask yourself is if you really need an async void method at all? If the answer is "yes", then the question to ask is what would you want to have happen if an unexpected exception occurred? What happens in the case of a synchronous event handler for the same event throwing an exception, and how does the resulting behavior in environment in question differ when the exception is thrown asynchronously? If the behavior of allowing the exception to go unhandled is unacceptable, then your recourse is to catch the exception and handle it instead in whatever manner makes the most sense. |
Thank you @stephentoub |
@stephentoub I have a question: If there are some unexpected exceptions in async event, such as OutOfMemoryException, that the process will terminate. It means that the OutOfMemoryException may crash the application when we do not try-catch in async event handler. I'm worried this will make the dotnet application unstable. Is my idea reasonable? Thank you. |
That's no different from an OutOfMemoryException happening in a queued work item (e.g. ThreadPool.QueueUserWorkItem) or a new Thread (e.g. new Thread(...)), etc. Additionally, if you get an unexpected exception in the middle of doing something, that thing will have been interrupted at an unexpected time, so the state of your application is already likely inconsistent making it possibly unstable. |
You're almost never supposed to catch OoM anyways, because your ability to handle and recover from it are pretty low.
Recovery in the face of exceptions - even the "foreseeable" ones you're supposed to handle (mostly IO related) - is a non-trivial task at the best of times. |
Thank you @stephentoub and @Clockwork-Muse . My understanding is that if I can handle exceptions well, such as OutOfMemoryException , then I should catch and handle it. Is my understanding correct? |
Outside of some very specific scenarios - which are unlikely to be relevant here - you shouldn't be catching "unrecoverable" exceptions, which are usually:
In general, the exceptions you're supposed to handle and recover from are "foreseeable". They're most commonly:
Normally any given All this aside, though, we don't have enough information about what you're actually doing to give better advice (and this sort of isn't the place for it, as opposed to something like StackOverflow). |
Description
Some code add the event and work with asynchronous logic. But I find the process will terminate when the async event throw any exception.
I think it's a scary question. This means that the process will crash at any time and we can't handle exceptions in a unified code.
Reproduction Steps
Raise the
FooEvent
event. And you will find the Process be terminated.Expected behavior
Maybe we can ignore the exception
Actual behavior
The exception will terminate.
Do we have to write try-catch in every event to improve stability?
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: