Skip to content

Commit f94332e

Browse files
Clean up more of SERVER_PROCESS (#50015)
1 parent d9a1cf0 commit f94332e

File tree

2 files changed

+59
-71
lines changed

2 files changed

+59
-71
lines changed

src/Servers/IIS/AspNetCoreModuleV2/OutOfProcessRequestHandler/serverprocess.cpp

Lines changed: 59 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88
#include "file_utility.h"
99
#include "exceptions.h"
1010

11-
#define STARTUP_TIME_LIMIT_INCREMENT_IN_MILLISECONDS 5000
12-
1311
HRESULT
1412
SERVER_PROCESS::Initialize(
1513
PROCESS_MANAGER *pProcessManager,
1614
STRU *pszProcessExePath,
1715
STRU *pszArguments,
1816
DWORD dwStartupTimeLimitInMS,
19-
DWORD dwShtudownTimeLimitInMS,
17+
DWORD dwShutdownTimeLimitInMS,
2018
BOOL fWindowsAuthEnabled,
2119
BOOL fBasicAuthEnabled,
2220
BOOL fAnonymousAuthEnabled,
@@ -31,11 +29,9 @@ SERVER_PROCESS::Initialize(
3129
STRU *pszHttpsPort
3230
)
3331
{
34-
HRESULT hr = S_OK;
35-
3632
m_pProcessManager = pProcessManager;
3733
m_dwStartupTimeLimitInMS = dwStartupTimeLimitInMS;
38-
m_dwShutdownTimeLimitInMS = dwShtudownTimeLimitInMS;
34+
m_dwShutdownTimeLimitInMS = dwShutdownTimeLimitInMS;
3935
m_fStdoutLogEnabled = fStdoutLogEnabled;
4036
m_fWebSocketSupported = fWebSocketSupported;
4137
m_fWindowsAuthEnabled = fWindowsAuthEnabled;
@@ -45,6 +41,7 @@ SERVER_PROCESS::Initialize(
4541
m_pProcessManager->ReferenceProcessManager();
4642
m_fDebuggerAttached = FALSE;
4743

44+
HRESULT hr;
4845
if (FAILED_LOG(hr = m_ProcessPath.Copy(*pszProcessExePath)) ||
4946
FAILED_LOG(hr = m_struLogFile.Copy(*pstruStdoutLogFile))||
5047
FAILED_LOG(hr = m_struPhysicalPath.Copy(*pszAppPhysicalPath))||
@@ -54,49 +51,44 @@ SERVER_PROCESS::Initialize(
5451
FAILED_LOG(hr = m_struHttpsPort.Copy(*pszHttpsPort)) ||
5552
FAILED_LOG(hr = SetupJobObject()))
5653
{
57-
goto Finished;
54+
return hr;
5855
}
5956

6057
m_pEnvironmentVarTable = pEnvironmentVariables;
6158

62-
Finished:
63-
return hr;
59+
return S_OK;
6460
}
6561

6662
HRESULT
6763
SERVER_PROCESS::SetupJobObject(VOID)
6864
{
69-
HRESULT hr = S_OK;
70-
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobInfo = { 0 };
65+
if (m_hJobObject != nullptr)
66+
{
67+
return S_OK;
68+
}
69+
70+
m_hJobObject = CreateJobObject(nullptr /* lpJobAttributes */, nullptr /* lpName */);
7171

72-
if (m_hJobObject == NULL)
72+
// 0xdeadbeef is used by Antares
73+
constexpr size_t magicAntaresNumber = 0xdeadbeef;
74+
if (m_hJobObject == nullptr || m_hJobObject == reinterpret_cast<HANDLE>(magicAntaresNumber))
7375
{
74-
m_hJobObject = CreateJobObject(NULL, // LPSECURITY_ATTRIBUTES
75-
NULL); // LPCTSTR lpName
76-
#pragma warning( disable : 4312)
77-
// 0xdeadbeef is used by Antares
78-
if (m_hJobObject == NULL || m_hJobObject == (HANDLE)0xdeadbeef)
79-
{
80-
m_hJobObject = NULL;
81-
// ignore job object creation error.
82-
}
83-
#pragma warning( error : 4312)
84-
if (m_hJobObject != NULL)
85-
{
86-
jobInfo.BasicLimitInformation.LimitFlags =
87-
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
76+
m_hJobObject = nullptr;
8877

89-
if (!SetInformationJobObject(m_hJobObject,
90-
JobObjectExtendedLimitInformation,
91-
&jobInfo,
92-
sizeof jobInfo))
93-
{
94-
hr = HRESULT_FROM_WIN32(GetLastError());
95-
}
96-
}
78+
// ignore job object creation error.
79+
return S_OK;
9780
}
9881

99-
return hr;
82+
// Created a job object successfully. Set the job object limit.
83+
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobInfo = { 0 };
84+
jobInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
85+
86+
if (!SetInformationJobObject(m_hJobObject, JobObjectExtendedLimitInformation, &jobInfo, sizeof jobInfo))
87+
{
88+
return HRESULT_FROM_WIN32(GetLastError());
89+
}
90+
91+
return S_OK;
10092
}
10193

10294
HRESULT
@@ -106,29 +98,34 @@ SERVER_PROCESS::GetRandomPort
10698
DWORD dwExcludedPort = 0
10799
)
108100
{
109-
HRESULT hr = S_OK;
110-
BOOL fPortInUse = FALSE;
111-
DWORD dwActualProcessId = 0;
101+
DBG_ASSERT(pdwPickedPort);
112102

113103
std::uniform_int_distribution<> dist(MIN_PORT_RANDOM, MAX_PORT);
114-
DWORD cRetry = 0;
115-
do
116-
{
117-
//
118-
// ignore dwActualProcessId because here we are
119-
// determing whether the randomly generated port is
120-
// in use by any other process.
121-
//
122-
while ((*pdwPickedPort = dist(m_randomGenerator)) == dwExcludedPort);
123-
hr = CheckIfServerIsUp(*pdwPickedPort, &dwActualProcessId, &fPortInUse);
124-
} while (fPortInUse && ++cRetry < MAX_RETRY);
125104

126-
if (cRetry >= MAX_RETRY)
105+
BOOL fPortInUse;
106+
DWORD dwActualProcessId; // Ignored, but required for the function call.
107+
constexpr int maxRetries = 10;
108+
for (int retry = 0; retry < maxRetries; ++retry)
127109
{
128-
hr = HRESULT_FROM_WIN32(ERROR_PORT_NOT_SET);
110+
do
111+
{
112+
*pdwPickedPort = dist(m_randomGenerator);
113+
} while (*pdwPickedPort == dwExcludedPort); // Keep generating until a valid port is found.
114+
115+
HRESULT hr = CheckIfServerIsUp(*pdwPickedPort, &dwActualProcessId, &fPortInUse);
116+
if (FAILED(hr))
117+
{
118+
return hr;
119+
}
120+
121+
if (!fPortInUse)
122+
{
123+
return S_OK; // Port found and is not in use, success!
124+
}
129125
}
130126

131-
return hr;
127+
// All retries failed, return error.
128+
return HRESULT_FROM_WIN32(ERROR_PORT_NOT_SET);
132129
}
133130

134131
HRESULT
@@ -219,40 +216,32 @@ SERVER_PROCESS::SetupListenPort(
219216

220217
HRESULT
221218
SERVER_PROCESS::SetupAppPath(
222-
ENVIRONMENT_VAR_HASH* pEnvironmentVarTable
219+
ENVIRONMENT_VAR_HASH* pEnvironmentVarTable
223220
)
224221
{
225-
HRESULT hr = S_OK;
226-
ENVIRONMENT_VAR_ENTRY* pEntry = NULL;
227-
222+
ENVIRONMENT_VAR_ENTRY* pEntry = nullptr;
228223
pEnvironmentVarTable->FindKey(ASPNETCORE_APP_PATH_ENV_STR, &pEntry);
229-
if (pEntry != NULL)
224+
if (pEntry != nullptr)
230225
{
231226
// user should not set this environment variable in configuration
232227
pEnvironmentVarTable->DeleteKey(ASPNETCORE_APP_PATH_ENV_STR);
233228
pEntry->Dereference();
234-
pEntry = NULL;
229+
pEntry = nullptr;
235230
}
236231

237232
pEntry = new ENVIRONMENT_VAR_ENTRY();
238-
if (pEntry == NULL)
233+
if (pEntry == nullptr)
239234
{
240-
hr = E_OUTOFMEMORY;
241-
goto Finished;
235+
return E_OUTOFMEMORY;
242236
}
243237

244-
if (FAILED_LOG(hr = pEntry->Initialize(ASPNETCORE_APP_PATH_ENV_STR, m_struAppVirtualPath.QueryStr())) ||
245-
FAILED_LOG(hr = pEnvironmentVarTable->InsertRecord(pEntry)))
238+
HRESULT hr = S_OK;
239+
if (SUCCEEDED_LOG(hr = pEntry->Initialize(ASPNETCORE_APP_PATH_ENV_STR, m_struAppVirtualPath.QueryStr())))
246240
{
247-
goto Finished;
241+
LOG_IF_FAILED(hr = pEnvironmentVarTable->InsertRecord(pEntry));
248242
}
249243

250-
Finished:
251-
if (pEntry != NULL)
252-
{
253-
pEntry->Dereference();
254-
pEntry = NULL;
255-
}
244+
pEntry->Dereference();
256245
return hr;
257246
}
258247

src/Servers/IIS/AspNetCoreModuleV2/OutOfProcessRequestHandler/serverprocess.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// to reduce the chance of collisions.
1717
#define MIN_PORT_RANDOM 10000
1818
#define MAX_PORT 48000
19-
#define MAX_RETRY 10
2019
#define MAX_ACTIVE_CHILD_PROCESSES 16
2120
#define PIPE_OUTPUT_THREAD_TIMEOUT 2000
2221
#define LOCALHOST "127.0.0.1"

0 commit comments

Comments
 (0)