Skip to content

Conversation

@bzoz
Copy link
Member

@bzoz bzoz commented Jul 7, 2017

Add SetWinEventHook for EVENT_CONSOLE_LAYOUT for better detection of
console resize events.

Ref: nodejs/node#13197

@bzoz
Copy link
Member Author

bzoz commented Jul 7, 2017

src/win/tty.c Outdated
/* Store the global tty output handle. This handle is used by TTY read */
/* streams to update the virtual window when a CONSOLE_BUFFER_SIZE_EVENT */
/* is received. */
uv_tty_console_handle = handle;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to hold the uv_tty_output_lock at least here, this is not thread safe.

@bzoz bzoz force-pushed the bartek-win-sigwinch branch from a805728 to 6418a8a Compare July 28, 2017 10:36
@bzoz
Copy link
Member Author

bzoz commented Jul 28, 2017

src/win/tty.c Outdated
* handle signalling SIGWINCH
*/

static HANDLE uv_tty_console_handle = INVALID_HANDLE_VALUE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you prefix these with uv__ instead? I know src/win is not very consistent in this regard, but let's try to be, for new code.

WINEVENT_OUTOFCONTEXT))
uv_fatal_error(GetLastError(), "SetWinEventHook");

while (GetMessage(&msg, NULL, 0, 0)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is this intettupted?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've described it in #1408 (comment)

src/win/tty.c Outdated
height = screen_buffer_info.srWindow.Bottom - screen_buffer_info.srWindow.Top + 1;

if (width != uv_tty_console_width || height != uv_tty_console_height) {
uv_tty_console_width = width;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to hold the lock

src/win/tty.c Outdated
if (!GetConsoleScreenBufferInfo(uv_tty_console_handle, &screen_buffer_info))
uv_fatal_error(GetLastError(), "GetConsoleScreenBufferInfo");

uv_tty_console_width = screen_buffer_info.dwSize.X;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to hold the lock

@bzoz bzoz force-pushed the bartek-win-sigwinch branch 2 times, most recently from b7e21a5 to 6048b42 Compare August 7, 2017 11:06
@bzoz
Copy link
Member Author

bzoz commented Aug 7, 2017

@saghul I've updated the change, PTAL.

I've moved the uv__tty_console_resize_message_loop_thread start to uv_console_init. This way SIGWINCH signals can be received before any call to uv_tty_init.

The loop in uv__tty_console_resize_message_loop_thread will never be interrupted - it will spin as long as the app is alive. The uv__tty_console_resize_event callback will be run in the uv__tty_console_resize_message_loop_thread thread. Since there is only one such thread no synchronization is needed.

CI: https://ci.nodejs.org/view/libuv/job/libuv-test-commit/364/

@bzoz
Copy link
Member Author

bzoz commented Aug 7, 2017

@bzoz
Copy link
Member Author

bzoz commented Aug 17, 2017

Ping?

src/win/tty.c Outdated
MSG msg;

if (!GetConsoleScreenBufferInfo(uv__tty_console_handle, &sb_info))
uv_fatal_error(GetLastError(), "GetConsoleScreenBufferInfo");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really fatal? Can't we live if it fails?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can, but there is no way to signal the user that this failed, and that SIGWINCH will not work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, but so what? it fails, we move on. I think it's far better than crashing!

src/win/tty.c Outdated
0,
0,
WINEVENT_OUTOFCONTEXT))
uv_fatal_error(GetLastError(), "SetWinEventHook");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

WINEVENT_OUTOFCONTEXT))
uv_fatal_error(GetLastError(), "SetWinEventHook");

