You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
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
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 :)
Activity
glennc commentedon Apr 7, 2019
The StopAsync isn't actually required to reproduce, if you let the process exit the same thing happens.
Tratcher commentedon Apr 7, 2019
It's not actually unblocked until Dispose.
https://github.com/aspnet/Extensions/blob/4b123acda735f7a7e20b62d9cfafabe5707290a0/src/Hosting/Hosting/src/Internal/ConsoleLifetime.cs#L99
davidfowl commentedon Apr 7, 2019
That might be problematic.
Tratcher commentedon Apr 7, 2019
Yeah. We did it that way to try to let app cleanup run on app exit events.
glennc commentedon Apr 8, 2019
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.
xqrzd commentedon Apr 9, 2019
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
DontExitProcessUntilMainMethodHasReturnedor something like that.Tratcher commentedon Jun 4, 2019
Note to self: Could address this with a finalizer?
davidfowl commentedon Jun 4, 2019
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.
tmds commentedon Jun 6, 2019
StopApplicationisn't leading to aDisposewhich causes the block.That's one option.
Another option is to call something more than
StopApplicationwhich causes the host to also beDisposed.davidfowl commentedon Jun 7, 2019
I like @xqrzd 's suggestion
tmds commentedon Jun 7, 2019
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.ProcessExitis 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 causesMainto eventually return.Tratcher commentedon Jun 7, 2019
Yeah, that would align a lot better with how we handle CTL+C.
https://github.com/aspnet/Extensions/blob/0f0782d9bb45b2f86b882530224f792e48d971b7/src/Hosting/Hosting/src/Internal/ConsoleLifetime.cs#L85-L89
Who wants to chase this into corefx?
26 remaining items
Add logging for the hanging case when calling start/stop dotnet/exten…
Add logging for the hanging case when calling start/stop dotnet/exten…
Add logging for the hanging case when calling start/stop dotnet/exten…
Add logging for the hanging case when calling start/stop dotnet/exten…
Daniele122898 commentedon Apr 18, 2020
I have a ASP.NET core 3.1 app that has a BackgroundService. When i call
systemctl stopon the service that hosts the asp.net core app i getWaiting 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
davidfowl commentedon Apr 18, 2020
Yes, it happens when you don't call Dispose explicitly or Run (which calls Dispose).
Daniele122898 commentedon Apr 19, 2020
So if the backgroundservice runs executeasync over DI i have to manually call dispose? Am i understanding this correctly
davidfowl commentedon Apr 19, 2020
It has nothing to do with background services. It’s all about the code in Program.cs that starts the host.
Daniele122898 commentedon Apr 19, 2020
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 :)analogrelay commentedon Apr 24, 2020
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.