Skip to content

CoreCLR's CNTRL_SHUTDOWN_EVENT handler prevents graceful exit of services during system shutdown #83093

Open
@gkapellmann

Description

@gkapellmann

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions