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.
… a bit - and other service can cause stop
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. |
eerhardt
left a comment
There was a problem hiding this comment.
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
eerhardt
left a comment
There was a problem hiding this comment.
LGTM. @davidfowl - any thoughts?
|
Test failures seem unrelated: if this change broke This smells like a coreclr break. |
|
Logged #51346 for the test failures. |
BackgroundServiceExceptionBehaviorenum.HostOptions.BackgroundServiceExceptionBehaviorproperty.StopHost, which was not the original behavior.HostHandleBackgroundExceptiontoTryStartBackgroundServiceAsyncas it is more conditional and appended the "Async" suffix as it isTaskreturningMoved the invocation ofStartAsyncto theTryStartBackgroundServiceAsyncto ensure that situations where it immediately throws are correctly handled.IgnoreexplicitlyStopHostwhich callsIApplicationLifetime.StopApplication(), thus triggeringApplicationStopping.Fixes #43637