Skip to content

Commit 401e020

Browse files
Make sure AkkaHostedService failures are always visible (#494)
* Make sure `AkkaHostedService` failures are always visible close #470 - application crashes and exits should always be explicitly logged someplace where they can be seen regardless of logging configuration. * Re-throw exception on application startup --------- Co-authored-by: Gregorius Soedharmo <[email protected]>
1 parent fb80b87 commit 401e020

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Akka.Actor;
4+
using FluentAssertions;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Hosting;
7+
using Microsoft.Extensions.Logging;
8+
using Xunit;
9+
using Xunit.Abstractions;
10+
using static FluentAssertions.FluentActions;
11+
12+
namespace Akka.Hosting.Tests;
13+
14+
public class StartFailureSpec
15+
{
16+
private readonly ITestOutputHelper _output;
17+
18+
public StartFailureSpec(ITestOutputHelper output)
19+
{
20+
_output = output;
21+
}
22+
23+
[Fact]
24+
public async Task ShouldThrowWhenActorSystemFailedToStart()
25+
{
26+
// arrange
27+
var host = new HostBuilder()
28+
.ConfigureLogging(builder =>
29+
{
30+
builder.ClearProviders();
31+
builder.AddProvider(new XUnitLoggerProvider(_output, LogLevel.Debug));
32+
})
33+
.ConfigureServices(services =>
34+
{
35+
services.AddAkka("MySys", (builder, provider) =>
36+
{
37+
builder.AddStartup((_, _) => throw new TestException("BOOM"));
38+
});
39+
})
40+
.Build();
41+
42+
await Awaiting(async () => await host.StartAsync()).Should()
43+
.ThrowExactlyAsync<TestException>().WithMessage("BOOM");
44+
}
45+
46+
private class TestException: Exception
47+
{
48+
public TestException(string? message) : base(message)
49+
{
50+
}
51+
}
52+
}

src/Akka.Hosting/AkkaHostedService.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.ExceptionServices;
23
using System.Threading;
34
using System.Threading.Tasks;
45
using Akka.Actor;
@@ -72,7 +73,15 @@ async Task TerminationHook()
7273
catch (Exception ex)
7374
{
7475
Logger.Log(LogLevel.Critical, ex, "Unable to start AkkaHostedService - shutting down application");
75-
HostApplicationLifetime?.StopApplication();
76+
77+
// resolve https://github.com/akkadotnet/Akka.Hosting/issues/470 - never allow failures to be silent
78+
Console.WriteLine($"Unable to start AkkaHostedService - shutting down application.\nCause: {ex}");
79+
80+
// Best effort to perform a clean stop
81+
var capturedException = ExceptionDispatchInfo.Capture(ex);
82+
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3));
83+
await StopAsync(cts.Token);
84+
capturedException.Throw();
7685
}
7786
}
7887

0 commit comments

Comments
 (0)