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

Reading input byte by byte causes lines with even numbers of characters #16606

Closed
paperdave opened this issue Jan 26, 2024 · 2 comments
Closed
Labels
Area-Input Related to input processing (key presses, mouse, etc.) Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-1 A description (P1) Product-Conhost For issues in the Console codebase Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release.

Comments

@paperdave
Copy link

Windows Terminal version

1.18.3181.0

Windows build number

10.0.22621.0

Other Software

This bug report is a minimized reproduction of oven-sh/bun#8504.

Steps to reproduce

Demo application "input.c"

#include <stdio.h>
#include <windows.h>

int main()
{
    HANDLE hStdin;
    DWORD len;
    char buf[1];

    hStdin = GetStdHandle(STD_INPUT_HANDLE);
    if (hStdin == INVALID_HANDLE_VALUE)
        printf("GetStdHandle failed with %lu\n", GetLastError());

    while (1)
    {
        if (!ReadFile(hStdin, buf, 1, &len, NULL)) // number of records read
            printf("ReadConsoleInput failed with %lu\n", GetLastError());

        printf("byte: %d\n", buf[0]);
    }
    return 0;
}

(This program is mimicking the behavior of the Zig standard library function std.io.Reader().readUntilDelimiterAlloc, which does a byte by byte read until it hits a specified character)

clang input.c (I used llvm 16.0.4, but I do not think this matters)

Run in Windows terminal. .\a.exe

If you type 0 or an even number of characters, the \n byte 10 is not read. only the \r byte is.

If you type an odd number of characters, everything is as normal.

Expected Behavior

I expect all lines to end with \r\n, to match the behavior of conhost.exe, ConEmu, Visual Studio Code's Integrated terminal, and also various terminals on Linux/MacOS over an ssh connection.

Actual Behavior

my inputs are: 1<return>12<return>123<return><return>, and then an S pressed by mistake when trying to take a screenshot.

image

@paperdave paperdave 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 Jan 26, 2024
paperdave added a commit to oven-sh/bun that referenced this issue Jan 26, 2024
Jarred-Sumner pushed a commit to oven-sh/bun that referenced this issue Jan 27, 2024
@lhecker lhecker self-assigned this Jan 29, 2024
@lhecker lhecker added Product-Conhost For issues in the Console codebase Area-Input Related to input processing (key presses, mouse, etc.) Priority-1 A description (P1) and removed Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels Jan 29, 2024
@lhecker lhecker modified the milestone: Terminal 1.21 Jan 29, 2024
@lhecker lhecker added the Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release. label Jan 29, 2024
@lhecker lhecker removed their assignment Jan 29, 2024
@lhecker
Copy link
Member

lhecker commented Jan 29, 2024

I've found that this PR fixed the issue: #16313
The issue was caused by this PR: #14745

It's planned to be shipped in the next service release for both 1.18 (Stable) and 1.19 (Preview). I'm sorry that it's been taking some time to ship this! But since the bug only started appearing in 1.18, I personally believe it's fine to not hotfix Zig just yet. (Although I do sort of think that using a buffered reader is a great idea, if it's an option. The ReadFile syscalls are very expensive.)

If you want to test the fix in the meantime, you can find the nightly Canary build here: https://aka.ms/terminal-canary-installer

@lhecker lhecker closed this as not planned Won't fix, can't repro, duplicate, stale Jan 29, 2024
@paperdave
Copy link
Author

Thank you.

In Bun i have changed our code to use a buffered reader for all platforms. This makes the readByte call less expensive since it will buffer an amount already.

It probably makes sense for us to leave that commit in regardless because it improves performance slightly. These are only used for terminal input, where the ReadFile is going to always be blocked on user input (bun init and the prompt() JS api).

Maybe I'll revert my change if there are issues consuming stdin from a user program while using the prompt api, but I suspect this case is very unlikely, and by the time this becomes an issue for anyone, Windows Terminal should be propagated to everyone.

ryoppippi pushed a commit to ryoppippi/bun that referenced this issue Feb 1, 2024
sroussey pushed a commit to sroussey/bun that referenced this issue Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Input Related to input processing (key presses, mouse, etc.) Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-1 A description (P1) Product-Conhost For issues in the Console codebase Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release.
Projects
None yet
Development

No branches or pull requests

2 participants