-
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
When awaiting an async method from ExecuteAsync which throws an unhandled exception, the service hangs #36017
Comments
A background service throwing an error during ExecuteAsync doesn't cause the app to shut down. That's intentional behavior. If you want that behavior, you'll have to
We could consider logging the exception as it occurs (today it should be logged or manifested somehow when the host stops). |
Which caller? Program.Main? |
@Tratcher Good question, for which I don't have the answer. This screen recording might help clarify the issue at hand: https://drive.google.com/file/d/1gdMzXwQi2VzWP1OUivYgiY-yyDELpDGB/view As you can see, any exception thrown synchronously will crash the service (which is what I'd expect). However, when thrown in combination with some asynchronous operation it will just silently hang. I feel the behaviour should have been consistent in all of these cases. |
This is because
This is behavior I'd expect. In general, BackgroundServices do not crash the process when they fail by design. |
We should really make sure that these exceptions are at least logged. I've been helping @stevejgordon with putting together some worker samples, and it's a painful lesson that everyone using worker seems to have to learn once. In general if the framework is calling user-code we should make sure the exception is at least logged - I think there's a bigger discussion to be had about whether an unhandled exception from an |
This is a reasonable candidate for 5.0, but it'll be up to the runtime team to prioritize for sure :). The idea here would be to have |
I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label. |
I investigated this issue and here is what I found: There is a problem that there is no good location to do the logging. When the runtime/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/BackgroundService.cs Lines 13 to 15 in a52f237
And in runtime/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/BackgroundService.cs Lines 30 to 46 in a52f237
We could add a The more correct place for the logging to occur would be in the Host that starts the task. The Host already contains a logger, so it can simply log any exception to it:
However, the Host doesn't have access to the We could do some hack to make this work in |
The |
Expose the task that executes the background service, so consumers can check if it is running and/or has ran to competition or faulted. Use the new ExecuteTask to log exceptions when a BackgroundService fails after await, instead of appearing to hang. Fix dotnet#35991 Fix dotnet#36017
* Expose BackgroundService.ExecuteTask Expose the task that executes the background service, so consumers can check if it is running and/or has ran to competition or faulted. Use the new ExecuteTask to log exceptions when a BackgroundService fails after await, instead of appearing to hang. Fix #35991 Fix #36017
Describe the bug
In a .Net Core Worker Service, whenever an exception is thrown inside an async method, and you happened to not surround the method invocation in a try/catch, the application will simply hang when it hits the exception -- giving no sign that an exception has occured at all.
The same code applied within a simple Console Application does indeed work as expected, and you get the normal "unhandled exception" behaviour.
To Reproduce
Run sample worker service available at https://github.com/hognevevle/WorkerServicePlayground
This project was created using the Worker Service project template, and my only changes are done inside
Worker.cs
.Expected behavior
The exception should be propagated up to the caller, and not simply disappear.
Screenshots
https://drive.google.com/file/d/1VCfrJY_a45gJ17uQXwGAq_krc8n-iteB/view?usp=drivesdk
Additional context
The text was updated successfully, but these errors were encountered: