Skip to content

Allow UseShellExecute when ProcessStartInfo.Environment was only read - #137428

Merged
adamsitnik merged 2 commits into
mainfrom
fix-58069-shellexecute-env
Nov 12, 2026
Merged

adamsitnik merged 2 commits into
mainfrom
fix-58069-shellexecute-env

Conversation

@adamsitnik

Copy link
Copy Markdown
Member

Summary

  • Track actual environment mutations in DictionaryWrapper so reading ProcessStartInfo.Environment or EnvironmentVariables alone does not trigger the Windows UseShellExecute restriction.
  • Preserve explicit environment passing for CreateProcess paths after the environment is read.
  • Add Windows tests for read-only access and set/remove/clear mutations.

Testing

  • dotnet build src\libraries\System.Diagnostics.Process\tests\System.Diagnostics.Process.Tests.csproj /t:Test "/p:XUnitOptions=-class System.Diagnostics.Tests.ProcessStartInfoTests" — passed (74 tests, 0 failed, 2 skipped).
  • Full Process test suite: 1 unrelated failure (ChildProcess_CanBreakAwayFromJob(killOnParentExit: False)); reproduced on baseline main.

Resolves #58069

Note

This PR description was generated with GitHub Copilot.

Reading ProcessStartInfo.Environment/EnvironmentVariables (e.g. by a debugger,
serializer or logging) lazily created the dictionary, which made Process.Start
with UseShellExecute = true throw InvalidOperationException on Windows.

Track actual modifications in DictionaryWrapper and throw only when the
environment was modified.

Fixes #58069

Co-authored-by: Copilot <[email protected]>
Copilot-Session: f1f4be50-afad-498e-96d6-62c7358bd322
@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.

@adamsitnik
adamsitnik requested a balanced review from Copilot November 12, 2026 14:43
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

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.

🟡 Changes recommended

The new test runs on Windows Nano Server, where shell execution throws ThreadStartException instead of the expected Win32Exception, so it needs a conditional guard excluding that platform.

1 open issue
What changed in this PR

This PR updates System.Diagnostics.Process so reading ProcessStartInfo.Environment or EnvironmentVariables no longer blocks Windows shell execution, while environment modifications remain disallowed.

Changes:

  • Track environment mutations separately from initial population.
  • Check mutation state when validating UseShellExecute.
  • Add Windows tests for read-only access and set/remove/clear operations.
File Description
src/​libraries/​System.Diagnostics.Process/​tests/​ProcessStartInfoTests.cs Adds read-only and mutation validation tests.
src/​libraries/​System.Diagnostics.Process/​src/​System/​Diagnostics/​ProcessStartInfo.cs Populates the environment snapshot without marking it mutated.
src/​libraries/​System.Diagnostics.Process/​src/​System/​Collections/​Specialized/​DictionaryWrapper.cs Tracks dictionary mutations.
src/​libraries/​System.Diagnostics.Process/​src/​Microsoft/​Win32/​SafeHandles/​SafeProcessHandle.Windows.cs Rejects shell execution only when the environment was mutated.

Review effort: Balanced

Co-authored-by: Copilot Autofix powered by AI <[email protected]>

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.

🟢 Approval recommended

No active review issues remain for the change allowing shell execution after read-only environment access.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@adamsitnik
adamsitnik enabled auto-merge (squash) November 12, 2026 15:07
@adamsitnik
adamsitnik merged commit aa931db into main Nov 12, 2026
86 of 88 checks passed
@adamsitnik
adamsitnik deleted the fix-58069-shellexecute-env branch November 12, 2026 17:02
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.

Diagnostics.Process.Start: UseShellExecute=true and EnvironmentVariables gotcha

3 participants