runtime: CoreCLR's CNTRL_SHUTDOWN_EVENT handler prevents graceful exit of services during system shutdown

Description

I am not sure if this belongs here, but this is a new weird behaviour I have been fighting with lately.

I have a service, where the ‘Main_Worker’ is a ‘IHostedService’ and a ‘BackgroundService’. I have overriden the ‘StopAsync’ function and the only thing it really does is write to the Windows Event Log a message and save a txt file with some data.

While I was working with this structure in .NET 6, it all worked preatty well. Then I upgraded to .Net 7, and it seems that now ‘StopAsync’ is not triggering when I turn of the PC by a normal Power -> Shutdown or if I use ‘Process.Start(“cmd”, @“/c shutdown -s -t 0”);’

When in .NET 6, both ways it worked without problems.

Reproduction Steps

public async override Task<Task> StopAsync(CancellationToken cancellationToken) { Log.Text("Stopping Worker_Main service..."); logger.LogWarning("Test Service is Stopping..."); await Log.SaveCurrentLog(); return base.StopAsync(cancellationToken); }

That is my StopAsync function, it used to work like this with no problems. But now, even if you take away the await line, not even the logger is appearing.

Expected behavior

At least I would expect to see the logged message in the Windows Event Logger when the PC is turned on again.

Actual behavior

No trace of the message being logged or the file being written. The service probably closes, yet I have no idea if it is in a forced mode or if it is actually being closed in a gracefull way.

Regression?

No response

Known Workarounds

I do not have workarounds at the moment.

Configuration

Have tested in Windows 10 & 11 Both are x64

Other information

No response

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 36 (26 by maintainers)

Most upvoted comments

This one is actually different than the error 1067 issue. In that case the service process was exiting before completing ServiceBase’s OnStop method which is what notified SCM that the service cleanly stopped. In that case it would run all ApplicationStopping and ApplicationStopped.

In this case the machine is shutting down. I was looking at a similar issue internally and found that the problem was actually due to https://github.com/dotnet/runtime/pull/41101. During a shutdown a service will first receive a CNTRL_SHUTDOWN_EVENT. The default is to do nothing for a service process per https://learn.microsoft.com/en-us/windows/console/handlerroutine#remarks, but that was changed for .NET processes in https://github.com/dotnet/runtime/pull/41101.

I was able to workaround this by adding ConsoleControlHandler that cancels other handlers when seeing CNTRL_SHUTDOWN_EVENT - stopping the runtime’s handler from shutting down the process and letting the service exit normally. In .NET 6.0 you can do this with a one-liner:

    using var registration = PosixSignalRegistration.Create(PosixSignal.SIGTERM, (context) => context.Cancel = true);

Just make sure that registration stays alive for the entire duration of the service - I put it in the Main() routine.

We should look into adding something like this to ServiceBase – or having the runtime try to avoid handling CNTRL_SHUTDOWN_EVENT for services.

Makes sense. It is .NET Core -> .NET Framework regression introduced by https://github.com/dotnet/coreclr/pull/17265.

https://github.com/dotnet/coreclr/pull/17265 was an attempt to fix the console lifetime issues without introducing proper signal handling APIs. It did not work well, and we ended up introducing proper signal handling APIs anyway.

Should we revert https://github.com/dotnet/coreclr/pull/17265 and go back to .NET Framework behavior so that we are not baking app-model specific concerns into runtime?

  • We may need to treat it as a breaking change
  • It would align the behavior between regular CoreCLR and NAOT. NAOT does not register the shutdown handler and nobody noticed so far.

The primary issue is that a service should not call ExitProcess when receiving the CTRL_SHUTDOWN_EVENT, as described in https://learn.microsoft.com/en-us/windows/console/handlerroutine#remarks:

When a console application is run as a service, it receives a modified default console control handler. This modified handler does not call ExitProcess when processing the CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT signals. This allows the service to continue running after the user logs off. If the service installs its own console control handler, this handler is called before the default handler. If the installed handler calls ExitProcess when processing the CTRL_LOGOFF_EVENT signal, the service exits when the user logs off.

The problem got introduced when .NET started to register the control handler in #55333. To fix that, we should change the behavior of our handler when the .NET app is running as a service. In fact, I’ve discussed that with Windows developers in the past and they said that the MSDN doc is slightly misleading and this is true for system session, not just services.

@janvorli @ericstj What do you expect the runtime fix to be?

If I am interpreting the comments above correctly, this is what’s happening in general:

  1. The OS starts sending shutdown callbacks to services (e.g. CONTROL_SHUTDOWN).
  2. The service does not cleanup completely before returning from these callbacks. There is some ongoing async/background activity.
  3. This background activity can encounter unhandled exceptions if it attempts to call into other services that has been shutdown already (e.g. log events using EventLog)

