-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
win,tty: fix uv_tty_set_mode race conditions #1054
Conversation
Additional synchronization is needed to ensure that the program cannot modify the screen state while a line read is getting cancelled. Also, we need to stop any pending reads *before* calling SetConsoleMode, or a call to ReadConsole could start while the console is still in raw mode. Credit: orangemocha - Alexis Campailla <orangemocha@nodejs.org> Fixes: nodejs/node#7837
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some comments. Can a test for this be written?
/* thread and release it in another thread. Using a semaphore ensures that */ | ||
/* in such scenario the main thread will still block when trying to acquire */ | ||
/* the lock. */ | ||
static HANDLE uv_tty_output_lock; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use a uv_sem_t
here instead?
if (InterlockedOr(&uv__restore_screen_state, 0)) { | ||
HANDLE active_screen_buffer = CreateFileA("conout$", | ||
if (status == TRAP_REQUESTED) { | ||
/* If we canceled the read by sending a VK_RETURN event, restore the */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use a multi-line comment here, like it was before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What style to use? This:
/* comment comment
* comment comment
*/
or this:
/* comment comment
comment comment */
?
@@ -1035,11 +1052,16 @@ static int uv__cancel_read_console(uv_tty_t* handle) { | |||
|
|||
assert(!(handle->flags & UV_HANDLE_CANCELLATION_PENDING)); | |||
|
|||
/* Hold the output lock during the cancellation, to ensure that further |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
Updated, PTAL |
@@ -111,7 +111,12 @@ static int uv_tty_virtual_offset = -1; | |||
static int uv_tty_virtual_height = -1; | |||
static int uv_tty_virtual_width = -1; | |||
|
|||
static CRITICAL_SECTION uv_tty_output_lock; | |||
/* We use a semaphore rather than a mutex or critical section because in */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: can you adjust the comment style as below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with a style nit. A test would be nice though, not sure how possible that is. @bzoz?
Updated the comment style. I don't think that a test for this is possible, at least I don't know how to make one. |
LGTM with a leap of faith ;-) |
Additional synchronization is needed to ensure that the program cannot modify the screen state while a line read is getting cancelled. Also, we need to stop any pending reads *before* calling SetConsoleMode, or a call to ReadConsole could start while the console is still in raw mode. Credit: @orangemocha - Alexis Campailla <orangemocha@nodejs.org> Fixes: nodejs/node#7837 PR-URL: #1054 Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
Cheers, Bartosz, landed in 8414403. |
This reverts commit f59b888 now that the libuv update containing the proper fix has landed in 63243bc. Ref: libuv/libuv#1054 Ref: nodejs#7837
This reverts commit f59b888 now that the libuv update containing the proper fix has landed in 63243bc. Ref: libuv/libuv#1054 Ref: #7837 PR-URL: #8645 Reviewed-By: Nikolai Vavilov <vvnicholas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This reverts commit f59b888 now that the libuv update containing the proper fix has landed in 63243bc. Ref: libuv/libuv#1054 Ref: nodejs#7837 PR-URL: nodejs#8645 Reviewed-By: Nikolai Vavilov <vvnicholas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This reverts commit f59b888 now that the libuv update containing the proper fix has landed in 63243bc. Ref: libuv/libuv#1054 Ref: #7837 PR-URL: #8645 Reviewed-By: Nikolai Vavilov <vvnicholas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This reverts commit f59b888 now that the libuv update containing the proper fix has landed in 63243bc. Ref: libuv/libuv#1054 Ref: #7837 PR-URL: #8645 Reviewed-By: Nikolai Vavilov <vvnicholas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This reverts commit f59b888 now that the libuv update containing the proper fix has landed in 63243bc. Ref: libuv/libuv#1054 Ref: nodejs#7837 PR-URL: nodejs#8645 Reviewed-By: Nikolai Vavilov <vvnicholas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This reverts commit f59b888 now that the libuv update containing the proper fix has landed in 63243bc. Ref: libuv/libuv#1054 Ref: nodejs#7837 PR-URL: nodejs#8645 Reviewed-By: Nikolai Vavilov <vvnicholas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This reverts commit f59b888 now that the libuv update containing the proper fix has landed in 63243bc. Ref: libuv/libuv#1054 Ref: #7837 PR-URL: #8645 Reviewed-By: Nikolai Vavilov <vvnicholas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This reverts commit f59b888 now that the libuv update containing the proper fix has landed in 63243bc. Ref: libuv/libuv#1054 Ref: #7837 PR-URL: #8645 Reviewed-By: Nikolai Vavilov <vvnicholas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This reverts commit f59b888 now that the libuv update containing the proper fix has landed in 63243bc. Ref: libuv/libuv#1054 Ref: #7837 PR-URL: #8645 Reviewed-By: Nikolai Vavilov <vvnicholas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This reverts commit f59b888 now that the libuv update containing the proper fix has landed in 63243bc. Ref: libuv/libuv#1054 Ref: nodejs/node#7837 PR-URL: nodejs/node#8645 Reviewed-By: Nikolai Vavilov <vvnicholas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Additional synchronization is needed to ensure that the program cannot modify the screen state while a line read is getting cancelled.
Also, we need to stop any pending reads before calling
SetConsoleMode
, or a call toReadConsole
could start while the console is still in raw mode.We also switch to use a semaphore rather than a critical section because in some cases (
uv__cancel_read_console
) we need take the lock in the main thread and release it in another thread. Using a semaphore ensures that in such scenario the main thread will still block when trying to acquire the lockCredit: orangemocha - Alexis Campailla orangemocha@nodejs.org
Fixes: nodejs/node#7837