-
Notifications
You must be signed in to change notification settings - Fork 1.8k
CLI: Fix hang with exec/run -i with self-exiting command and open stdin #40921
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,49 @@ using wsl::windows::common::io::ReadConsoleHandle; | |
| using wsl::windows::common::io::ReadHandle; | ||
| using wsl::windows::common::io::RelayHandle; | ||
|
|
||
| namespace { | ||
|
|
||
| // Interrupts and joins the stdin-relay worker thread at teardown. | ||
| // | ||
| // The worker only exists when stdin is not a character device (any non-FILE_TYPE_CHAR handle, e.g. a | ||
| // redirected pipe), so this no-ops (not joinable) for a console. When stdin is a non-overlapped | ||
| // (synchronous) pipe the worker blocks in a ReadFile() that neither the exit event nor CancelIoEx() can | ||
| // interrupt, so the join() below would hang until stdin is closed -- the bug this guards against. | ||
| void InterruptAndJoinInputThread(std::thread& inputThread, wil::unique_event& exitEvent) | ||
| { | ||
| if (!inputThread.joinable()) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| WI_ASSERT(exitEvent); | ||
| exitEvent.SetEvent(); | ||
|
|
||
| // Overlapped IO will get terminated by SetEvent(). Synchronous IO will not, so we need to cancel it. | ||
| const auto threadHandle = static_cast<HANDLE>(inputThread.native_handle()); | ||
| DWORD waitResult = WAIT_TIMEOUT; | ||
| while (waitResult == WAIT_TIMEOUT) | ||
| { | ||
| if (!CancelSynchronousIo(threadHandle)) | ||
| { | ||
| // ERROR_NOT_FOUND just means nothing was in flight to cancel (expected); any other error | ||
| // means the cancel cannot make progress and would otherwise spin this loop, so throw. | ||
| const auto cancelError = GetLastError(); | ||
| THROW_WIN32_IF(cancelError, cancelError != ERROR_NOT_FOUND); | ||
| } | ||
|
|
||
| waitResult = WaitForSingleObject(threadHandle, 50); | ||
| } | ||
|
|
||
| // The loop only breaks on a non-timeout result; anything but WAIT_OBJECT_0 (e.g. WAIT_FAILED) is an | ||
| // error -- surface it instead of join()ing on a thread we can't confirm has exited. | ||
| THROW_LAST_ERROR_IF(waitResult != WAIT_OBJECT_0); | ||
|
|
||
| inputThread.join(); | ||
| } | ||
|
Comment on lines
+46
to
+66
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only real thing to do here is change to a failfast but its going to crash either way. It will not "mask the original failure" because the scope exit log will log the failure first, so this is really not a "high" priority issue, it's just a discussion about whether we want a failfast crash or an abort() crash w/ a log message. |
||
|
|
||
| } // namespace | ||
|
|
||
| bool ConsoleService::RelayInteractiveTty(wsl::windows::common::ConsoleState& Console, ClientRunningWSLCProcess& Process, HANDLE Tty, bool TriggerRefresh) | ||
| { | ||
| // Configure the console for interactive usage. | ||
|
|
@@ -43,13 +86,7 @@ bool ConsoleService::RelayInteractiveTty(wsl::windows::common::ConsoleState& Con | |
| wil::unique_event exitEvent; | ||
| std::thread inputThread; | ||
|
|
||
| auto joinThread = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&]() { | ||
| if (inputThread.joinable()) | ||
| { | ||
| exitEvent.SetEvent(); | ||
| inputThread.join(); | ||
| } | ||
| }); | ||
| auto joinThread = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&]() { InterruptAndJoinInputThread(inputThread, exitEvent); }); | ||
|
|
||
| bool detached = false; | ||
| MultiHandleWait io; | ||
|
|
@@ -99,13 +136,7 @@ void ConsoleService::RelayNonTtyProcess(wil::unique_handle&& Stdin, wil::unique_ | |
| wil::unique_event exitEvent; | ||
| std::thread inputThread; | ||
|
|
||
| auto joinThread = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&]() { | ||
| if (inputThread.joinable()) | ||
| { | ||
| exitEvent.SetEvent(); | ||
| inputThread.join(); | ||
| } | ||
| }); | ||
| auto joinThread = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&]() { InterruptAndJoinInputThread(inputThread, exitEvent); }); | ||
|
|
||
| if (Stdin.is_valid()) | ||
| { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.