[PR #18228] Properly implement WaitForData for ReadConsoleInput #31477

Open
opened 2026-01-31 09:47:31 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/microsoft/terminal/pull/18228

State: closed
Merged: Yes


There were two bugs:

  • Ever since the conhost v1 -> v2 rewrite the readDataDirect.cpp
    implementation incorrectly passed false as the wait flag.
    The unintentional mistake is obvious in hindsight as the
    check for CONSOLE_STATUS_WAIT makes no sense in this case.
  • The ConPTY integration into InputBuffer was done incorrectly,
    as it would unconditionally wake up the readers/waiters without
    checking if the buffer is now actually non-empty.

Closes #15859

Validation Steps Performed

Test code:

#include <Windows.h>
#include <stdio.h>

int main() {
    HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
    INPUT_RECORD buf[128];
    DWORD read;

    SetConsoleMode(
        in,
        ENABLE_PROCESSED_INPUT | ENABLE_VIRTUAL_TERMINAL_INPUT
    );

    for (int i = 0; ReadConsoleInputW(in, buf, 128, &read); ++i) {
        printf("%d read=%lu\n", i, read);
    }

    return 0;
}

Run it under Windows Terminal and type any input. >50% of all
inputs will result in read=0. This is fixed after this PR.

**Original Pull Request:** https://github.com/microsoft/terminal/pull/18228 **State:** closed **Merged:** Yes --- There were two bugs: * Ever since the conhost v1 -> v2 rewrite the `readDataDirect.cpp` implementation incorrectly passed `false` as the wait flag. The unintentional mistake is obvious in hindsight as the check for `CONSOLE_STATUS_WAIT` makes no sense in this case. * The ConPTY integration into `InputBuffer` was done incorrectly, as it would unconditionally wake up the readers/waiters without checking if the buffer is now actually non-empty. Closes #15859 ## Validation Steps Performed Test code: ```cpp #include <Windows.h> #include <stdio.h> int main() { HANDLE in = GetStdHandle(STD_INPUT_HANDLE); INPUT_RECORD buf[128]; DWORD read; SetConsoleMode( in, ENABLE_PROCESSED_INPUT | ENABLE_VIRTUAL_TERMINAL_INPUT ); for (int i = 0; ReadConsoleInputW(in, buf, 128, &read); ++i) { printf("%d read=%lu\n", i, read); } return 0; } ``` Run it under Windows Terminal and type any input. >50% of all inputs will result in `read=0`. This is fixed after this PR.
claunia added the pull-request label 2026-01-31 09:47:31 +00:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#31477