microsoft / terminal

The new Windows Terminal and the original Windows console host, all in the same place!
MIT License
95.85k stars 8.34k forks source link

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

Closed edo9300 closed 1 year ago

edo9300 commented 2 years ago

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.

edo9300 commented 2 years ago

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.)

lhecker commented 2 years ago

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");
edo9300 commented 2 years ago

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.

ghost commented 1 year ago

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

Handy links: