aspire: Can't WaitForResourceAsync KnownResourceStates.Finished

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

It’s not possible to get notification when a resource has been stopped/finished when after await app.StopAsync();.

I’m trying to write a test that uses a Docker Volume, and when the test is done, it deletes the volume. However, the volume is still in use even after await app.StopAsync(); returns, which means the container using the volume is still running.

I’d like a way to get notified when the resource/container is finished so I can delete the volume.

One reason this isn’t possible today is that WaitForResourceAsync cancels once the app is stopping here: https://github.com/dotnet/aspire/blob/681f2e754dc849f96abafcd2829783e69155abf1/src/Aspire.Hosting/ApplicationModel/ResourceNotificationService.cs#L89-L93

Once the app is stopping, it cancels this loop, which means we won’t get more notifications about the resource.

Expected Behavior

I’d like to get a notification when the container is finished.

Steps To Reproduce

await using var app = await appHost.BuildAsync();
var resourceNotificationService = app.Services.GetRequiredService<ResourceNotificationService>();
await app.StartAsync();

await app.StopAsync();

await resourceNotificationService.WaitForResourceAsync("webfrontend", KnownResourceStates.Finished).WaitAsync(TimeSpan.FromSeconds(30));

Exceptions (if any)

No response

.NET Version info

No response

Anything else?

cc @DamianEdwards @karolz-ms @mitchdenny

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 34 (33 by maintainers)

Commits related to this issue

Most upvoted comments

I think we need a test to actually use this pattern to see if we need more changes to the app model codez.

Yes, I am working on this as we speak. Specifically, on adding a new property to DcpExecutor that determines whether StopAsync() waits for resource cleanup to be complete or not. That will then be leveraged by test application builder. Should have a PR out for review sometime later today.

@karolz-ms It seems like we need 2 phase shutdown:

  1. Stop resources
  2. Stop dcp.

As part of 1, you would run all resources through the terminal states, 2 shuts down dcp. The app host would call 1 in StopAsync and 2 in DisposeAsync

We can definitively do this in DCP. To a large degree it is possible already, just not through a single call. Namely, both Containers and Executables have a spec property named Stop; if you set that to true, the object will transition to final state.

@ReubenBond it works because the application will be changed to stop pumping updates in dispose, not stop.

@karolz-ms It seems like we need 2 phase shutdown:

  1. Stop resources
  2. Stop dcp.

As part of 1, you would run all resources through the terminal states, 2 shuts down dcp. The app host would call 1 in StopAsync and 2 in DisposeAsync

The latter. It seems like it reports stopping then dies, so I never see finished or exited.

It turns out when you let DCP shutdown it may not put resources in the Finished state. I observed Stopping and then nothing. This doesn’t feel like a bug but we need to decide if this is the desired behavior.

https://github.com/dotnet/aspire/compare/davidfowl/stop-listeing-ondispose?expand=1

I think shutting down in dispose would be better (implement IDisposable or IAsyncDisposable)

@DamianEdwards this is the code you added to unhook the notification service on shutdown automatically. We’d need to allow listening to changes past the stopping of the application (but before disposal)