Skip to content

Fix shutdown hang with IIS - #65733

Merged
BrennanConroy merged 11 commits into
mainfrom
brecon/iislifetime
Aug 14, 2026
Merged

BrennanConroy merged 11 commits into
mainfrom
brecon/iislifetime

Conversation

@BrennanConroy

@BrennanConroy BrennanConroy commented Mar 10, 2026 •

Copy link
Copy Markdown
Member

Fixes #65436

dotnet/runtime#116652 revealed what is probably a latent bug in IIS/WAS/Windows where if Preload Enabled = true for your IIS application then machine shutdown can hang forever due to the default 30 second delay blocking the w3wp process from being closed which allows a new w3wp process time to start up, rinse and repeat.

This PR is using a custom IHostLifetime that effectively reverts ConsoleLifetime to the pre-10.0 behavior of not blocking for 30 seconds to workaround whatever bug exists in the lower layers. I have pinged an IIS dev about this, and will push on a solution from that end outside of this change.

@BrennanConroy
BrennanConroy marked this pull request as ready for review March 11, 2026 23:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a shutdown hang when IIS has Preload Enabled = true by introducing a custom IISHostLifetime that avoids the 30-second blocking sleep added in .NET 10.0's ConsoleLifetime. Also fixes a null reference bug in IISDeployer.

Changes:

  • Adds IISHostLifetime as a replacement for ConsoleLifetime in the IIS in-process hosting path
  • Fixes null dereference in IISDeployer.WaitUntilSiteStarted by moving site.Name access after the null check
  • Adds tests verifying the correct IHostLifetime is used for in-process and out-of-process hosting

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
WebHostBuilderIISExtensions.cs Registers IISHostLifetime and defines the class
IISDeployer.cs Moves site.Name access after null check
StartupTests.cs Adds tests for host lifetime verification
InProcessWebSite/Program.cs Adds HostBuilder test case
InProcessWebSite/Startup.cs Adds GetHostLifetime endpoint

Comment on lines +1467 to +1484
[ConditionalFact]
public async Task OutOfProcessHostlifetime()
{
if (DeployerSelector.IsNewShimTest)
{
// NewShim tests use 2.2 IIS packages which don't have the host lifetime
return;
}

var deploymentParameters = Fixture.GetBaseDeploymentParameters(HostingModel.OutOfProcess);
deploymentParameters.TransformArguments((a, _) => $"{a} HostBuilder");

if (deploymentParameters.ServerType == ServerType.IISExpress)
{
return;
}

Assert.Equal("ConsoleLifetime", await GetStringAsync(deploymentParameters, "GetHostLifetime"));

Copilot AI Mar 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The early return for IISExpress silently skips the test without any indication. Consider using Skip or a ConditionalFact attribute so test results clearly show the test was skipped rather than appearing to pass.

Copilot uses AI. Check for mistakes.
}

[ConditionalFact]
public async Task InProcessHostlifetime()

Copilot AI Mar 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor inconsistency: Hostlifetime should be HostLifetime (capital 'L') to match .NET naming conventions.

Copilot uses AI. Check for mistakes.
// for SIGTERM on Windows we must block this thread until the application is finished
// otherwise the process will be killed immediately on return from this handler

// don't allow Dispose to unregister handlers, since Windows has a lock that prevents the unregistration while this handler is running

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this code is changing in dotnet/runtime#124893


public Task WaitForStartAsync(CancellationToken cancellationToken)
{
_sigTermRegistration = PosixSignalRegistration.Create(PosixSignal.SIGTERM,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that it is SIGINT and SIGQUIT handlers are intentionally missing here since they are not needed by IIS.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, ctrl-c or ctrl-break doesn't make sense for IIS apps.

@jkotas
jkotas requested a review from mrek-msft March 12, 2026 17:42
// Does not copy Thread.Sleep(HostOptions.ShutdownTimeout)
// from ConsoleLifetime because that causes machine shutdown to hang
// if Preload Enabled = true
// Likely an IIS/WAS issue.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine as a minimal low-risk fix that can be backported.

For the right fix, I would expect that there is a IIS-native way to subscribe to shutdown and we should not need PosixSignal.SIGTERM handler at all.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I hope we can find a better solution in the future and this is just a temporary solution for customers that are broken by the current behavior.

@BrennanConroy

Copy link
Copy Markdown
Member Author

/backport to release/10.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

@github-actions github-actions Bot mentioned this pull request Mar 12, 2026
4 of 10 tasks
@halter73

Copy link
Copy Markdown
Member

Have we validated that active requests will keep the process running until they are drained or you it the HostOptions.ShutdownTimeout in IIS with Preload Enabled = true? Does this exist already as an automated test or would this be easy to add?

@BrennanConroy

Copy link
Copy Markdown
Member Author

We have shutdown tests in https://github.com/dotnet/aspnetcore/blob/main/src/Servers/IIS/IIS/test/Common.LongTests/ShutdownTests.cs including ones with requests while app shutdown occurs. I don't think any explicitly test preload with shutdown, and we can't test preload with machine shutdown because there is no way I know of to fake a machine shutdown for a service process.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Mar 24, 2026
BrennanConroy and others added 6 commits July 8, 2026 14:21
@BrennanConroy
BrennanConroy enabled auto-merge (squash) August 13, 2026 21:51
@BrennanConroy
BrennanConroy disabled auto-merge August 13, 2026 23:01
@BrennanConroy
BrennanConroy merged commit f97c0ff into main Aug 14, 2026
27 checks passed
@BrennanConroy
BrennanConroy deleted the brecon/iislifetime branch August 14, 2026 00:15
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions feature-iis Includes: IIS, ANCM pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NET10 + IIS (InProcess with Preload Enabled) causes Windows to hang during shutdown

6 participants