-
Notifications
You must be signed in to change notification settings - Fork 312
WebHost.Run() completes before ApplicationStopping #812
Comments
Yep I can reproduce the problem. |
Not directly related, but speaking of doing things on "ProcessExit" in case someone is looking around at various options, see: https://github.com/dotnet/corefx/issues/10012#issuecomment-232436326 |
Is there anything we can do about this for now? Can't depend on Thought about doing some manual sync that makes |
It might be worth putting this fix in a patch release. |
@mrahhal what problem is it causing you right now? What does your repro look like? |
@davidfowl that would be super cool. Because with the current state, I have simple tasks that can run for a longish time. And even if an exception happens I have some fault tolerance built in. But in the current version the thread is being shutdown aprubtly that even I have something like the following (assume that public class SomeService
{
private ILogger<SomeService> _logger;
private IServiceProvider _provider;
private Task _task;
public SomeService(
ILogger<SomeService> logger,
IApplicationLifetime appLifetime,
IServiceProvider provider)
{
_logger = logger;
_provider = provider;
appLifetime.ApplicationStopping.Register(() =>
{
try
{
_logger.LogInformation("Waiting for task.");
_task?.Wait();
_logger.LogInformation("Finished waiting for task.");
}
catch (Exception ex)
{
_logger.LogError("Error.", ex);
}
});
}
public void StartTask()
{
_task = StartTaskCoreAsync();
}
public async Task StartTaskCoreAsync()
{
_logger.LogInformation("Starting.");
await Task.Delay(5000);
_logger.LogInformation("Finished.");
}
} When I hit ctrl+c after I issue the request that calls this service, every single time the only logs I'm seeing are: Now, this is a simple example. But this is extremely harmful for things like a background processing jobs library that depends on |
@mrahhal since public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IApplicationLifetime>(new ApplicationLifetime());
} |
@davidfowl so this did not work at all, as I thought. The It's important to note that this issue makes all services/libraries depending on the documented behavior of Is a patch release a lot of work? I'm not sure if it happened before, but this is definitely worth it. Especially if |
@mrahhal we're looking at putting this in a patch release. It's going to be proposed. |
When using
applicationLifetime.ApplicationStopping.Register(HandleStopping);
the program exitshost.Run();
before that handler has completed also the documentation (IntelliSense) says Shutdown will block until this event completes.This also means that ApplicationStopped will be executed before ApplicationStopping has completed.
Also it should be noted that ApplicationStopping is not executed on the main thread (while ApplicationStopped is) and that you should not use
async
for those handler as that will cause the methods to be completed.This is ASP.NET 1.0.0 on Windows 10 using Kestrel (1.0.0).
Startup.cs
The text was updated successfully, but these errors were encountered: