port change from v2 to v1 on resetting timeout for debugger attached (#787)

This commit is contained in:
pan-wang 2018-05-01 15:50:51 -07:00 committed by GitHub
parent 3d2bf869c5
commit e36b0982bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 34 deletions

View File

@ -18,3 +18,4 @@
#define ASPNETCORE_EVENT_GRACEFUL_SHUTDOWN_FAILURE_MSG L"Failed to gracefully shutdown process '%d'." #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_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_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'"

View File

@ -65,6 +65,14 @@ public:
return m_fReady; return m_fReady;
} }
BOOL
IsDebuggerAttached(
VOID
)
{
return m_fDebuggerAttached;
}
VOID VOID
StopProcess( StopProcess(
VOID VOID
@ -278,6 +286,7 @@ private:
BOOL m_fWindowsAuthEnabled; BOOL m_fWindowsAuthEnabled;
BOOL m_fBasicAuthEnabled; BOOL m_fBasicAuthEnabled;
BOOL m_fAnonymousAuthEnabled; BOOL m_fAnonymousAuthEnabled;
BOOL m_fDebuggerAttached;
STTIMER m_Timer; STTIMER m_Timer;
SOCKET m_socket; SOCKET m_socket;

View File

@ -73,6 +73,12 @@ Language=English
%1 %1
. .
Messageid=1030
SymbolicName=ASPNETCORE_EVENT_PROCESS_SHUTDOWN
Language=English
%1
.
; ;
;#endif // _ASPNETCORE_MODULE_MSG_H_ ;#endif // _ASPNETCORE_MODULE_MSG_H_
; ;

View File

@ -883,6 +883,7 @@ FORWARDING_HANDLER::CreateWinHttpRequest(
HRESULT hr = S_OK; HRESULT hr = S_OK;
PCWSTR pszVersion = NULL; PCWSTR pszVersion = NULL;
PCSTR pszVerb; PCSTR pszVerb;
DWORD dwTimeout = INFINITE;
STACK_STRU(strVerb, 32); STACK_STRU(strVerb, 32);
// //
@ -923,11 +924,16 @@ FORWARDING_HANDLER::CreateWinHttpRequest(
goto Finished; goto Finished;
} }
if (!pServerProcess->IsDebuggerAttached())
{
dwTimeout = pProtocol->QueryTimeout();
}
if (!WinHttpSetTimeouts(m_hRequest, if (!WinHttpSetTimeouts(m_hRequest,
pProtocol->QueryTimeout(), dwTimeout, //resolve timeout
pProtocol->QueryTimeout(), dwTimeout, // connect timeout
pProtocol->QueryTimeout(), dwTimeout, // send timeout
pProtocol->QueryTimeout())) dwTimeout)) // receive timeout
{ {
hr = HRESULT_FROM_WIN32(GetLastError()); hr = HRESULT_FROM_WIN32(GetLastError());
goto Finished; goto Finished;

View File

@ -35,6 +35,7 @@ SERVER_PROCESS::Initialize(
m_fBasicAuthEnabled = fBasicAuthEnabled; m_fBasicAuthEnabled = fBasicAuthEnabled;
m_fAnonymousAuthEnabled = fAnonymousAuthEnabled; m_fAnonymousAuthEnabled = fAnonymousAuthEnabled;
m_pProcessManager->ReferenceProcessManager(); m_pProcessManager->ReferenceProcessManager();
m_fDebuggerAttached = FALSE;
if (FAILED (hr = m_ProcessPath.Copy(*pszProcessExePath)) || if (FAILED (hr = m_ProcessPath.Copy(*pszProcessExePath)) ||
FAILED (hr = m_struLogFile.Copy(*pstruStdoutLogFile))|| FAILED (hr = m_struLogFile.Copy(*pstruStdoutLogFile))||
@ -880,6 +881,7 @@ SERVER_PROCESS::PostStartCheck(
m_fReady = TRUE; m_fReady = TRUE;
Finished: Finished:
m_fDebuggerAttached = fDebuggerAttached;
return hr; return hr;
} }
@ -1328,10 +1330,10 @@ SERVER_PROCESS::CheckIfServerIsUp(
) )
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
DWORD dwResult = 0; DWORD dwResult = ERROR_INSUFFICIENT_BUFFER;
MIB_TCPTABLE_OWNER_PID *pTCPInfo = NULL; MIB_TCPTABLE_OWNER_PID *pTCPInfo = NULL;
MIB_TCPROW_OWNER_PID *pOwner = NULL; MIB_TCPROW_OWNER_PID *pOwner = NULL;
DWORD dwSize = 0; DWORD dwSize = 1000;
int iResult = 0; int iResult = 0;
SOCKADDR_IN sockAddr; SOCKADDR_IN sockAddr;
SOCKET socketCheck = INVALID_SOCKET; SOCKET socketCheck = INVALID_SOCKET;
@ -1347,36 +1349,36 @@ SERVER_PROCESS::CheckIfServerIsUp(
if (!g_fNsiApiNotSupported) if (!g_fNsiApiNotSupported)
{ {
dwResult = GetExtendedTcpTable(NULL, while (dwResult == ERROR_INSUFFICIENT_BUFFER)
&dwSize,
FALSE,
AF_INET,
TCP_TABLE_OWNER_PID_LISTENER,
0);
if (dwResult != NO_ERROR && dwResult != ERROR_INSUFFICIENT_BUFFER)
{ {
hr = HRESULT_FROM_WIN32(dwResult); // Increase the buffer size with additional space, MIB_TCPROW 20 bytes
goto Finished; // 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)
if (pTCPInfo == NULL) {
{ HeapFree(GetProcessHeap(), 0, pTCPInfo);
hr = E_OUTOFMEMORY; }
goto Finished;
}
dwResult = GetExtendedTcpTable(pTCPInfo, pTCPInfo = (MIB_TCPTABLE_OWNER_PID*)HeapAlloc(GetProcessHeap(), 0, dwSize);
&dwSize, if (pTCPInfo == NULL)
FALSE, {
AF_INET, hr = E_OUTOFMEMORY;
TCP_TABLE_OWNER_PID_LISTENER, goto Finished;
0); }
if (dwResult != NO_ERROR)
{ dwResult = GetExtendedTcpTable(pTCPInfo,
hr = HRESULT_FROM_WIN32(dwResult); &dwSize,
goto Finished; 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 // iterate pTcpInfo struct to find PID/PORT entry
@ -1421,6 +1423,12 @@ SERVER_PROCESS::CheckIfServerIsUp(
if (iResult == SOCKET_ERROR) if (iResult == SOCKET_ERROR)
{ {
hr = HRESULT_FROM_WIN32(WSAGetLastError()); 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; goto Finished;
} }
@ -1483,7 +1491,7 @@ SERVER_PROCESS::SendSignal(
goto Finished; 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); hr = HRESULT_FROM_WIN32(ERROR_TIMEOUT);
goto Finished; goto Finished;
@ -2065,12 +2073,36 @@ SERVER_PROCESS::HandleProcessExit()
HRESULT hr = S_OK; HRESULT hr = S_OK;
BOOL fReady = FALSE; BOOL fReady = FALSE;
DWORD dwProcessId = 0; DWORD dwProcessId = 0;
LPCWSTR apsz[1];
STACK_STRU(strEventMsg, 256);
if (InterlockedCompareExchange(&m_lStopping, 1L, 0L) == 0L) if (InterlockedCompareExchange(&m_lStopping, 1L, 0L) == 0L)
{ {
CheckIfServerIsUp(m_dwPort, &dwProcessId, &fReady); CheckIfServerIsUp(m_dwPort, &dwProcessId, &fReady);
if (!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); m_pProcessManager->ShutdownProcess(this);
} }