Skip to content

Commit

Permalink
win, tty: fix problem of receiving unexpected SIGWINCH
Browse files Browse the repository at this point in the history
Fix an issue where WINDOWS_BUFFER_SIZE_EVENT occurs and unexpected
SIGWINCH is received before calling
uv__tty_console_resize_message_loop_thread.

Refs: neovim/neovim#10978 (comment)
PR-URL: libuv#2478
Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com>
Reviewed-By: Saúl Ibarra Corretgé <s@saghul.net>
(cherry picked from commit 5013042)
  • Loading branch information
erw7 authored and musm committed Jul 9, 2020
1 parent 6888be4 commit 911c405
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/win/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,15 @@ void uv_console_init(void) {
0,
0);
if (uv__tty_console_handle != INVALID_HANDLE_VALUE) {
CONSOLE_SCREEN_BUFFER_INFO sb_info;
QueueUserWorkItem(uv__tty_console_resize_message_loop_thread,
NULL,
WT_EXECUTELONGFUNCTION);
uv_mutex_init(&uv__tty_console_resize_mutex);
if (GetConsoleScreenBufferInfo(uv__tty_console_handle, &sb_info)) {
uv__tty_console_width = sb_info.dwSize.X;
uv__tty_console_height = sb_info.srWindow.Bottom - sb_info.srWindow.Top + 1;
}
}
}

Expand Down Expand Up @@ -2349,16 +2355,35 @@ static void uv__determine_vterm_state(HANDLE handle) {
}

static DWORD WINAPI uv__tty_console_resize_message_loop_thread(void* param) {
CONSOLE_SCREEN_BUFFER_INFO sb_info;
NTSTATUS status;
ULONG_PTR conhost_pid;
MSG msg;

if (!GetConsoleScreenBufferInfo(uv__tty_console_handle, &sb_info))
if (pSetWinEventHook == NULL || pNtQueryInformationProcess == NULL)
return 0;

status = pNtQueryInformationProcess(GetCurrentProcess(),
ProcessConsoleHostProcess,
&conhost_pid,
sizeof(conhost_pid),
NULL);

if (!NT_SUCCESS(status)) {
/* We couldn't retrieve our console host process, probably because this
* is a 32-bit process running on 64-bit Windows. Fall back to receiving
* console events from the input stream only. */
return 0;
}

uv__tty_console_width = sb_info.dwSize.X;
uv__tty_console_height = sb_info.srWindow.Bottom - sb_info.srWindow.Top + 1;
/* Ensure the PID is a multiple of 4, which is required by SetWinEventHook */
conhost_pid &= ~(ULONG_PTR)0x3;

if (pSetWinEventHook == NULL)
uv__tty_console_resized = CreateEvent(NULL, TRUE, FALSE, NULL);
if (uv__tty_console_resized == NULL)
return 0;
if (QueueUserWorkItem(uv__tty_console_resize_watcher_thread,
NULL,
WT_EXECUTELONGFUNCTION) == 0)
return 0;

if (!pSetWinEventHook(EVENT_CONSOLE_LAYOUT,
Expand Down Expand Up @@ -2393,6 +2418,8 @@ static void CALLBACK uv__tty_console_resize_event(HWINEVENTHOOK hWinEventHook,
width = sb_info.dwSize.X;
height = sb_info.srWindow.Bottom - sb_info.srWindow.Top + 1;

uv_mutex_lock(&uv__tty_console_resize_mutex);
assert(uv__tty_console_width != -1 && uv__tty_console_height != -1);
if (width != uv__tty_console_width || height != uv__tty_console_height) {
uv__tty_console_width = width;
uv__tty_console_height = height;
Expand Down

0 comments on commit 911c405

Please sign in to comment.