To avoid unhandled exceptions in the last step, I think the service either needs to shutdown completely in the response to the shutdown callback and/or the service needs to start ignoring unhandled exceptions at some point during shutdown. https://github.com/dotnet/runtime/issues/42275 may help with the later.

believe there is a workaround in place to help with this issue? Will mark it for 9 to determine the feasibility of the fix.

This issue did help us discover the shutdown problem with CoreCLR’s CNTRL_SHUTDOWN_EVENT which I mentioned here: https://github.com/dotnet/runtime/issues/83093#issuecomment-1470814801.

We’ve seen other reports of this same problem, so I’d like to keep this issue open to track a fix for that in CoreCLR. @janvorli was looking into a fix for that.

Right, so I think that things have gone really clearer for me with this.

Part of the problem is that I was trying to use the backgroundservice in a way its not meant to be used.

So:

  • If ExecuteAsync has a loop, it not necessarily will be fully finished when the service is ending.
  • StopAsync is called at the end, but whatever happens in it might already be unloaded or unavailable.
  • StopAsync shouldn’t have long tasks in it.

I have modified how I do the logging, and seems to be working fine now.

Thanks a lot @ericstj for your help through this.

If we all are ok, I am good to close this issue.

@gkapellmann I took a look at your repro. I was able to find out what was wrong by using Time-Travel debugging and examining the behavior at shutdown.

The PosixSignalRegistration workaround helped in that it did prevent the runtime from disabling exception handling, but the problem was there was still an unhandled exception in the logger:

(3/17/2023 9:24:59 AM) .ctor> Constructor...
(3/17/2023 9:24:59 AM) StartAsync> StartAsync...
(3/17/2023 9:24:59 AM) ExecuteAsync> Start of ExecuteAsync...
(3/17/2023 9:25:07 AM) StopAsync> StopAsync...
(3/17/2023 9:25:07 AM) StopAsync> System.AggregateException: An error occurred while writing to logger(s). (The interface is unknown.)
 ---> System.ComponentModel.Win32Exception (1717): The interface is unknown.
   at System.Diagnostics.EventLogInternal.InternalWriteEvent(UInt32 eventID, UInt16 category, EventLogEntryType type, String[] strings, Byte[] rawData, String currentMachineName)
   at System.Diagnostics.EventLogInternal.WriteEvent(EventInstance instance, Byte[] data, Object[] values)
   at System.Diagnostics.EventLog.WriteEvent(EventInstance instance, Byte[] data, Object[] values)
   at System.Diagnostics.EventLog.WriteEvent(EventInstance instance, Object[] values)
   at Microsoft.Extensions.Logging.EventLog.WindowsEventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category)
   at Microsoft.Extensions.Logging.EventLog.EventLogLogger.WriteMessage(String message, EventLogEntryType eventLogEntryType, Int32 eventId)
   at Microsoft.Extensions.Logging.EventLog.EventLogLogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)
   at Microsoft.Extensions.Logging.Logger.<Log>g__LoggerLog|13_0[TState](LogLevel logLevel, EventId eventId, ILogger logger, Exception exception, Func`3 formatter, List`1& exceptions, TState& state)
   --- End of inner exception stack trace ---
   at Microsoft.Extensions.Logging.Logger.ThrowLoggingError(List`1 exceptions)
   at Microsoft.Extensions.Logging.Logger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)
   at Microsoft.Extensions.Logging.Logger`1.Microsoft.Extensions.Logging.ILogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)
   at Microsoft.Extensions.Logging.LoggerExtensions.Log(ILogger logger, LogLevel logLevel, EventId eventId, Exception exception, String message, Object[] args)
   at Microsoft.Extensions.Logging.LoggerExtensions.Log(ILogger logger, LogLevel logLevel, String message, Object[] args)
   at Microsoft.Extensions.Logging.LoggerExtensions.LogInformation(ILogger logger, String message, Object[] args)
   at TestService.Worker.StopAsync(CancellationToken cancellationToken) in C:\scratch\SimpleWinService_dotnet\VeroTest-WinService\Worker.cs:line 45

This was preventing you from running your SaveCurrentLog method. I was able to workaround that by wrapping the call to _logger.LogInformation in your stopasync with a try/catch.

I’ll take a deeper look at this to see why it’s happening. Could be that this is normal during shutdown. Could be that a handle got disposed out from under the logger.

Ideally we don’t do this only in ServiceBase since that wouldn’t fix cases where folks were implementing services themselves without using ServiceBase. Instead we should try to replicate whatever the OS does in the default control handler to distinguish between normal console processes and services.

Looks similar to https://github.com/dotnet/runtime/issues/62579 and https://github.com/dotnet/runtime/issues/81274, @gkapellmann could you try the workaround mentioned and let us know if that helps your issue? The workaround is for unblocking you for short term, if you could let us know how it goes, it would be a useful data for to the team to find appropriate fix.