Skip to content

Prevent storing incomplete _remoteInvokeHandle.Process on error - #137025

Merged
mrek-msft merged 5 commits into
dotnet:mainfrom
mrek-msft:dev/mrek/mehwsvc-tester-handle-race
Nov 18, 2026
Merged

mrek-msft merged 5 commits into
dotnet:mainfrom
mrek-msft:dev/mrek/mehwsvc-tester-handle-race

Conversation

@mrek-msft

Copy link
Copy Markdown
Member

Fix #136992

Precodnitions:

  • We assume process handle is always available, it is precondition for Dispose to not throw (it needs handle to read ExitCode which is also expressed in comment)

Race details:

  • In Start(), GetProcessById succeeds, no ArgumentException thrown
  • Process exit then
  • Obtaining SafeHandle on next line fails then
  • precondition violated, we have Process, but without Handle

Test failure:

  • Dispose crashes sometime afterwards due to earlier violated precondition

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-hosting
See info in area-owners.md if you want to be subscribed.

@svick

svick commented Nov 12, 2026

Copy link
Copy Markdown
Member

Doesn't this just replace one failure with another? If accessing p.SafeHandle fails, because the process exited, then it throws InvalidOperationException, which is not caught, and the test fails. Or am I missing something?

Or is the goal of this PR to make future test failures caused by this race easier to diagnose, but not to prevent them?

@mrek-msft

Copy link
Copy Markdown
Member Author

@svick Considering catch of ArgumentException intention, I added same for InvalidOperationException. I also reworked it to call Dispose if we are unable to proceed with obtained Process.

@svick svick 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.

LGTM

@mrek-msft
mrek-msft merged commit 9ae326e into dotnet:main Nov 18, 2026
62 of 64 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 2026
lewing added a commit that referenced this pull request Nov 19, 2026
…137929)

Fixes Issue #136992 on release/11.0. <!-- Issue Number -->

main PR #137025 <!-- Link to PR if
any that fixed this in the main branch. -->

# Description

<!-- Give a brief summary of the issue and how the pull request is
fixing it. -->

Whole, unmodified backport of 9ae326e.
Changes only
`src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceTester.cs`.

The test helper now obtains the service process's SafeHandle before
assigning the Process to RemoteInvokeHandle. If the process exits before
handle acquisition, the helper disposes the temporary Process and
returns without retaining an incomplete Process that can make cleanup
throw.

# Customer Impact

<!-- What is the impact to customers of not taking this fix? -->

No product behavior changes. Without this test-only fix, Windows service
tests can fail intermittently during cleanup, adding CI noise to
release/11.0.

# Regression

<!-- Is this fixing a problem that was introduced in the most recent
release, ie., fixing a regression? -->

This fixes an existing test-helper race present on release/11.0; it is
not a customer-facing product regression.

# Testing

<!-- What kind of testing has been done with the fix. -->

- `git diff --check` passed.
- `git diff --
src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceTester.cs
| git patch-id --stable` matched the source commit's patch ID:
`3c8ec18b17b577a70a57170a026a92060eb1e27a`.
- Installed the .NET 11 daily SDK with `dotnet-install --channel 11.0
--quality daily` (SDK `11.0.200-preview.26568.113`, macOS arm64).
- `.dotnet/dotnet build
src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/Microsoft.Extensions.Hosting.WindowsServices.Tests.csproj
-p:TargetOS=windows -p:BuildTargetFramework=net11.0` failed because the
repository's local shared-framework targeting pack has not been built
(`eng/targetingpacks.targets(107,5)`). No successful test-project
compilation is claimed.
- The full `clr+libs` baseline and all test execution were not run.
Windows service execution requires Windows. PR CI will provide build and
runtime verification.

# Risk

<!-- Please assess the risk of taking this fix. Provide details backing
up your assessment. -->

Low: test-only, single-file, exact upstream patch; no product code,
public APIs, or package changes. Successful handle acquisition preserves
the existing path; the process-exit race no longer retains an incomplete
Process and disposes the temporary object.

# Package authoring no longer needed in .NET 9

IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet
package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older
versions.

> [!NOTE]
> This backport and PR description were prepared with GitHub Copilot.

Co-authored-by: Copilot App <[email protected]>
lewing added a commit that referenced this pull request Nov 20, 2026
…137930)

Fixes Issue #136992 <!-- Issue Number -->

main PR #137025 <!-- Link to PR if any that fixed this in the main
branch. -->

# Description

<!-- Give a brief summary of the issue and how the pull request is
fixing it. -->

Test-only backport of 9ae326e.

Keep the process local until its SafeHandle has been acquired, then
assign it to the remote invocation handle. If the service process exits
before handle acquisition, dispose the temporary Process and leave the
remote invocation handle's Process unset. This prevents a later cleanup
failure caused by storing a Process without an acquired handle.

Only
src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/WindowsServiceTester.cs
changes; no product code changes or branch adaptations.

# Customer Impact

<!-- What is the impact to customers of not taking this fix? -->

No shipped product behavior changes. Without this fix, Windows service
tests can fail intermittently when the service exits during test-helper
startup.

# Regression

<!-- Is this fixing a problem that was introduced in the most recent
release, ie., fixing a regression? -->

The same test-helper race exists on release/10.0. It has not been
established as a regression introduced in the most recent release.

# Testing

<!-- What kind of testing has been done with the fix. -->

- `dotnet build
src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/Microsoft.Extensions.Hosting.WindowsServices.Tests.csproj
--nologo -v:minimal` failed because the local shared-framework targeting
pack has not been built.
- `dotnet build
src/libraries/Microsoft.Extensions.Hosting.WindowsServices/tests/Microsoft.Extensions.Hosting.WindowsServices.Tests.csproj
-p:UseLocalTargetingRuntimePack=false --nologo -v:minimal` passed on
macOS, with zero warnings and zero errors, producing both
net10.0-windows and net481 test assemblies. This is compile-only
verification using SDK targeting packs and installed SDK
11.0.100-preview.6.26351.102 via the branch's allowed major
roll-forward; it is not daily-runtime verification.
- `git diff --check` passed. The stable patch ID matches the source
commit.

The full clr+libs baseline build was skipped for this test-only
backport. No tests were executed locally, including Windows service
tests, which require Windows. Windows runtime verification is deferred
to PR CI.

# Risk

<!-- Please assess the risk of taking this fix. Provide details backing
up your assessment. -->

Low risk: an unchanged upstream fix confined to one test helper. The
helper publishes a Process only after handle acquisition succeeds and
disposes it on the process-exit race. No shipping code or public APIs
change.

# Package authoring no longer needed in .NET 9

IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet
package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older
versions.

Co-authored-by: MichalZ <[email protected]>
Co-authored-by: Copilot App <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test failure: Microsoft.Extensions.Hosting.WindowsServiceLifetimeTests.ExceptionOnStartIsPropagated

2 participants