Skip to content

Fix ANCM server process dispatch lifetime - #69349

Open
javiercn wants to merge 2 commits into
mainfrom
javiercn/iis-getprocess-ownership
Open

javiercn wants to merge 2 commits into
mainfrom
javiercn/iis-getprocess-ownership

Conversation

@javiercn

@javiercn javiercn commented Sep 16, 2026 •

Copy link
Copy Markdown
Member

Summary

  • acquire a caller-owned SERVER_PROCESS reference on every successful PROCESS_MANAGER::GetProcess path while the process-manager lock still protects the selected slot
  • release that reference with RAII after synchronous WinHTTP request setup and the initial send
  • remove the unlocked process-list read after a shared-lock miss
  • add deterministic native ownership coverage and an IIS backend-exit stress test

Fixes #68366.

Why

GetProcess returned a borrowed pointer after dropping the process-manager lock. If the backend exited before ExecuteRequestHandler finished synchronous request setup, the process-exit callback could remove the list reference, release the wait reference, and delete SERVER_PROCESS and its FORWARDER_CONNECTION while dispatch still used them.

The new reference is acquired under the same shared/exclusive lock that protects process-list removal. The forwarding handler adopts it immediately in SERVER_PROCESS_PTR and releases it before DereferenceRequestHandler, which may delete the handler.

The reference deliberately ends after the initial WinHttpSendRequest. The handler stores no SERVER_PROCESS* for asynchronous completion, and retaining a dead backend for the full HTTP/WebSocket lifetime would change existing process-exit semantics.

Compatibility

This changes private implementation inside aspnetcorev2_outofprocess.dll. The module still exports only CreateApplication; IAPPLICATION, IREQUEST_HANDLER, and shim/handler ABI are unchanged. Allocation and deletion remain in the same module. Supported mixed shim/handler versions cannot combine different sides of this internal ownership contract because producer and caller ship in the same request-handler DLL.

Validation

  • deterministic native red/green test: removing the three dispatch AddRef calls fails ProcessManagerTests.GetProcessReferenceSurvivesRemovalFromManager because removal of the list reference destroys the process; restoring them passes
  • native owner test passes in x64 Debug, x64 Release, and Win32 Debug
  • x64 Debug and Release native handler builds pass
  • x64 Debug native code analysis passes
  • IIS long stress test compiles and is discovered; local execution is skipped because the console is not elevated, so elevated CI provides the faithful execution environment

The native test uses the real manager and process refcount path: it acquires through GetProcess, removes the manager list reference through ShutdownProcess, verifies the caller keeps the process alive, then verifies destruction when the caller releases it.

Scope

The pre-existing parent/child wait-reference imbalance and UnregisterWait pending-callback behavior are independent shutdown-lifetime concerns and intentionally excluded from this focused change.

@github-actions github-actions Bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Sep 16, 2026
@javiercn
javiercn force-pushed the javiercn/iis-getprocess-ownership branch from 3b3b75b to 3993bdc Compare September 24, 2026 14:22
@javiercn

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.

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.

Copilot review overview

🔵 Needs a closer look

Native process lifetime and concurrent shutdown behavior warrant final human review.

Review effort: Lite
Findings: None

What changed in this PR

Fixes ANCM out-of-process process lifetime handling using caller-owned references and RAII cleanup.

Changes:

  • Protects SERVER_PROCESS references during request dispatch.
  • Adds native ownership coverage and IIS shutdown stress testing.
  • Documents ownership requirements and updates test project dependencies.
File Description
src/​Servers/​IIS/​IIS/​test/​Common.LongTests/​ShutdownTests.cs Adds backend-exit dispatch stress coverage.
src/​Servers/​IIS/​AspNetCoreModuleV2/​OutOfProcessRequestHandler/​serverprocess.h Defines the RAII process pointer and test access.
src/​Servers/​IIS/​AspNetCoreModuleV2/​OutOfProcessRequestHandler/​processmanager.h Documents process ownership requirements.
src/​Servers/​IIS/​AspNetCoreModuleV2/​OutOfProcessRequestHandler/​processmanager.cpp Acquires references safely under lock.
src/​Servers/​IIS/​AspNetCoreModuleV2/​OutOfProcessRequestHandler/​outprocessapplication.h Propagates ownership documentation.
src/​Servers/​IIS/​AspNetCoreModuleV2/​OutOfProcessRequestHandler/​forwardinghandler.cpp Releases process ownership via RAII.
src/​Servers/​IIS/​AspNetCoreModuleV2/​CommonLibTests/​processmanager_owner_tests.cpp Adds deterministic lifetime coverage.
src/​Servers/​IIS/​AspNetCoreModuleV2/​CommonLibTests/​CommonLibTests.vcxproj Builds and links the native ownership tests.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@BrennanConroy BrennanConroy left a comment

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.

processmanager_owner_tests.cpp isn't really needed but I won't block on removing it.

It would be slightly better for GetProcess to use SERVER_PROCESS_PTR& pServerProcess so the explicit reset() calls aren't needed.

@javiercn

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.

@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 Oct 7, 2026

This branch has not been deployed

No deployments
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 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.

IIS OutOfProcess ANCM: GetProcess() hands out SERVER_PROCESS* without a caller-owned reference

3 participants