Skip to content

Commit

Permalink
tty, win: fix read stop for raw mode
Browse files Browse the repository at this point in the history
New Windows version requires `EventType` to be set to something
meaningful, otherwise WriteConsoleInputW() will fail with
`ERROR_INVALID_PARAMETER`. This sets it to `FOCUS_EVENT` which
is ignored by `uv_process_tty_read_raw_req()`.

Fixes: nodejs/node#21773
PR-URL: libuv#1989
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jameson Nash <vtjnash@gmail.com>
  • Loading branch information
bzoz authored and cjihrig committed Sep 26, 2018
1 parent 4bd0187 commit b9a0840
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/win/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,7 @@ int uv_tty_read_stop(uv_tty_t* handle) {
/* Cancel raw read. Write some bullshit event to force the console wait to
* return. */
memset(&record, 0, sizeof record);
record.EventType = FOCUS_EVENT;
if (!WriteConsoleInputW(handle->handle, &record, 1, &written)) {
return GetLastError();
}
Expand Down
2 changes: 2 additions & 0 deletions test/test-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ TEST_DECLARE (tty)
TEST_DECLARE (tty_raw)
TEST_DECLARE (tty_empty_write)
TEST_DECLARE (tty_large_write)
TEST_DECLARE (tty_raw_cancel)
#endif
TEST_DECLARE (tty_file)
TEST_DECLARE (tty_pty)
Expand Down Expand Up @@ -480,6 +481,7 @@ TASK_LIST_START
TEST_ENTRY (tty_raw)
TEST_ENTRY (tty_empty_write)
TEST_ENTRY (tty_large_write)
TEST_ENTRY (tty_raw_cancel)
#endif
TEST_ENTRY (tty_file)
TEST_ENTRY (tty_pty)
Expand Down
35 changes: 35 additions & 0 deletions test/test-tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,41 @@ TEST_IMPL(tty_large_write) {
MAKE_VALGRIND_HAPPY();
return 0;
}

TEST_IMPL(tty_raw_cancel) {
int r;
int ttyin_fd;
uv_tty_t tty_in;
uv_loop_t* loop;
HANDLE handle;

loop = uv_default_loop();
/* Make sure we have an FD that refers to a tty */
handle = CreateFileA("conin$",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
ASSERT(handle != INVALID_HANDLE_VALUE);
ttyin_fd = _open_osfhandle((intptr_t) handle, 0);
ASSERT(ttyin_fd >= 0);
ASSERT(UV_TTY == uv_guess_handle(ttyin_fd));

r = uv_tty_init(uv_default_loop(), &tty_in, ttyin_fd, 1); /* Readable. */
ASSERT(r == 0);
r = uv_tty_set_mode(&tty_in, UV_TTY_MODE_RAW);
ASSERT(r == 0);
r = uv_read_start((uv_stream_t*)&tty_in, tty_raw_alloc, tty_raw_read);
ASSERT(r == 0);

r = uv_read_stop((uv_stream_t*) &tty_in);
ASSERT(r == 0);

MAKE_VALGRIND_HAPPY();
return 0;
}
#endif


Expand Down

0 comments on commit b9a0840

Please sign in to comment.