Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanConroy committed Feb 4, 2025
1 parent bb29868 commit f7fbf14
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ APPLICATION_MANAGER::GetOrCreateApplicationInfo(
)
{
// GetOrCreateApplicationInfo is called from proxymodule when a request is received.
// Set this value to indicate that a request has been received so we can disable shutdown logic in OnGlobalApplicationStop
m_hasStarted = true;

PCWSTR pszVariableValue = nullptr;
DWORD cbLength = 0;
// Check for preload or warmup request, part of the application initialization process, see comments in ASPNET_CORE_GLOBAL_MODULE::OnGlobalApplicationStop for more info
if (FAILED(pHttpContext.GetServerVariable("PRELOAD_REQUEST", &pszVariableValue, &cbLength)) &&
FAILED(pHttpContext.GetServerVariable("WARMUP_REQUEST", &pszVariableValue, &cbLength)))
{
// Set this value to indicate that a request has been received so we can disable shutdown logic in OnGlobalApplicationStop
m_hasStarted = true;
}
auto &pApplication = *pHttpContext.GetApplication();

// The configuration path is unique for each application and is used for the
Expand Down
25 changes: 15 additions & 10 deletions src/Servers/IIS/AspNetCoreModuleV2/AspNetCore/globalmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ ASPNET_CORE_GLOBAL_MODULE::OnGlobalStopListening(
return GL_NOTIFICATION_CONTINUE;
}

// We prefer shutting down from OnGlobalStopListening as it is called right before the IIS request handler is disabled, which means it'll start queueing requests
// But if we stopped in OnGlobalApplicationStop then we can start shutting down while the request handler is still active resulting in us returning 503's since we're shutting down.
// We still need to shutdown in specific cases where OnGlobalStopListening isn't called, like IISExpress or if the app never receives a request (app preload).
GLOBAL_NOTIFICATION_STATUS
ASPNET_CORE_GLOBAL_MODULE::OnGlobalApplicationStop(
IN IHttpApplicationStopProvider* pProvider
Expand All @@ -51,17 +54,19 @@ ASPNET_CORE_GLOBAL_MODULE::OnGlobalApplicationStop(

LOG_INFO(L"ASPNET_CORE_GLOBAL_MODULE::OnGlobalApplicationStop");

if (!g_fInShutdown && !m_shutdown.joinable()
&& (m_pApplicationManager->IsIISExpress() || !m_pApplicationManager->HasReceivedRequest()))
if (!g_fInShutdown && !m_shutdown.joinable())
{
// Apps with preload + always running that don't receive a request before recycle/shutdown will never call OnGlobalStopListening
// IISExpress can also close without calling OnGlobalStopListening which is where we usually would trigger shutdown
// so we should make sure to shutdown the server in those cases
StartShutdown();
}
else
{
LOG_INFO(L"Ignoring OnGlobalApplicationStop, OnGlobalStopListening should be called shortly.");
if ((m_pApplicationManager->IsIISExpress() || !m_pApplicationManager->HasReceivedRequest()))
{
// Apps with preload + always running that don't receive a request before recycle/shutdown will never call OnGlobalStopListening
// IISExpress can also close without calling OnGlobalStopListening which is where we usually would trigger shutdown
// so we should make sure to shutdown the server in those cases
StartShutdown();
}
else
{
LOG_INFO(L"Ignoring OnGlobalApplicationStop, OnGlobalStopListening has been called or should be called shortly.");
}
}

return GL_NOTIFICATION_CONTINUE;
Expand Down
3 changes: 1 addition & 2 deletions src/Servers/IIS/IntegrationTesting.IIS/src/IISDeployer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,14 @@ private static Site FindSite(ServerManager serverManager, string contentRoot)

private void AddTemporaryAppHostConfig(string contentRoot, int port)
{
var multiSite = true;
var multiSite = _applicationHostConfig is not null;
XDocument config;
if (_applicationHostConfig is not null)
{
config = XDocument.Parse(File.ReadAllText(_applicationHostConfig));
}
else
{
multiSite = false;
_configPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("D"));
_applicationHostConfig = Path.Combine(_configPath, "applicationHost.config");
Directory.CreateDirectory(_configPath);
Expand Down

0 comments on commit f7fbf14

Please sign in to comment.