From 46a6cc9c7cc0e29be47bbd0b40e2656c2212e75f Mon Sep 17 00:00:00 2001 From: manojampalam Date: Thu, 20 Oct 2016 22:14:42 -0700 Subject: [PATCH] Fixes to hang on TTY child process exit --- contrib/win32/win32compat/shell-host.c | 6 +++++- session.c | 8 ++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/contrib/win32/win32compat/shell-host.c b/contrib/win32/win32compat/shell-host.c index e9938db..f12e80a 100644 --- a/contrib/win32/win32compat/shell-host.c +++ b/contrib/win32/win32compat/shell-host.c @@ -1116,9 +1116,13 @@ int start_with_pty(int ac, wchar_t **av) { childProcessId = pi.dwProcessId; FreeConsole(); + Sleep(20); while (!AttachConsole(pi.dwProcessId)) { - Sleep(1000); + DWORD exit_code; + if (GetExitCodeProcess(pi.hProcess, &exit_code) && exit_code != STILL_ACTIVE) + break; + Sleep(100); } /* monitor child exist */ diff --git a/session.c b/session.c index c08daa9..d6741c6 100644 --- a/session.c +++ b/session.c @@ -567,7 +567,7 @@ int do_exec_windows(Session *s, const char *command, int pty) { free(tmp); if (s->display) - SetEnvironmentVariableW(L"DISPLAY", s->display); + SetEnvironmentVariableA("DISPLAY", s->display); //_wchdir(pw_dir_w); @@ -674,8 +674,12 @@ int do_exec_windows(Session *s, const char *command, int pty) { FreeConsole(); if (!debug_flag) ImpersonateLoggedOnUser(hToken); + Sleep(20); while (AttachConsole(pi.dwProcessId) == FALSE) { - Sleep(200); + DWORD exit_code; + if (GetExitCodeProcess(pi.hProcess, &exit_code) && exit_code != STILL_ACTIVE) + break; + Sleep(100); } if (!debug_flag) RevertToSelf();