while (GetMessage(&msg, NULL, 0, 0)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping

src/win/tty.c Outdated
int width, height;

if (!GetConsoleScreenBufferInfo(uv__tty_console_handle, &sb_info))
uv_fatal_error(GetLastError(), "GetConsoleScreenBufferInfo");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@refack
Copy link
Contributor

refack commented Aug 18, 2017

[biased scepticism, but I'm open to be persuaded]
I'm not sure this is worth it 🤷‍♂️ upstream we just added a disclaimer nodejs/node@ff07eaa

Do we know of any interesting uses for this event? (honest question)
Anyone that is doing GUI-like console apps? Because very few Windows console apps react to a window change, they keep assuming the initial size and just look horrible:

screenshot- 21 -console-rezise

@saghul
Copy link
Member

saghul commented Aug 18, 2017

I hate that broken behavior. If we can do better, why not? Also, Node is not the entire universe other projects are not going to read those disclaimers.

@bzoz
Copy link
Member Author

bzoz commented Aug 21, 2017

@saghul I've removed the uv_fatal_error calls, PTAL

@bzoz
Copy link
Member Author

bzoz commented Aug 24, 2017

@saghul, @refack PTAL

src/win/tty.c Outdated
NULL,
WT_EXECUTELONGFUNCTION);
if (r == 0)
abort();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think this should be fatal

src/win/tty.c Outdated
static void uv__determine_vterm_state(HANDLE handle);

void uv_console_init(void) {
int r;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can declare r inside the if where it's used.

@bzoz
Copy link
Member Author

bzoz commented Aug 29, 2017

Updated, PTAL

@bzoz
Copy link
Member Author

bzoz commented Sep 6, 2017

Ping?

Copy link
Member

@saghul saghul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Add SetWinEventHook for EVENT_CONSOLE_LAYOUT for better detection of
console resize events.

Ref: nodejs/node#13197
PR-URL: libuv#1408
Reviewed-By: Saúl Ibarra Corretgé <[email protected]>
@bzoz bzoz force-pushed the bartek-win-sigwinch branch from aa3dd00 to 0b9959e Compare September 14, 2017 07:31
@bzoz
Copy link
Member Author

bzoz commented Sep 14, 2017

@bzoz
Copy link
Member Author

bzoz commented Sep 14, 2017

bzoz added a commit that referenced this pull request Sep 14, 2017
Add SetWinEventHook for EVENT_CONSOLE_LAYOUT for better detection of
console resize events.

Ref: nodejs/node#13197
PR-URL: #1408
Reviewed-By: Saúl Ibarra Corretgé <[email protected]>
@bzoz
Copy link
Member Author

bzoz commented Sep 14, 2017

Landed in 6ad1e81

@bzoz bzoz closed this Sep 14, 2017
cjihrig pushed a commit that referenced this pull request Oct 2, 2017
This commit removes unused variables left over from
6ad1e81.

Refs: #1408
PR-URL: #1571
Reviewed-By: Santiago Gimeno <[email protected]>
Reviewed-By: Bartosz Sosnowski <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
bzoz added a commit to JaneaSystems/libuv that referenced this pull request Jul 18, 2019
Continuing improvement of SIGWINCH from PR libuv#2308.

Running SetWinEventHook without filtering for the specific PIDs has
significant impact on the performance of the entire system. This PR
changes the way SIGWINCH is handled.

The SetWinEventHook callback now signals a separate thread,
uv__tty_console_resize_watcher_thread. This thread calls
uv__tty_console_signal_resize() which checks if the console was actually
resized. The uv__tty_console_resize_watcher_thread makes sure to not to
call the uv__tty_console_signal_resize function more than 30 times per
second.

The SetWinEventHook will not be installed, if the PID of the
conhost.exe process that owns the console window cannot be
determinated. This can happen when a 32bit libuv app is running on a
64bit Windows.

For such cases PR libuv#1408 is partially reverted - when tty reads
WINDOW_BUFFER_SIZE_EVENT, it will also trigger a call to
uv__tty_console_signal_resize(). This will also help when the app is
running under console emulators. Documentation was alos updated to
reflect that.

Refs: microsoft/terminal#1811
Refs: microsoft/terminal#410
Refs: libuv#2308
bzoz added a commit to JaneaSystems/libuv that referenced this pull request Sep 5, 2019
Continuing improvement of SIGWINCH from PR libuv#2308.

Running SetWinEventHook without filtering for the specific PIDs has
significant impact on the performance of the entire system. This PR
changes the way SIGWINCH is handled.

The SetWinEventHook callback now signals a separate thread,
uv__tty_console_resize_watcher_thread. This thread calls
uv__tty_console_signal_resize() which checks if the console was actually
resized. The uv__tty_console_resize_watcher_thread makes sure to not to
call the uv__tty_console_signal_resize function more than 30 times per
second.

The SetWinEventHook will not be installed, if the PID of the
conhost.exe process that owns the console window cannot be
determinated. This can happen when a 32bit libuv app is running on a
64bit Windows.

For such cases PR libuv#1408 is partially reverted - when tty reads
WINDOW_BUFFER_SIZE_EVENT, it will also trigger a call to
uv__tty_console_signal_resize(). This will also help when the app is
running under console emulators. Documentation was alos updated to
reflect that.

Refs: microsoft/terminal#1811
Refs: microsoft/terminal#410
Refs: libuv#2308
bzoz added a commit that referenced this pull request Sep 5, 2019
Continuing improvement of SIGWINCH from PR #2308.

Running SetWinEventHook without filtering for the specific PIDs has
significant impact on the performance of the entire system. This PR
changes the way SIGWINCH is handled.

The SetWinEventHook callback now signals a separate thread,
uv__tty_console_resize_watcher_thread. This thread calls
uv__tty_console_signal_resize() which checks if the console was actually
resized. The uv__tty_console_resize_watcher_thread makes sure to not to
call the uv__tty_console_signal_resize function more than 30 times per
second.

The SetWinEventHook will not be installed, if the PID of the
conhost.exe process that owns the console window cannot be
determinated. This can happen when a 32bit libuv app is running on a
64bit Windows.

For such cases PR #1408 is partially reverted - when tty reads
WINDOW_BUFFER_SIZE_EVENT, it will also trigger a call to
uv__tty_console_signal_resize(). This will also help when the app is
running under console emulators. Documentation was also updated to
reflect that.

Refs: microsoft/terminal#1811
Refs: microsoft/terminal#410
Refs: #2308

PR-URL: #2381
Reviewed-By: Ben Noordhuis <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants