Skip to content
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

Merged
merged 17 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,25 @@ public void HostBuilderConfigureDefaultsInterleavesMissingConfigValues()
Assert.Equal(expectedContentRootPath, env.ContentRootPath);
}

[Fact]
public void HostBuilderCanConfigureBackgroundServiceExceptionBehavior()
{
using IHost host = new HostBuilder()
.ConfigureServices(
services =>
services.Configure<HostOptions>(
options =>
options.BackgroundServiceExceptionBehavior =
BackgroundServiceExceptionBehavior.StopHost))
.Build();

var options = host.Services.GetRequiredService<IOptions<HostOptions>>();

Assert.Equal(
BackgroundServiceExceptionBehavior.StopHost,
options.Value.BackgroundServiceExceptionBehavior);
}

private class FakeFileProvider : IFileProvider, IDisposable
{
public bool Disposed { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting.Fakes;
Expand Down Expand Up @@ -659,84 +658,34 @@ public async Task WebHostStopAsyncUsesDefaultTimeoutIfNoTokenProvided()
}

[Fact]
public void HostPropagatesExceptionsThrownWithBackgroundServiceExceptionBehaviorOfStopHost()
public async Task HostPropagatesExceptionsThrownWithBackgroundServiceExceptionBehaviorOfStopHost()
{
RemoteExecutor.Invoke(async () =>
{
using IHost host = CreateBuilder()
.ConfigureServices(
services =>
{
services.AddHostedService<AsyncThrowingService>();
services.Configure<HostOptions>(
options =>
options.BackgroundServiceExceptionBehavior =
BackgroundServiceExceptionBehavior.StopHost);
})
.Build();

await host.StartAsync();

var lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
var wasStoppingCalled = false;
lifetime.ApplicationStopping.Register(() => wasStoppingCalled = true);
var wasStoppedCalled = false;
lifetime.ApplicationStopped.Register(() => wasStoppedCalled = true);

Assert.True(
wasStoppingCalled,
$"The {nameof(AsyncThrowingService)} should have thrown, calling Host.StopAsync, gracefully stopping the host.");
Assert.True(
wasStoppedCalled,
$"The {nameof(AsyncThrowingService)} should have thrown, calling Host.StopAsync, gracefully stopping the host.");
}).Dispose();
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void HostPropagatesExceptionsThrownWithBackgroundServiceExceptionBehaviorOfStopHostAfterSometime()
{
RemoteExecutor.Invoke(async () =>
{
var backgroundDelayTaskSource = new TaskCompletionSource<bool>();

using IHost host = CreateBuilder()
.ConfigureServices(
services =>
{
services.AddHostedService(
_ => new AsyncThrowingService(backgroundDelayTaskSource.Task));

services.Configure<HostOptions>(
options =>
options.BackgroundServiceExceptionBehavior =
BackgroundServiceExceptionBehavior.StopHost);
})
.Build();
using IHost host = CreateBuilder()
.ConfigureServices(
services =>
{
services.AddHostedService<AsyncThrowingService>();
services.Configure<HostOptions>(
options =>
options.BackgroundServiceExceptionBehavior =
BackgroundServiceExceptionBehavior.StopHost);
})
.Build();

var lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
var wasStoppingCalled = false;
lifetime.ApplicationStopping.Register(() => wasStoppingCalled = true);
var wasStoppedCalled = false;
lifetime.ApplicationStopped.Register(() => wasStoppedCalled = true);
await host.StartAsync();

await Task.WhenAll(host.StartAsync(), DelayThenSignalContinueAsync());
var lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
var wasStoppingCalled = false;
lifetime.ApplicationStopping.Register(() => wasStoppingCalled = true);
var wasStoppedCalled = false;
lifetime.ApplicationStopped.Register(() => wasStoppedCalled = true);

async Task DelayThenSignalContinueAsync()
{
// Emulate the background service doing some work successfully
// for 5 seconds before throwing exception.
await Task.Delay(5000);

backgroundDelayTaskSource.SetResult(true);
};

Assert.True(
wasStoppingCalled,
$"The {nameof(AsyncThrowingService)} should have thrown, calling Host.StopAsync, gracefully stopping the host.");
Assert.True(
wasStoppedCalled,
$"The {nameof(AsyncThrowingService)} should have thrown, calling Host.StopAsync, gracefully stopping the host.");
}).Dispose();
Assert.True(
wasStoppingCalled,
$"The {nameof(AsyncThrowingService)} should have thrown, calling Host.StopAsync, gracefully stopping the host.");
Assert.True(
wasStoppedCalled,
$"The {nameof(AsyncThrowingService)} should have thrown, calling Host.StopAsync, gracefully stopping the host.");
}

[Fact]
Expand All @@ -761,15 +710,12 @@ public void HostHandlesExceptionsThrownWithBackgroundServiceExceptionBehaviorOfI
var lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
var wasStoppingCalled = false;
lifetime.ApplicationStopping.Register(() => wasStoppingCalled = true);
var wasStoppedCalled = false;
lifetime.ApplicationStopped.Register(() => wasStoppedCalled = true);

host.Start();

backgroundDelayTaskSource.SetResult(true);

Assert.False(wasStoppingCalled);
Assert.False(wasStoppedCalled);
}

[Fact]
Expand Down Expand Up @@ -1490,8 +1436,6 @@ private class AsyncThrowingService : BackgroundService
{
private readonly Task? _executeDelayTask;

internal bool Disposed { get; private set; }

public AsyncThrowingService() { }

public AsyncThrowingService(Task executeDelayTask)
Expand All @@ -1501,17 +1445,13 @@ public AsyncThrowingService(Task executeDelayTask)

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (_executeDelayTask is { })
await _executeDelayTask;

throw new Exception("Background Exception");
}

public override void Dispose()
{
Disposed = true;
while (true)
{
if (_executeDelayTask is { })
await _executeDelayTask;

base.Dispose();
throw new Exception("Background Exception");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);net461</TargetFrameworks>
<EnableDefaultItems>true</EnableDefaultItems>
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
</PropertyGroup>

<ItemGroup>
Expand Down