Skip to content

Can't WaitForResourceAsync KnownResourceStates.Finished #4878

Closed

Description

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 Damian Edwards (@DamianEdwards) Karol Zadora-Przylecki (@karolz-ms) Mitch Denny (@mitchdenny)

Activity

  1. ghost added
    area-app-model
    on Jul 12, 2024
  2. karolz-ms commented on Jul 15, 2024

    @karolz-ms
    Contributor

    Eric Erhardt (@eerhardt) and I chatted. We think the main issue is that as soon as app.StopAsync() is called, the resource service and ApplicationExecutor stops listening to resource changes. So even if DcpOptions.DeleteResourcesOnShutdown is set, there is no way for the Aspire model to learn that the container (or any resource for that matter) has changed state, or was deleted.

    It would probably make sense to change this behavior and differentiate between stopping the application (which would stop application executables and containers, but preserve the Aspire model and its ability to get state updates) vs disposing of the application (which would dispose of the model, stop DCP orchestrator etc.)

  3. ealeykin commented on Nov 16, 2024

    @ealeykin

    Karol Zadora-Przylecki (@karolz-ms) I'm observing the same behavior even before StopAsync - not getting a Finished notification when the target host exits. Well, from time to time I get it, but most of the time it's stuck on waiting till the timeout.

  4. davidfowl commented on Jan 14, 2025

    @davidfowl
    Collaborator

    Damian Edwards (@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)

  5. DamianEdwards commented on Jan 14, 2025

    @DamianEdwards
    Member

    We'd need to allow listening to changes past the stopping of the application (but before disposal)

    Is there an existing signal for that or would we need to do something custom?

  6. davidfowl commented on Jan 14, 2025

    @davidfowl
    Collaborator

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

  7. karolz-ms commented on Jan 14, 2025

    @karolz-ms
    Contributor

    This issue covers 3 different use cases: stopping a resource vs deleting a resource vs disposing of the application. We should recognize that they are different and are useful for different use cases. We should be clear about what use case we are talking about, what mechanism(s) we want to use for that use case(s), and what works as expected vs needs improvement. Some thoughts on the original use case that prompted Eric Erhardt (@eerhardt) to open this issue follow.

    To me the right mechanism seems deleting the container that is using a volume. When the container is deleted, the app host should receive a notification and can proceed to delete the container volume that the container was using. This is all supported by the app orchestrator today and should work, provided the app model allows the app host code to do this (not 100% sure, some work may be necessary).

    Alternatively the orchestrator could support marking the volume as "should be deleted when the app shuts down". That would be a new feature for the orchestrator. The test could then dispose the whole app and rely on automatic cleanup by app orchestrator.

    Stopping a container in this scenario is not sufficient, because a stopped container is still holding a reference to the volume. That is just how Docker/Podman work.

    Disposing the app host while listening to notifications is not going to work because (once #7098 is merged) disposing the app triggers wholesale resource cleanup and orchestrator shutdown. The app orchestrator does not make any guarantees about the order in which resources are cleaned up, and it does not guarantee sending notifications about resources being cleaned up either. It just tries to get the machine to clean state as quickly as possible.

  8. DamianEdwards commented on Jan 14, 2025

    @DamianEdwards
    Member

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

    Disposing the app host while listening to notifications is not going to work because (once #7098 is merged) disposing the app triggers wholesale resource cleanup and orchestrator shutdown. The app orchestrator does not make any guarantees about the order in which resources are cleaned up, and it does not guarantee sending notifications about resources being cleaned up either. It just tries to get the machine to clean state as quickly as possible.

    Regardless, it seems it would still make sense to change ResourceNotificationService to not kill the loop until it's disposed so that notifications sent after app host stopping but before disposal can be seen.

  9. 33 remaining items

  10. DamianEdwards commented on Feb 5, 2025

    @DamianEdwards
    Member

    Is the work left here to update Aspire.Hosting to issue the relevant two-phase commands to DCP appropriately, i.e. Stop() -> stop, Dispose() -> shutdown?

  11. davidfowl commented on Feb 6, 2025

    @davidfowl
    Collaborator

    Yes we never merged #4878 (comment)

  12. DamianEdwards commented on Feb 6, 2025

    @DamianEdwards
    Member

    David Fowler (@davidfowl) OK so DCP got updates to ensure it can send final states now and we just need to merge your changes?

  13. davidfowl commented on Feb 6, 2025

    @davidfowl
    Collaborator

    I assume we also need to call the new APIs in DCP yep.

  14. DamianEdwards commented on Feb 6, 2025

    @DamianEdwards
    Member

    OK, is this correctly assigned? Karol Zadora-Przylecki (@karolz-ms) are you planning to do the work on the Aspire side for this?

  15. karolz-ms commented on Feb 6, 2025

    @karolz-ms
    Contributor

    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.

  16. karolz-ms commented on Feb 7, 2025

    @karolz-ms
    Contributor

    Should be resolved with #7446

  17. davidfowl commented on Feb 10, 2025

    @davidfowl
    Collaborator

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

  18. DamianEdwards commented on Feb 10, 2025

    @DamianEdwards
    Member

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

    I can update the samples tests to do it. Is there are code snippet someone can point me at?

  19. eerhardt commented on Feb 10, 2025

    @eerhardt
    MemberAuthor

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

    Also #7477 could use this as well.

    cc Sébastien Ros (@sebastienros)

  20. locked and limited conversation to collaborators on Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions