Skip to content

Commit 18884b2

Browse files
NoMoreFoodmanojampalam
authored andcommitted
Addressed Thread Hanging Issue (#374)
- Replaced TerminateThread() call with an interrupt routine to gracefully call _endthreadex(0). - Resolves PowerShell/Win32-OpenSSH#1338.
1 parent 8d7ab2b commit 18884b2

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

contrib/win32/win32compat/termio.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ syncio_initiate_write(struct w32_io* pio, DWORD num_bytes)
244244
return 0;
245245
}
246246

247+
static VOID CALLBACK
248+
InterruptThread(_In_ ULONG_PTR dwParam)
249+
{
250+
_endthreadex(0);
251+
}
252+
247253
/* close */
248254
int
249255
syncio_close(struct w32_io* pio)
@@ -258,10 +264,12 @@ syncio_close(struct w32_io* pio)
258264
1. For console - the read thread is blocked by the while loop on raw mode
259265
2. Function ReadFile on Win7 machine dees not return when no content to read in non-interactive mode.
260266
*/
261-
if (FILETYPE(pio) == FILE_TYPE_CHAR && (IsWin7OrLess() || in_raw_mode))
262-
TerminateThread(pio->read_overlapped.hEvent, 0);
263-
else
264-
WaitForSingleObject(pio->read_overlapped.hEvent, INFINITE);
267+
if (FILETYPE(pio) == FILE_TYPE_CHAR && (IsWin7OrLess() || in_raw_mode)) {
268+
QueueUserAPC(InterruptThread, pio->read_overlapped.hEvent, (ULONG_PTR)NULL);
269+
CancelSynchronousIo(pio->read_overlapped.hEvent);
270+
}
271+
272+
WaitForSingleObject(pio->read_overlapped.hEvent, INFINITE);
265273
}
266274
if (pio->write_details.pending)
267275
WaitForSingleObject(pio->write_overlapped.hEvent, INFINITE);

contrib/win32/win32compat/tncon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ NetWriteString2(SOCKET sock, char* source, size_t len, int options)
9696
BOOL
9797
DataAvailable(HANDLE h)
9898
{
99-
DWORD dwRet = WaitForSingleObject(h, INFINITE);
99+
DWORD dwRet = WaitForSingleObjectEx(h, INFINITE, TRUE);
100100
if (dwRet == WAIT_OBJECT_0)
101101
return TRUE;
102102
if (dwRet == WAIT_FAILED)

0 commit comments

Comments
 (0)