Skip to content

Commit

Permalink
Merge pull request #126 from mayuki/hotfix/AbortOnActivationError
Browse files Browse the repository at this point in the history
Fix stalls when class activation fails.
  • Loading branch information
mayuki authored Mar 5, 2024
2 parents 7eb829b + 11b6f5b commit 28257ec
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/Cocona.Lite/Lite/Hosting/CoconaLiteAppHost.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using Cocona.Application;
using Cocona.Command;

namespace Cocona.Lite.Hosting;
Expand All @@ -21,6 +23,7 @@ public async Task RunAsync(CancellationToken cancellationToken)
{
var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _cancellationTokenSource.Token);
var bootstrapper = _serviceProvider.GetRequiredService<ICoconaBootstrapper>();
var console = _serviceProvider.GetRequiredService<ICoconaConsoleProvider>();

#pragma warning disable RS0030 // Do not used banned APIs
Console.CancelKeyPress += OnCancelKeyPress;
Expand Down Expand Up @@ -54,6 +57,11 @@ public async Task RunAsync(CancellationToken cancellationToken)
// NOTE: Ignore OperationCanceledException that was thrown by non-user code.
Environment.ExitCode = 130;
}
catch (Exception ex)
{
console.Error.WriteLine(ex.ToString());
Environment.ExitCode = 1;
}

_waitForShutdown.Set();

Expand Down
10 changes: 6 additions & 4 deletions src/Cocona/Hosting/CoconaHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ private async Task ExecuteCoconaApplicationAsync(Task waitForApplicationStarted)
// NOTE: Ignore OperationCanceledException that was thrown by non-user code.
Environment.ExitCode = 0;
}
catch (Exception)
catch (Exception ex)
{
_console.Error.WriteLine(ex.ToString());
Environment.ExitCode = 1;
throw;
}

_lifetime.StopApplication();
finally
{
_lifetime.StopApplication();
}
}

public async Task StopAsync(CancellationToken cancellationToken)
Expand Down
23 changes: 23 additions & 0 deletions test/Cocona.Test/Integration/CoconaAppRunTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -989,4 +989,27 @@ class TestCommand_StopParsingOption
public void A([Option]int a, [Option(StopParsingOptions = true)]string b, [Argument]string arg0, [Argument]string[] args)
=> Console.WriteLine($"A:{a}:{b}:{arg0}:{string.Join(",", args)}");
}


[Theory]
[InlineData(RunBuilderMode.CreateHostBuilder)]
[InlineData(RunBuilderMode.CreateBuilder)]
public void CoconaApp_Run_AbortOnActivationError(RunBuilderMode mode)
{
var (stdOut, stdErr, exitCode) = Run<TestCommand_AbortOnActivationError>(mode, Array.Empty<string>());
exitCode.Should().Be(1);
stdErr.Should().StartWith("System.InvalidOperationException: Unable to resolve service");
}

class TestCommand_AbortOnActivationError
{
public TestCommand_AbortOnActivationError(Class1 class1)
{ }

public void A()
=> Console.WriteLine($"Hello");

public class Class1
{ }
}
}

0 comments on commit 28257ec

Please sign in to comment.