From f0f86971e2223d9b40a30f8b23ecc453387f2e3f Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Mon, 16 Sep 2024 16:56:40 -0500 Subject: [PATCH 1/2] 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. --- src/Akka.Hosting/AkkaHostedService.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Akka.Hosting/AkkaHostedService.cs b/src/Akka.Hosting/AkkaHostedService.cs index 65f9df7..2ef137d 100644 --- a/src/Akka.Hosting/AkkaHostedService.cs +++ b/src/Akka.Hosting/AkkaHostedService.cs @@ -72,6 +72,9 @@ async Task TerminationHook() catch (Exception ex) { Logger.Log(LogLevel.Critical, ex, "Unable to start AkkaHostedService - shutting down application"); + + // resolve https://github.com/akkadotnet/Akka.Hosting/issues/470 - never allow failures to be silent + Console.WriteLine($"Unable to start AkkaHostedService due to [{ex}] - shutting down application"); HostApplicationLifetime?.StopApplication(); } } From ed0a81c4ffb17549160047772c17492dbec88c55 Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Tue, 1 Oct 2024 23:21:10 +0700 Subject: [PATCH 2/2] Re-throw exception on application startup --- src/Akka.Hosting.Tests/StartFailureSpec.cs | 52 ++++++++++++++++++++++ src/Akka.Hosting/AkkaHostedService.cs | 10 ++++- 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/Akka.Hosting.Tests/StartFailureSpec.cs diff --git a/src/Akka.Hosting.Tests/StartFailureSpec.cs b/src/Akka.Hosting.Tests/StartFailureSpec.cs new file mode 100644 index 0000000..032b27c --- /dev/null +++ b/src/Akka.Hosting.Tests/StartFailureSpec.cs @@ -0,0 +1,52 @@ +using System; +using System.Threading.Tasks; +using Akka.Actor; +using FluentAssertions; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Xunit; +using Xunit.Abstractions; +using static FluentAssertions.FluentActions; + +namespace Akka.Hosting.Tests; + +public class StartFailureSpec +{ + private readonly ITestOutputHelper _output; + + public StartFailureSpec(ITestOutputHelper output) + { + _output = output; + } + + [Fact] + public async Task ShouldThrowWhenActorSystemFailedToStart() + { + // arrange + var host = new HostBuilder() + .ConfigureLogging(builder => + { + builder.ClearProviders(); + builder.AddProvider(new XUnitLoggerProvider(_output, LogLevel.Debug)); + }) + .ConfigureServices(services => + { + services.AddAkka("MySys", (builder, provider) => + { + builder.AddStartup((_, _) => throw new TestException("BOOM")); + }); + }) + .Build(); + + await Awaiting(async () => await host.StartAsync()).Should() + .ThrowExactlyAsync().WithMessage("BOOM"); + } + + private class TestException: Exception + { + public TestException(string? message) : base(message) + { + } + } +} \ No newline at end of file diff --git a/src/Akka.Hosting/AkkaHostedService.cs b/src/Akka.Hosting/AkkaHostedService.cs index 2ef137d..14c5c82 100644 --- a/src/Akka.Hosting/AkkaHostedService.cs +++ b/src/Akka.Hosting/AkkaHostedService.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.ExceptionServices; using System.Threading; using System.Threading.Tasks; using Akka.Actor; @@ -74,8 +75,13 @@ async Task TerminationHook() Logger.Log(LogLevel.Critical, ex, "Unable to start AkkaHostedService - shutting down application"); // resolve https://github.com/akkadotnet/Akka.Hosting/issues/470 - never allow failures to be silent - Console.WriteLine($"Unable to start AkkaHostedService due to [{ex}] - shutting down application"); - HostApplicationLifetime?.StopApplication(); + Console.WriteLine($"Unable to start AkkaHostedService - shutting down application.\nCause: {ex}"); + + // Best effort to perform a clean stop + var capturedException = ExceptionDispatchInfo.Capture(ex); + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3)); + await StopAsync(cts.Token); + capturedException.Throw(); } }