-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
Description
When using .NET 10, the breaking change
states that AppDomain.ProcessExit will no longer be raised for CTRL_CLOSE_EVENT. The recommended workaround is to use PosixSignalRegistration.Create.
However, when closing the console window on Windows, the registered handler is not invoked — so cleanup code never executes.
This is a regression from .NET 8, where AppDomain.CurrentDomain.ProcessExit fired consistently on console close and cleanup logic ran.
Reproduction Steps
Minimal repro
Class library (cleanup manager)
using System;
using System.IO;
using System.Runtime.InteropServices;
public static class CleanupManager
{
static CleanupManager()
{
// .NET 10 path (expected to work but does not on Windows console close)
PosixSignalRegistration.Create(PosixSignal.SIGTERM, context =>
{
CleanUp("SIGTERM handler");
});
// Uncomment for .NET 8 path:
// AppDomain.CurrentDomain.ProcessExit += (_, __) => CleanUp("ProcessExit");
}
public static int counter = 0;
private static void CleanUp(string source)
{
var path = Path.Combine(Path.GetTempPath(), "cleanup_log.txt");
File.AppendAllText(path, $"{DateTime.Now}: Cleanup triggered from {source}{Environment.NewLine}");
}
}Console app
class Program
{
static void Main(string[] args)
{
var _ = CleanupManager.counter; // trigger static constructor
Console.WriteLine("App running. Close console window to test.");
while (true)
{
Thread.Sleep(1000);
}
}
}Steps to reproduce
- Create the class library and console app above.
- Run the console app targeting .NET 8.0.
- Close the console window (click the X).
- Observe that cleanup-log.txt is created in %TEMP% with log entries.
- Run the same console app targeting .NET 10.0.
- Close the console window (click the X).
- Observe that no log file is created → handler never fired.
Expected behavior
Since the breaking change doc
instructs developers to use PosixSignalRegistration.Create for CTRL_CLOSE_EVENT, the handler should fire when the console window is closed on Windows.
Or, documentation should explicitly state that CTRL_CLOSE_EVENT is not supported and provide guidance for how cleanup should be handled on Windows.
Actual behavior
- In .NET 10, closing the console window via the X button does not invoke the
PosixSignalRegistration.Create(PosixSignal.SIGTERM, …)handler. - Cleanup logic (temp log file) is never executed.
Regression?
Yes, this works when the console app targets .NET 8.
Known Workarounds
No response
Configuration
- .NET SDK: 8.0.x → works (ProcessExit fires)
- .NET SDK: 10.0.x → fails (SIGTERM handler not invoked)
- OS: Windows 11
- Architecture: x64
Other information
Question / Assistance needed
-
Is this the intended behavior on Windows (that
PosixSignalRegistration.Create(PosixSignal.SIGTERM, …)will not reliably fire forCTRL_CLOSE_EVENT)? -
If so, what is the supported approach for ensuring cleanup when the console window is closed?
-
Is there a new recommended API?