From 87f808cc9d6904f01da2f97996203a243ccb460c Mon Sep 17 00:00:00 2001 From: pan-wang Date: Tue, 17 Jan 2017 13:43:37 -0800 Subject: [PATCH] fixing the AV for graceful shutdown on Win7, set forwardWindowsAuthToken default to true and other minor issues --- src/AspNetCore/Inc/applicationmanager.h | 2 +- src/AspNetCore/Inc/serverprocess.h | 1 - src/AspNetCore/Src/processmanager.cxx | 1 - src/AspNetCore/Src/serverprocess.cxx | 83 +++++++++++++------------ src/AspNetCore/aspnetcore_schema.xml | 2 +- 5 files changed, 44 insertions(+), 45 deletions(-) diff --git a/src/AspNetCore/Inc/applicationmanager.h b/src/AspNetCore/Inc/applicationmanager.h index ee2b0542dc..c3767ec076 100644 --- a/src/AspNetCore/Inc/applicationmanager.h +++ b/src/AspNetCore/Inc/applicationmanager.h @@ -141,7 +141,7 @@ private:
  • Enable logging the application process' stdout messages
  • \
  • Attach a debugger to the application process and inspect
  • \

    For more information visit: \ - http://go.microsoft.com/fwlink/?LinkID=808681

    \ + https://go.microsoft.com/fwlink/?LinkID=808681 \
    \ \ ") diff --git a/src/AspNetCore/Inc/serverprocess.h b/src/AspNetCore/Inc/serverprocess.h index 487f1767ea..1c45602082 100644 --- a/src/AspNetCore/Inc/serverprocess.h +++ b/src/AspNetCore/Inc/serverprocess.h @@ -237,7 +237,6 @@ private: DWORD m_dwProcessId; DWORD m_dwListeningProcessId; STRA m_straGuid; - HANDLE m_CancelEvent; // // m_hProcessHandle is the handle to process this object creates. diff --git a/src/AspNetCore/Src/processmanager.cxx b/src/AspNetCore/Src/processmanager.cxx index 35fd35d6e4..26f96becc0 100644 --- a/src/AspNetCore/Src/processmanager.cxx +++ b/src/AspNetCore/Src/processmanager.cxx @@ -232,7 +232,6 @@ PROCESS_MANAGER::GetProcess( goto Finished; } - this->ReferenceProcessManager(); hr = m_ppServerProcessList[dwProcessIndex]->Initialize( this, pConfig->QueryProcessPath(), diff --git a/src/AspNetCore/Src/serverprocess.cxx b/src/AspNetCore/Src/serverprocess.cxx index 28e6ded07c..6b1e0f4af6 100644 --- a/src/AspNetCore/Src/serverprocess.cxx +++ b/src/AspNetCore/Src/serverprocess.cxx @@ -29,6 +29,8 @@ SERVER_PROCESS::Initialize( m_dwShutdownTimeLimitInMS = dwShtudownTimeLimitInMS; m_fStdoutLogEnabled = fStdoutLogEnabled; + m_pProcessManager->ReferenceProcessManager(); + hr = m_ProcessPath.Copy(*pszProcessExePath); if (FAILED(hr)) { @@ -1208,7 +1210,7 @@ Finished: return hr; } -// send ctrl-c signnal to the process to let it graceful shutdown +// send ctrl-c signnal to the process to let it gracefully shutdown // if the process cannot shutdown within given time, terminate it // todo: allow user to config this shutdown timeout @@ -1217,7 +1219,7 @@ SERVER_PROCESS::SendSignal( VOID ) { - HANDLE hProc; + HANDLE hProc = INVALID_HANDLE_VALUE; BOOL fIsSuccess = FALSE; LPCWSTR apsz[1]; STACK_STRU(strEventMsg, 256); @@ -1225,54 +1227,61 @@ SERVER_PROCESS::SendSignal( hProc = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE, FALSE, m_dwProcessId); if (hProc != INVALID_HANDLE_VALUE) { - fIsSuccess = GenerateConsoleCtrlEvent(CTRL_C_EVENT, m_dwProcessId); + fIsSuccess = GenerateConsoleCtrlEvent(CTRL_C_EVENT, m_dwProcessId); if (!fIsSuccess) { if (AttachConsole(m_dwProcessId)) { - fIsSuccess = GenerateConsoleCtrlEvent(CTRL_C_EVENT, m_dwProcessId); + fIsSuccess = GenerateConsoleCtrlEvent(CTRL_C_EVENT, m_dwProcessId); FreeConsole(); - CloseHandle(m_hProcessHandle); - m_hProcessHandle = INVALID_HANDLE_VALUE; - } - - if (!fIsSuccess) - { - if (SUCCEEDED(strEventMsg.SafeSnwprintf( - ASPNETCORE_EVENT_GRACEFUL_SHUTDOWN_FAILURE_MSG, - m_dwProcessId ))) - { - apsz[0] = strEventMsg.QueryStr(); - // log a warning for ungraceful shutdown - if (FORWARDING_HANDLER::QueryEventLog() != NULL) - { - ReportEventW(FORWARDING_HANDLER::QueryEventLog(), - EVENTLOG_INFORMATION_TYPE, - 0, - ASPNETCORE_EVENT_GRACEFUL_SHUTDOWN_FAILURE, - NULL, - 1, - 0, - apsz, - NULL); - } - } } + } + } + + if (!fIsSuccess || (WaitForSingleObject(hProc, m_dwShutdownTimeLimitInMS) != WAIT_OBJECT_0)) + { + //Backend process will be terminated, remove the waitcallback + if (m_hProcessWaitHandle != NULL) + { + UnregisterWait(m_hProcessWaitHandle); + m_hProcessWaitHandle = NULL; } - if (!fIsSuccess || (WaitForSingleObject(hProc, m_dwShutdownTimeLimitInMS) != WAIT_OBJECT_0)) + // cannot gracefully shutdown or timeout, terminate the process + TerminateProcess(m_hProcessHandle, 0); + + // log a warning for ungraceful shutdown + if (SUCCEEDED(strEventMsg.SafeSnwprintf( + ASPNETCORE_EVENT_GRACEFUL_SHUTDOWN_FAILURE_MSG, + m_dwProcessId))) { - // cannot gracefule shutdown or timeout - // terminate the process - TerminateProcess(m_hProcessHandle, 0); + apsz[0] = strEventMsg.QueryStr(); + if (FORWARDING_HANDLER::QueryEventLog() != NULL) + { + ReportEventW(FORWARDING_HANDLER::QueryEventLog(), + EVENTLOG_WARNING_TYPE, + 0, + ASPNETCORE_EVENT_GRACEFUL_SHUTDOWN_FAILURE, + NULL, + 1, + 0, + apsz, + NULL); + } } } + if (hProc != INVALID_HANDLE_VALUE) { CloseHandle(hProc); hProc = INVALID_HANDLE_VALUE; } + if (m_hProcessHandle != INVALID_HANDLE_VALUE) + { + CloseHandle(m_hProcessHandle); + m_hProcessHandle = INVALID_HANDLE_VALUE; + } } // @@ -1640,7 +1649,6 @@ Finished: SERVER_PROCESS::SERVER_PROCESS() : m_cRefs( 1 ), - m_CancelEvent( NULL ), m_hProcessHandle( NULL ), m_hProcessWaitHandle( NULL ), m_dwProcessId( 0 ), @@ -1681,13 +1689,6 @@ SERVER_PROCESS::~SERVER_PROCESS() m_hProcessWaitHandle = NULL; } - if( m_CancelEvent != NULL ) - { - SetEvent( m_CancelEvent ); - CloseHandle( m_CancelEvent ); - m_CancelEvent = NULL; - } - for(INT i=0;i - +