Skip to content

Hang when calling start/stop on IHost #1363

Closed

Description

@glennc

The program below appears to hang after logging the application is shutting down message and I don't understand why. The OnProcessExit event appears to be waiting here https://github.com/aspnet/Extensions/blob/master/src/Hosting/Hosting/src/Internal/ConsoleLifetime.cs#L79 but I don't know why that would be or if that is a problem or just a red herring.

    public class Program
    {
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();
            await host.StartAsync();
            await host.StopAsync();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureServices(services => services.AddHostedService<Worker>());
    }

@Tratcher @davidfowl

Activity

  1. glennc commented on Apr 7, 2019

    @glennc
    Author

    The StopAsync isn't actually required to reproduce, if you let the process exit the same thing happens.

  2. davidfowl commented on Apr 7, 2019

    @davidfowl
    Member

    That might be problematic.

  3. Tratcher commented on Apr 7, 2019

    @Tratcher
    Member

    Yeah. We did it that way to try to let app cleanup run on app exit events.

  4. glennc commented on Apr 8, 2019

    @glennc
    Author

    OK, that's surprising. Can it not be tied to something like applicationLifetime.Stopped that gets raised after the host has finished stopping all the hosted services? I want to make sure I understand the tradeoff.

    This means if you're not calling Run then you want to put the host in a using, which I don't think we've ever told anyone they'd have to do.

  5. xqrzd commented on Apr 9, 2019

    @xqrzd

    It was done there to allow any hosted services / dependencies to be disposed before the process exits. Prior to that change on Linux sigterm would effectively kill the process. If the event was tied to applicationLifetime.Stopped it would allow any hosted services to stop, but not be disposed. I think a proper fix requires a .net runtime change, something like DontExitProcessUntilMainMethodHasReturned or something like that.

  6. Tratcher commented on Jun 4, 2019

    @Tratcher
    Member

    Note to self: Could address this with a finalizer?

  7. davidfowl commented on Jun 4, 2019

    @davidfowl
    Member

    I'm not sure that would work. The host is rooted by the DI container which is rooted by the callback, which is stored in a static.

  8. tmds commented on Jun 6, 2019

    @tmds
    Member
            private void OnProcessExit(object sender, EventArgs e)
            {
                ApplicationLifetime.StopApplication();
                _shutdownBlock.WaitOne();
    
            public void Dispose()
            {
                _shutdownBlock.Set();
    

    StopApplication isn't leading to a Dispose which causes the block.

    Can it not be tied to something like applicationLifetime.Stopped that gets raised after the host has finished stopping all the hosted services?

    That's one option.
    Another option is to call something more than StopApplication which causes the host to also be Disposed.

  9. davidfowl commented on Jun 7, 2019

    @davidfowl
    Member

    I like @xqrzd 's suggestion

  10. tmds commented on Jun 7, 2019

    @tmds
    Member

    I like @xqrzd 's suggestion

    All options are better than hanging.

    To work that suggestion a bit more:
    Now on SIGTERM the runtime will end the process and hence the AppDomain.CurrentDomain.ProcessExit is emitted. By blocking the event, the process exit is delayed.
    Instead, you want to get an event on SIGTERM (other than ProcessExit, e.g. ProcessExitRequest) and the runtime to do nothing. In your event handler, you'll do something that causes Main to eventually return.

  11. 26 remaining items

  12. Daniele122898 commented on Apr 18, 2020

    @Daniele122898

    I have a ASP.NET core 3.1 app that has a BackgroundService. When i call systemctl stop on the service that hosts the asp.net core app i get
    Waiting for the host to be disposed. Ensure all 'IHost' instances are wrapped in 'using' blocks.
    Is this connected to this issue above? Bcs everything that inherits from IHost is managed by the DI.

    Just want to make sure if it's a problem in my code or if its indeed the same issue

  13. davidfowl commented on Apr 18, 2020

    @davidfowl
    Member

    Yes, it happens when you don't call Dispose explicitly or Run (which calls Dispose).

  14. Daniele122898 commented on Apr 19, 2020

    @Daniele122898

    So if the backgroundservice runs executeasync over DI i have to manually call dispose? Am i understanding this correctly

  15. davidfowl commented on Apr 19, 2020

    @davidfowl
    Member

    It has nothing to do with background services. It’s all about the code in Program.cs that starts the host.

  16. Daniele122898 commented on Apr 19, 2020

    @Daniele122898

    Oh i see so the initial Host service that you create in the Program.cs. My bad, then i misunderstood this. So i need to add a using. Will do that and see if that fixes the issue :)

  17. analogrelay commented on Apr 24, 2020

    @analogrelay

    This has the same root cause as https://github.com/dotnet/extensions/issues/2030 . The solution for that would also solve this problem since our SIGTERM handler would stop shutdown and allow Main to return.

    Closing as a duplicate.

  18. ghost locked as resolved and limited conversation to collaborators on May 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions