Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with cursor position escape #18117

Open
cracyc opened this issue Oct 27, 2024 · 6 comments
Open

Problems with cursor position escape #18117

cracyc opened this issue Oct 27, 2024 · 6 comments
Assignees
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting

Comments

@cracyc
Copy link

cracyc commented Oct 27, 2024

Windows Terminal version

1.22.2912.0

Windows build number

10.0.22631.4196

Other Software

https://github.com/cracyc/msdos-player/blob/vt/msdos.cpp#L1088

Steps to reproduce

Start it in a new console window or run in an existing one and execute a command with lots of output.

Expected Behavior

No response

Actual Behavior

When the program starts it immediately gets the console window size and the cursor position which hangs forever. If run from an existing console window it starts fine but if say command.com is run and dir then it will randomly hang waiting for the reply to ESC [6n, although it will resume after a bit or a key is pressed. This does not happen on terminal version 1.21.2911.0, in legacy conhost or if GetConsoleScreenBufferInfo is used instead.

@cracyc cracyc added Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels Oct 27, 2024
@shaymuiruri
Copy link

I would love to work on this issue.

@carlos-zamora carlos-zamora added this to the Terminal v1.23 milestone Oct 30, 2024
@carlos-zamora
Copy link
Member

Keeping the Triage tag on, but this may be of interest to @lhecker

@carlos-zamora
Copy link
Member

Thanks for filing. A few questions from the team:

@j4james
Copy link
Collaborator

j4james commented Nov 6, 2024

@carlos-zamora I'm not sure if this is exactly the same thing, but I can reproduce similar behavior with a batch file like this:

@echo �[6n
@set /p "x=RESPONSE:"

NB: The character is meant to be an escape.

If you run that from within an open terminal window, you'll get the expected DSR response looking something like this:

RESPONSE:^[[13;1R

But if you try and run the the script by double clicking on the file in Windows Explorer, it'll open up in the terminal, and you'll see the RESPONSE: prompt, but no actual DSR response.

Interestingly the problem appears to be specific to the CPR request. Other report sequences I tried (e.g. DA1) worked fine. I suspect we're sending out own CPR request as part of the conpty startup sequence and that possibly ends up grabbing the script's response as well.

@cracyc
Copy link
Author

cracyc commented Nov 7, 2024

This will hang it for me:

#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdio.h>

int main() {
    const HANDLE input = GetStdHandle(STD_INPUT_HANDLE);

    DWORD input_mode_old;
    GetConsoleMode(input, &input_mode_old);
    SetConsoleMode(input, ENABLE_PROCESSED_INPUT | ENABLE_VIRTUAL_TERMINAL_INPUT);

    for (unsigned long long i = 0;; i++) {
        printf("%llu \x1b[6n", i);

        char byte;
        DWORD chrs;
        do {
            if (!ReadConsoleA(input, &byte, 1, &chrs, NULL) || chrs == 0) {
                goto exit;
            }
            if (byte < 0x20 || byte >= 0x7f) {
                printf("\\x%02x", byte);
            } else {
                printf("%c", byte);
            }
        } while (byte != 'R');

        printf("\n");
    }

exit:
    SetConsoleMode(input, input_mode_old);
    return 0;
}

@lhecker
Copy link
Member

lhecker commented Nov 7, 2024

That is an excellent repro! It really hangs almost immediately.
I've rewritten your example slightly though to make Ctrl-C work and get the terminal into the previous input mode when it exits. I also added some logic to make sure all possibly non-printables are escaped (just in case). Finally, I added a counter so that it's easier to spot when it stops.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting
Projects
Status: To Cherry Pick
Status: To Cherry Pick
Development

No branches or pull requests

5 participants