Skip to content

Commit 8c53287

Browse files
committed
env var
1 parent 9e10362 commit 8c53287

File tree

7 files changed

+46
-33
lines changed

7 files changed

+46
-33
lines changed

src/Servers/IIS/AspNetCoreModuleV2/AspNetCore/ShimOptions.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define CS_ASPNETCORE_CLEAN_SHADOW_DIRECTORY_CONTENT L"cleanShadowCopyDirectory"
1414
#define CS_ASPNETCORE_DISALLOW_ROTATE_CONFIG L"disallowRotationOnConfigChange"
1515
#define CS_ASPNETCORE_SHUTDOWN_DELAY L"shutdownDelay"
16+
#define CS_ASPNEtCORE_SHUTDOWN_DELAY_ENV L"ANCM_shutdownDelay"
1617

1718
ShimOptions::ShimOptions(const ConfigurationSource &configurationSource) :
1819
m_hostingModel(HOSTING_UNKNOWN),
@@ -55,17 +56,6 @@ ShimOptions::ShimOptions(const ConfigurationSource &configurationSource) :
5556
auto disallowRotationOnConfigChange = find_element(handlerSettings, CS_ASPNETCORE_DISALLOW_ROTATE_CONFIG).value_or(std::wstring());
5657
m_fDisallowRotationOnConfigChange = equals_ignore_case(L"true", disallowRotationOnConfigChange);
5758

58-
auto shutdownDelay = find_element(handlerSettings, CS_ASPNETCORE_SHUTDOWN_DELAY).value_or(std::wstring());
59-
if (shutdownDelay.empty())
60-
{
61-
m_fShutdownDelay = std::chrono::seconds(1);
62-
}
63-
else
64-
{
65-
auto str = to_multi_byte_string(shutdownDelay, CP_UTF8);
66-
m_fShutdownDelay = std::chrono::milliseconds(std::atoi(str.c_str()));
67-
}
68-
6959
m_strProcessPath = section->GetRequiredString(CS_ASPNETCORE_PROCESS_EXE_PATH);
7060
m_strArguments = section->GetString(CS_ASPNETCORE_PROCESS_ARGUMENTS).value_or(CS_ASPNETCORE_PROCESS_ARGUMENTS_DEFAULT);
7161
m_fStdoutLogEnabled = section->GetRequiredBool(CS_ASPNETCORE_STDOUT_LOG_ENABLED);
@@ -94,4 +84,33 @@ ShimOptions::ShimOptions(const ConfigurationSource &configurationSource) :
9484
auto dotnetEnvironmentEnabled = equals_ignore_case(L"Development", dotnetEnvironment);
9585

9686
m_fShowDetailedErrors = detailedErrorsEnabled || aspnetCoreEnvironmentEnabled || dotnetEnvironmentEnabled;
87+
88+
// Specifies how long to delay (in milliseconds) after IIS tells us to stop before starting the application shutdown.
89+
// See StartShutdown in globalmodule to see how it's used.
90+
auto shutdownDelay = find_element(handlerSettings, CS_ASPNETCORE_SHUTDOWN_DELAY).value_or(std::wstring());
91+
if (shutdownDelay.empty())
92+
{
93+
// Fallback to environment variable if process specific config wasn't set
94+
shutdownDelay = Environment::GetEnvironmentVariableValue(CS_ASPNEtCORE_SHUTDOWN_DELAY_ENV)
95+
.value_or(environmentVariables[CS_ASPNEtCORE_SHUTDOWN_DELAY_ENV]);
96+
if (shutdownDelay.empty())
97+
{
98+
// Default if neither process specific config or environment variable aren't set
99+
m_fShutdownDelay = std::chrono::seconds(1);
100+
}
101+
else
102+
{
103+
SetShutdownDelay(shutdownDelay);
104+
}
105+
}
106+
else
107+
{
108+
SetShutdownDelay(shutdownDelay);
109+
}
110+
}
111+
112+
void ShimOptions::SetShutdownDelay(std::wstring& shutdownDelay)
113+
{
114+
auto str = to_multi_byte_string(shutdownDelay, CP_UTF8);
115+
m_fShutdownDelay = std::chrono::milliseconds(std::atoi(str.c_str()));
97116
}

src/Servers/IIS/AspNetCoreModuleV2/AspNetCore/ShimOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,6 @@ class ShimOptions: NonCopyable
111111
bool m_fDisallowRotationOnConfigChange;
112112
std::wstring m_strShadowCopyingDirectory;
113113
std::chrono::milliseconds m_fShutdownDelay;
114+
115+
void SetShutdownDelay(std::wstring& shutdownDelay);
114116
};

src/Servers/IIS/AspNetCoreModuleV2/AspNetCore/dllmain.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,24 +126,13 @@ HRESULT
126126
RQ_EXECUTE_REQUEST_HANDLER,
127127
0));
128128

129-
//auto delay = applicationManager->GetShutdownDelay();
130129
auto pGlobalModule = std::make_unique<ASPNET_CORE_GLOBAL_MODULE>(std::move(applicationManager));
131130

