-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Add the ability to stop the Host
when one of its BackgroundService
instances errors
#50569
Add the ability to stop the Host
when one of its BackgroundService
instances errors
#50569
Conversation
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
Tagging subscribers to this area: @eerhardt, @maryamariyan Issue Details
Fixes #43637
|
src/libraries/Microsoft.Extensions.Hosting/src/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/Internal/HostTests.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/Internal/HostTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostBuilderTests.cs
Show resolved
Hide resolved
...s/Microsoft.Extensions.Hosting.Abstractions/ref/Microsoft.Extensions.Hosting.Abstractions.cs
Outdated
Show resolved
Hide resolved
...ibraries/Microsoft.Extensions.Hosting.Abstractions/src/BackgroundServiceExceptionBehavior.cs
Outdated
Show resolved
Hide resolved
...ibraries/Microsoft.Extensions.Hosting.Abstractions/src/BackgroundServiceExceptionBehavior.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs
Outdated
Show resolved
Hide resolved
…ns. Define new EventId. Simplify check, avoid null eval. Update triple dash comments per peer review.
src/libraries/Microsoft.Extensions.Hosting/src/Internal/HostingLoggerExtensions.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostBuilderTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/Internal/HostTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/Internal/HostTests.cs
Outdated
Show resolved
Hide resolved
Looks like the new test is failling. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming CI passes clean, I think this is ready to be merged.
Thanks for the good work here, @IEvangelist.
The one question I have is the behavior around starting. Before if you threw an exception on start it would fail starting the host but it never called StopApplication since the application never started in the first place and the exception bubbled to the the host.StartAsync. What's the behavior here? Is it still the same? |
@davidfowl - This does introduce a change in behavior:
We will need to write up a "breaking change" doc, per @eerhardt. With these changes, by default, we'll stop the host. Before these changes, if the host started a |
I think that behavior is wrong. We should revert it. |
@davidfowl - In looking at this again - an // Fire IHostedService.Start
await hostedService.StartAsync(combinedCancellationToken).ConfigureAwait(false); Otherwise it was just logged in await backgroundService.ExecuteTask.ConfigureAwait(false); Since it is possible for an error to occur on either call, I intentionally grouped these two together to capture both possible background service failure scenarios. Are you suggesting that we I'm still a bit confused on why we wouldn't call Update in chatting this over with @eerhardt I have a better understanding now. Updating this now. |
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/Internal/HostTests.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. @davidfowl - any thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice!
Test failures seem unrelated:
if this change broke This smells like a coreclr break. |
Logged #51346 for the test failures. |
BackgroundServiceExceptionBehavior
enum.HostOptions.BackgroundServiceExceptionBehavior
property.StopHost
, which was not the original behavior.Host
HandleBackgroundException
toTryStartBackgroundServiceAsync
as it is more conditional and appended the "Async" suffix as it isTask
returningMoved the invocation ofStartAsync
to theTryStartBackgroundServiceAsync
to ensure that situations where it immediately throws are correctly handled.Ignore
explicitlyStopHost
which callsIApplicationLifetime.StopApplication()
, thus triggeringApplicationStopping
.Fixes #43637