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

Fix stalls when class activation fails. #126

Merged
merged 5 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
{ }
}
}
Loading