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_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'"

View File

@ -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;

View File

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

View File

@ -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;

View File

@ -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);
}