Terminal window/tab is not closed when console application calls FreeConsole #18934

Closed
opened 2026-01-31 06:28:52 +00:00 by claunia · 4 comments
Owner

Originally created by @edo9300 on GitHub (Nov 20, 2022).

Originally assigned to: @lhecker on GitHub.

Windows Terminal version

No response

Windows build number

No response

Other Software

No response

Steps to reproduce

On windows 11, with the windows terminal being set as the default console application, start a program that was compiled for the console subsystem from explorer.
That program calls FreeConsole during its lifetime and then keeps running (either because it has spawned a gui or it is still has work to do).

Expected Behavior

The console window that got spawned by that program is closed as it did with cmd on previous windows versions.

Actual Behavior

The console window stays open until the program terminates the execution.

Originally created by @edo9300 on GitHub (Nov 20, 2022). Originally assigned to: @lhecker on GitHub. ### Windows Terminal version _No response_ ### Windows build number _No response_ ### Other Software _No response_ ### Steps to reproduce On windows 11, with the windows terminal being set as the default console application, start a program that was compiled for the console subsystem from explorer. That program calls ```FreeConsole``` during its lifetime and then keeps running (either because it has spawned a gui or it is still has work to do). ### Expected Behavior The console window that got spawned by that program is closed as it did with cmd on previous windows versions. ### Actual Behavior The console window stays open until the program terminates the execution.
claunia added the Resolution-Fix-CommittedIssue-BugProduct-TerminalArea-DefApp labels 2026-01-31 06:28:53 +00:00
Author
Owner

@edo9300 commented on GitHub (Nov 20, 2022):

I've looked at other similar issues on this repository, but all of them are related to the program manually working on the console window HANDLE that as said various times was a deprecated behaviour, for FreeConsole instead there's no such deprecation notice, and as the docs says A console is closed when the last process attached to it terminates or calls FreeConsole., which is the behaviour I expect as only my program should be attached to that console. (That is also emphasized by the fact that when the program terminates then the console closes as well, and that indicates that there are no other programs attached to it.)

@edo9300 commented on GitHub (Nov 20, 2022): I've looked at other similar issues on this repository, but all of them are related to the program manually working on the console window HANDLE that as said various times was a deprecated behaviour, for ```FreeConsole``` instead there's no such deprecation notice, and as the docs says ```A console is closed when the last process attached to it terminates or calls FreeConsole.```, which is the behaviour I expect as only my program should be attached to that console. (That is also emphasized by the fact that when the program terminates then the console closes as well, and that indicates that there are no other programs attached to it.)
Author
Owner

@lhecker commented on GitHub (Nov 21, 2022):

We could make this work by removing ConPTY's \Reference handle from the ConsoleProcessList after starting OpenConsole, if OpenConsole was started as part of a handoff.

BTW if you're using FreeConsole to hide the console conditionally as part of your startup, I personally believe a better option might be to go with AttachConsole(ATTACH_PARENT_PROCESS) and reopen stdout/stdin. I haven't tested it, but it should look something like this:

// Compile with /SUBSYSTEM:WINDOWS!
if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
    AllocConsole();
}

FILE* stdinNew = nullptr;
freopen_s(&stdinNew, "CONIN$", "r+", stdin);
FILE* stdoutNew = nullptr;
freopen_s(&stdoutNew, "CONOUT$", "w+", stdout);
FILE* stderrNew = nullptr;
freopen_s(&stderrNew, "CONOUT$", "w+", stderr);

printf("Hello, World!\n");
@lhecker commented on GitHub (Nov 21, 2022): We could make this work by removing ConPTY's `\Reference` handle from the `ConsoleProcessList` after starting OpenConsole, if OpenConsole was started as part of a handoff. BTW if you're using `FreeConsole` to hide the console conditionally as part of your startup, I personally believe a better option might be to go with `AttachConsole(ATTACH_PARENT_PROCESS)` and reopen stdout/stdin. I haven't tested it, but it should look something like this: ```cpp // Compile with /SUBSYSTEM:WINDOWS! if (!AttachConsole(ATTACH_PARENT_PROCESS)) { AllocConsole(); } FILE* stdinNew = nullptr; freopen_s(&stdinNew, "CONIN$", "r+", stdin); FILE* stdoutNew = nullptr; freopen_s(&stdoutNew, "CONOUT$", "w+", stdout); FILE* stderrNew = nullptr; freopen_s(&stderrNew, "CONOUT$", "w+", stderr); printf("Hello, World!\n"); ```
Author
Owner

@edo9300 commented on GitHub (Nov 21, 2022):

BTW if you're using FreeConsole to hide the console conditionally as part of your startup

Yes, I'm doing that but because before my program reads its configs from which the console can be disabled, it has already printed some debug information that would not be there if I created the console afterwards (this is where I'm doing that call, by that time the console has already debug Info from its libraries printed to the console bce9989dbf/gframe/gframe.cpp (L223)), so I can't create the console afterwards.

@edo9300 commented on GitHub (Nov 21, 2022): > BTW if you're using FreeConsole to hide the console conditionally as part of your startup Yes, I'm doing that but because before my program reads its configs from which the console can be disabled, it has already printed some debug information that would not be there if I created the console afterwards (this is where I'm doing that call, by that time the console has already debug Info from its libraries printed to the console https://github.com/edo9300/edopro/blob/bce9989dbf6f497d1b5f3c009f4b6328d80e9201/gframe/gframe.cpp#L223), so I can't create the console afterwards.
Author
Owner

@ghost commented on GitHub (Jan 24, 2023):

:tada:This issue was addressed in #14544, which has now been successfully released as Windows Terminal Preview v1.17.1023.🎉

Handy links:

@ghost commented on GitHub (Jan 24, 2023): :tada:This issue was addressed in #14544, which has now been successfully released as `Windows Terminal Preview v1.17.1023`.:tada: Handy links: * [Release Notes](https://github.com/microsoft/terminal/releases/tag/v1.17.1023) * [Store Download](https://www.microsoft.com/store/apps/9n8g5rfz9xk3?cid=storebadge&ocid=badge)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#18934