132-
//if (delay == std::chrono::milliseconds::zero())
133-
//{
134-
// RETURN_IF_FAILED(pModuleInfo->SetGlobalNotifications(
135-
// pGlobalModule.release(),
136-
// GL_CONFIGURATION_CHANGE | // Configuration change triggers IIS application stop
137-
// GL_STOP_LISTENING)); // worker process stop
138-
//}
139-
//else
140-
{
141-
RETURN_IF_FAILED(pModuleInfo->SetGlobalNotifications(
142-
pGlobalModule.release(),
143-
GL_CONFIGURATION_CHANGE | // Configuration change triggers IIS application stop
144-
GL_STOP_LISTENING | // worker process stop
145-
GL_APPLICATION_STOP)); // app pool recycle
146-
}
131+
RETURN_IF_FAILED(pModuleInfo->SetGlobalNotifications(
132+
pGlobalModule.release(),
133+
GL_CONFIGURATION_CHANGE | // Configuration change triggers IIS application stop
134+
GL_STOP_LISTENING | // worker process will stop listening for http requests
135+
GL_APPLICATION_STOP)); // app pool recycle or stop
147136

148137
return S_OK;
149138
}

src/Servers/IIS/AspNetCoreModuleV2/AspNetCore/globalmodule.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ ASPNET_CORE_GLOBAL_MODULE::OnGlobalApplicationStop(
4040
IN IHttpApplicationStopProvider* pProvider
4141
)
4242
{
43+
UNREFERENCED_PARAMETER(pProvider);
44+
45+
// If we're already cleaned up just return.
46+
// If user has opted out of the new shutdown behavior ignore this call as we never registered for it before
4347
if (!m_pApplicationManager || m_pApplicationManager->GetShutdownDelay() == std::chrono::milliseconds::zero())
4448
{
4549
return GL_NOTIFICATION_CONTINUE;
4650
}
4751

48-
UNREFERENCED_PARAMETER(pProvider);
4952
LOG_INFO(L"ASPNET_CORE_GLOBAL_MODULE::OnGlobalApplicationStop");
5053

5154
if (!g_fInShutdown && !m_shutdown.joinable())

src/Servers/IIS/AspNetCoreModuleV2/AspNetCore/globalmodule.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,17 @@ class ASPNET_CORE_GLOBAL_MODULE : NonCopyable, public CGlobalModule
6868
else
6969
{
7070
// Run shutdown on a background thread. It seems like IIS keeps giving us requests if OnGlobalStopListening is still running
71-
// which will result in 503s since we're shutting down.
72-
// But if we return ASAP from OnGlobalStopListening (by not shutting down inline),
71+
// which will result in 503s from applicationmanager since we're shutting down and don't want to process new requests.
72+
// But if we return ASAP from OnGlobalStopListening, by not shutting down inline and with a small delay to reduce races,
7373
// IIS will actually stop giving us new requests and queue them instead for processing by the new app process.
7474
m_shutdown = std::thread([this]()
7575
{
7676
auto delay = m_pApplicationManager->GetShutdownDelay();
7777
LOG_INFOF(L"Shutdown starting in %d ms.", delay.count());
7878
// Delay so that any incoming requests while we're returning from OnGlobalStopListening are allowed to be processed
7979
std::this_thread::sleep_for(delay);
80+
81+
LOG_INFO(L"Shutdown starting.");
8082
m_pApplicationManager->ShutDown();
8183
m_pApplicationManager = nullptr;
8284
});

src/Servers/IIS/AspNetCoreModuleV2/AspNetCore/proxymodule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ ASPNET_CORE_PROXY_MODULE::OnExecuteRequestHandler(
9393
{
9494
if (g_fInShutdown)
9595
{
96-
LOG_INFO(L"Received request during shutdown. Will return 503.");
96+
LOG_INFO(L"Received a request during shutdown. Will return a 503 response.");
9797
FINISHED(HRESULT_FROM_WIN32(ERROR_SERVER_SHUTDOWN_IN_PROGRESS));
9898
}
9999

src/Servers/IIS/IIS/test/Common.LongTests/ShutdownTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ await statusConnection.Receive("5",
262262

263263
[ConditionalFact]
264264
[RequiresNewShim]
265-
[Repeat(10)] // temp for CI testing
266265
public async Task RequestsWhileRestartingAppFromConfigChangeAreProcessed()
267266
{
268267
var deploymentParameters = Fixture.GetBaseDeploymentParameters(Fixture.InProcessTestSite);
@@ -300,7 +299,6 @@ public async Task RequestsWhileRestartingAppFromConfigChangeAreProcessed()
300299

301300
[ConditionalFact]
302301
[RequiresNewShim]
303-
[Repeat(10)] // temp for CI testing
304302
public async Task RequestsWhileRecyclingAppAreProcessed()
305303
{
306304
var deploymentParameters = Fixture.GetBaseDeploymentParameters(Fixture.InProcessTestSite);

0 commit comments

Comments
 (0)