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

When Resource is Project and is terminated due to an exception the state is Finished with exit code 0 #7373

Open
1 task done
arielmoraes opened this issue Feb 2, 2025 · 5 comments
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication
Milestone

Comments

@arielmoraes
Copy link

arielmoraes commented Feb 2, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When using a Project Resource and for any reason it is terminated because of an exception while in debug, in other words, exit code not 0, the dashboard does not keep track of this and marks it as Finished without errors. A consequence is that if another resource is set to WaitForCompletion it'll start as if the referenced project was gracefully finished.

App1 is finished with an exception:
Image

App2 is started even if the completion exited with error:
Image

The AppHost is very simple:

var app1 = builder.AddProject<Projects.ConsoleApp1>("app1");

builder.AddProject<Projects.ConsoleApp2>("app2")
    .WithExternalHttpEndpoints()
    .WaitForCompletion(app1);

Expected Behavior

The DCP should keep track of the debug exit code instead of marking the resource as Finished without errors.

Steps To Reproduce

Repro project: https://github.com/arielmoraes/AspireFinishedStateError

I've just created the full Aspire template with two simple console app.

Exceptions (if any)

No response

.NET Version info

.NET SDK:
Version: 9.0.102
Commit: cb83cd4923
Workload version: 9.0.100-manifests.4a54b1a6
MSBuild version: 17.12.18+ed8c6aec5

Ambiente de runtime:
OS Name: Windows
OS Version: 10.0.26100
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\9.0.102\

Cargas de trabalho do .NET instaladas:
[aspire]
Origem da Instalação: VS 17.12.35707.178
Versão do Manifesto: 8.2.2/8.0.100
Caminho do Manifesto: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.2.2\WorkloadManifest.json
Tipo de Instalação: Msi

Anything else?

Using Visual Studio with Aspire 9.0.0 and ASP.NET Core 9

@davidfowl
Copy link
Member

I'm not sure we are consistent WRT how run and exit works. We have both Exited and Finished states depending on what is running the application (IDE vs dotnet run).

We should aim to clean this up and be consistent.

  1. Finished vs Exited, do we need both?
  2. ExitCode should be tracked in all cases

@karolz-ms @JakeRadMSFT @tmat

@arielmoraes
Copy link
Author

I was reading a bit further and I found out that VS reports an unhandled exception as exit code 0 when debugging. The source is in this comment from 5 years ago dotnet/runtime#35599 (comment).

I have no clue what the quirk they refer to is, but returning an exit code different than 0 from main makes Aspire work as intended.

Don't know if there is something Aspire can do about that, as it's up to the application to choose which exit code to return. If the application handles using the unhandled exception event it has a chance to also set the exit code.

So, the only problem is not handling an exception while debugging in VS.

@karolz-ms
Copy link
Member

karolz-ms commented Feb 3, 2025

Agree with @davidfowl we should report the state of an Executable as Exited when the program ends, regardless whether it is run from IDE or command line.

Edit: opened https://github.com/microsoft/usvc/issues/202 to fix

@davidfowl davidfowl added area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication and removed area-orchestrator labels Feb 3, 2025
@davidfowl davidfowl added this to the Backlog milestone Feb 3, 2025
@arielmoraes
Copy link
Author

arielmoraes commented Feb 3, 2025

By handling the exception as stated in my previous comment Aspire works as expected:

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    Environment.Exit(-1);
}

throw new Exception();

Image

@davidfowl
Copy link
Member

@arielmoraes we're not going to fix that, but, we'll unify the state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication
Projects
None yet
Development

No branches or pull requests

4 participants