FreeConsole() doesn't work past 5s during CTRL_CLOSE_EVENT #20105

Open
opened 2026-01-31 07:03:44 +00:00 by claunia · 2 comments
Owner

Originally created by @jredfox on GitHub (Jun 17, 2023).

Windows Terminal version

1.17.11461.0

Windows build number

10.0.19045.3086

Other Software

c++
mingwx64
an ide of your choice
cpu arch: x64

Steps to reproduce

compile this code in c++ and run it.
go to task manager --> open terminal group processes --> program.exe --> go to details
click the x button on windows terminal.
the process of program.exe is no longer running and if you insert a while loop in the handler it closes 5 seconds later. This bug is reproducible with cmd powershell and conhost and windows terminal. I don't know which program issue or issues its on so I am reporting it here

BOOL WINAPI controlHandler(DWORD sig)
{
	if(sig == CTRL_CLOSE_EVENT)
	{
		Beep(800, 500);
		FreeConsole();
		return TRUE;
	}
}
int main()
{
   SetConsoleCtrlHandler(&controlHandler, TRUE);
   while(true);
}

Expected Behavior

the process to stay open

Actual Behavior

process closes

FreeConsole() api doesn't have a parameter of PID and doesn't free all child processes another bug

Originally created by @jredfox on GitHub (Jun 17, 2023). ### Windows Terminal version 1.17.11461.0 ### Windows build number 10.0.19045.3086 ### Other Software c++ mingwx64 an ide of your choice cpu arch: x64 ### Steps to reproduce compile this code in c++ and run it. go to task manager --> open terminal group processes --> program.exe --> go to details click the x button on windows terminal. the process of program.exe is no longer running and if you insert a while loop in the handler it closes 5 seconds later. This bug is reproducible with cmd powershell and conhost and windows terminal. I don't know which program issue or issues its on so I am reporting it here ```c++ BOOL WINAPI controlHandler(DWORD sig) { if(sig == CTRL_CLOSE_EVENT) { Beep(800, 500); FreeConsole(); return TRUE; } } int main() { SetConsoleCtrlHandler(&controlHandler, TRUE); while(true); } ``` ### Expected Behavior the process to stay open ### Actual Behavior process closes ### Related Note: FreeConsole() api doesn't have a parameter of PID and doesn't free all child processes another bug
Author
Owner

@lhecker commented on GitHub (Jun 19, 2023):

The 5s timeout stems from SPI_GETHUNGAPPTIMEOUT and you cannot prevent the console from killing your process by calling FreeConsole in the SetConsoleCtrlHandler callback. Personally speaking, I'm actively not wishing to fix this right now. It'd be weird if a previously visible console application could keep running indefinitely even after the user closed the window explicitly. It's like the window vanishing on its own when FreeConsole is called, but even worse in a way?

If you ever choose to do hacks like this one I need to point out that they can (and honestly probably will, given enough time) break in the future if we ever choose to not inject threads anymore.

@lhecker commented on GitHub (Jun 19, 2023): The 5s timeout stems from `SPI_GETHUNGAPPTIMEOUT` and you cannot prevent the console from killing your process by calling `FreeConsole` in the `SetConsoleCtrlHandler` callback. Personally speaking, I'm actively not wishing to fix this right now. It'd be weird if a previously visible console application could keep running indefinitely even after the user closed the window explicitly. It's like the window vanishing on its own when `FreeConsole` is called, but even worse in a way? If you ever choose to do hacks like [this one](https://stackoverflow.com/a/18445043) I need to point out that they can (and honestly probably will, given enough time) break in the future if we ever choose to not inject threads anymore.
Author
Owner

@lhecker commented on GitHub (Jun 21, 2023):

I think we should investigate whether we should support this. If it's easy to implement: Let's just do it! (We'd also accept a PR if it's a simple change.) But we don't really believe that this is an important aspect to fix and implement right now, unless more people request this or if any other circumstances change.

@lhecker commented on GitHub (Jun 21, 2023): I think we should investigate whether we should support this. If it's easy to implement: Let's just do it! (We'd also accept a PR if it's a simple change.) But we don't really believe that this is an important aspect to fix and implement right now, unless more people request this or if any other circumstances change.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#20105