From e36b0982bfb5041e430dfc7b3d12e89570be9211 Mon Sep 17 00:00:00 2001 From: pan-wang Date: Tue, 1 May 2018 15:50:51 -0700 Subject: [PATCH] port change from v2 to v1 on resetting timeout for debugger attached (#787) --- .../AspNetCore/Inc/resource.h | 1 + .../AspNetCore/Inc/serverprocess.h | 9 ++ .../AspNetCore/aspnetcore_msg.mc | 6 ++ .../AspNetCore/src/forwardinghandler.cxx | 14 ++- .../AspNetCore/src/serverprocess.cxx | 92 +++++++++++++------ 5 files changed, 88 insertions(+), 34 deletions(-) diff --git a/src/AspNetCoreModuleV1/AspNetCore/Inc/resource.h b/src/AspNetCoreModuleV1/AspNetCore/Inc/resource.h index 64dde656b0..899e9a5c93 100644 --- a/src/AspNetCoreModuleV1/AspNetCore/Inc/resource.h +++ b/src/AspNetCoreModuleV1/AspNetCore/Inc/resource.h @@ -18,3 +18,4 @@ #define ASPNETCORE_EVENT_GRACEFUL_SHUTDOWN_FAILURE_MSG L"Failed to gracefully shutdown process '%d'." #define ASPNETCORE_EVENT_SENT_SHUTDOWN_HTTP_REQUEST_MSG L"Sent shutdown HTTP message to process '%d' and received http status '%d'." #define ASPNETCORE_EVENT_RECYCLE_APPOFFLINE_MSG L"App_offline file '%s' was detected." +#define ASPNETCORE_EVENT_PROCESS_SHUTDOWN_MSG L"Application '%s' with physical root '%s' shut down process with Id '%d' listening on port '%d'" diff --git a/src/AspNetCoreModuleV1/AspNetCore/Inc/serverprocess.h b/src/AspNetCoreModuleV1/AspNetCore/Inc/serverprocess.h index 4d6bf6c145..0a721c1544 100644 --- a/src/AspNetCoreModuleV1/AspNetCore/Inc/serverprocess.h +++ b/src/AspNetCoreModuleV1/AspNetCore/Inc/serverprocess.h @@ -65,6 +65,14 @@ public: return m_fReady; } + BOOL + IsDebuggerAttached( + VOID + ) + { + return m_fDebuggerAttached; + } + VOID StopProcess( VOID @@ -278,6 +286,7 @@ private: BOOL m_fWindowsAuthEnabled; BOOL m_fBasicAuthEnabled; BOOL m_fAnonymousAuthEnabled; + BOOL m_fDebuggerAttached; STTIMER m_Timer; SOCKET m_socket; diff --git a/src/AspNetCoreModuleV1/AspNetCore/aspnetcore_msg.mc b/src/AspNetCoreModuleV1/AspNetCore/aspnetcore_msg.mc index 50bf87b656..0bbc4fa1c2 100644 --- a/src/AspNetCoreModuleV1/AspNetCore/aspnetcore_msg.mc +++ b/src/AspNetCoreModuleV1/AspNetCore/aspnetcore_msg.mc @@ -73,6 +73,12 @@ Language=English %1 . +Messageid=1030 +SymbolicName=ASPNETCORE_EVENT_PROCESS_SHUTDOWN +Language=English +%1 +. + ; ;#endif // _ASPNETCORE_MODULE_MSG_H_ ; diff --git a/src/AspNetCoreModuleV1/AspNetCore/src/forwardinghandler.cxx b/src/AspNetCoreModuleV1/AspNetCore/src/forwardinghandler.cxx index f02a318f41..1665539f18 100644 --- a/src/AspNetCoreModuleV1/AspNetCore/src/forwardinghandler.cxx +++ b/src/AspNetCoreModuleV1/AspNetCore/src/forwardinghandler.cxx @@ -883,6 +883,7 @@ FORWARDING_HANDLER::CreateWinHttpRequest( HRESULT hr = S_OK; PCWSTR pszVersion = NULL; PCSTR pszVerb; + DWORD dwTimeout = INFINITE; STACK_STRU(strVerb, 32); // @@ -923,11 +924,16 @@ FORWARDING_HANDLER::CreateWinHttpRequest( goto Finished; } + if (!pServerProcess->IsDebuggerAttached()) + { + dwTimeout = pProtocol->QueryTimeout(); + } + if (!WinHttpSetTimeouts(m_hRequest, - pProtocol->QueryTimeout(), - pProtocol->QueryTimeout(), - pProtocol->QueryTimeout(), - pProtocol->QueryTimeout())) + dwTimeout, //resolve timeout + dwTimeout, // connect timeout + dwTimeout, // send timeout + dwTimeout)) // receive timeout { hr = HRESULT_FROM_WIN32(GetLastError()); goto Finished; diff --git a/src/AspNetCoreModuleV1/AspNetCore/src/serverprocess.cxx b/src/AspNetCoreModuleV1/AspNetCore/src/serverprocess.cxx index 006fc1dcb8..8b5e6f47e2 100644 --- a/src/AspNetCoreModuleV1/AspNetCore/src/serverprocess.cxx +++ b/src/AspNetCoreModuleV1/AspNetCore/src/serverprocess.cxx @@ -35,6 +35,7 @@ SERVER_PROCESS::Initialize( m_fBasicAuthEnabled = fBasicAuthEnabled; m_fAnonymousAuthEnabled = fAnonymousAuthEnabled; m_pProcessManager->ReferenceProcessManager(); + m_fDebuggerAttached = FALSE; if (FAILED (hr = m_ProcessPath.Copy(*pszProcessExePath)) || FAILED (hr = m_struLogFile.Copy(*pstruStdoutLogFile))|| @@ -880,6 +881,7 @@ SERVER_PROCESS::PostStartCheck( m_fReady = TRUE; Finished: + m_fDebuggerAttached = fDebuggerAttached; return hr; } @@ -1328,10 +1330,10 @@ SERVER_PROCESS::CheckIfServerIsUp( ) { HRESULT hr = S_OK; - DWORD dwResult = 0; + DWORD dwResult = ERROR_INSUFFICIENT_BUFFER; MIB_TCPTABLE_OWNER_PID *pTCPInfo = NULL; MIB_TCPROW_OWNER_PID *pOwner = NULL; - DWORD dwSize = 0; + DWORD dwSize = 1000; int iResult = 0; SOCKADDR_IN sockAddr; SOCKET socketCheck = INVALID_SOCKET; @@ -1347,36 +1349,36 @@ SERVER_PROCESS::CheckIfServerIsUp( if (!g_fNsiApiNotSupported) { - dwResult = GetExtendedTcpTable(NULL, - &dwSize, - FALSE, - AF_INET, - TCP_TABLE_OWNER_PID_LISTENER, - 0); - - if (dwResult != NO_ERROR && dwResult != ERROR_INSUFFICIENT_BUFFER) + while (dwResult == ERROR_INSUFFICIENT_BUFFER) { - hr = HRESULT_FROM_WIN32(dwResult); - goto Finished; - } + // Increase the buffer size with additional space, MIB_TCPROW 20 bytes + // New entries may be added by other processes before calling GetExtendedTcpTable + dwSize += 200; - pTCPInfo = (MIB_TCPTABLE_OWNER_PID*)HeapAlloc(GetProcessHeap(), 0, dwSize); - if (pTCPInfo == NULL) - { - hr = E_OUTOFMEMORY; - goto Finished; - } + if (pTCPInfo != NULL) + { + HeapFree(GetProcessHeap(), 0, pTCPInfo); + } - dwResult = GetExtendedTcpTable(pTCPInfo, - &dwSize, - FALSE, - AF_INET, - TCP_TABLE_OWNER_PID_LISTENER, - 0); - if (dwResult != NO_ERROR) - { - hr = HRESULT_FROM_WIN32(dwResult); - goto Finished; + pTCPInfo = (MIB_TCPTABLE_OWNER_PID*)HeapAlloc(GetProcessHeap(), 0, dwSize); + if (pTCPInfo == NULL) + { + hr = E_OUTOFMEMORY; + goto Finished; + } + + dwResult = GetExtendedTcpTable(pTCPInfo, + &dwSize, + FALSE, + AF_INET, + TCP_TABLE_OWNER_PID_LISTENER, + 0); + + if (dwResult != NO_ERROR && dwResult != ERROR_INSUFFICIENT_BUFFER) + { + hr = HRESULT_FROM_WIN32(dwResult); + goto Finished; + } } // iterate pTcpInfo struct to find PID/PORT entry @@ -1421,6 +1423,12 @@ SERVER_PROCESS::CheckIfServerIsUp( if (iResult == SOCKET_ERROR) { hr = HRESULT_FROM_WIN32(WSAGetLastError()); + if (hr == HRESULT_FROM_WIN32(WSAECONNREFUSED)) + { + // WSAECONNREFUSED means no application listen on the given port. + // This is not a failure. Reset the hresult to S_OK and return fReady to false + hr = S_OK; + } goto Finished; } @@ -1483,7 +1491,7 @@ SERVER_PROCESS::SendSignal( goto Finished; } - if (WaitForSingleObject(m_hShutdownHandle, m_dwShutdownTimeLimitInMS) != WAIT_OBJECT_0) + if (WaitForSingleObject(m_hShutdownHandle, m_fDebuggerAttached ? INFINITE : m_dwShutdownTimeLimitInMS) != WAIT_OBJECT_0) { hr = HRESULT_FROM_WIN32(ERROR_TIMEOUT); goto Finished; @@ -2065,12 +2073,36 @@ SERVER_PROCESS::HandleProcessExit() HRESULT hr = S_OK; BOOL fReady = FALSE; DWORD dwProcessId = 0; + LPCWSTR apsz[1]; + STACK_STRU(strEventMsg, 256); + if (InterlockedCompareExchange(&m_lStopping, 1L, 0L) == 0L) { CheckIfServerIsUp(m_dwPort, &dwProcessId, &fReady); if (!fReady) { + if (SUCCEEDED(strEventMsg.SafeSnwprintf( + ASPNETCORE_EVENT_PROCESS_SHUTDOWN_MSG, + m_struAppFullPath.QueryStr(), + m_pszRootApplicationPath.QueryStr(), + m_dwProcessId, + m_dwPort))) + { + apsz[0] = strEventMsg.QueryStr(); + if (FORWARDING_HANDLER::QueryEventLog() != NULL) + { + ReportEventW(FORWARDING_HANDLER::QueryEventLog(), + EVENTLOG_INFORMATION_TYPE, + 0, + ASPNETCORE_EVENT_PROCESS_SHUTDOWN, + NULL, + 1, + 0, + apsz, + NULL); + } + } m_pProcessManager->ShutdownProcess(this); }