Repository navigation
Allow UseShellExecute when ProcessStartInfo.Environment was only read - #137428
Merged
Merged
Conversation
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: 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. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-diagnostics-process |
Contributor
There was a problem hiding this comment.
🟡 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]>
eiriktsarpalis
approved these changes
Nov 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
DictionaryWrapperso readingProcessStartInfo.EnvironmentorEnvironmentVariablesalone does not trigger the WindowsUseShellExecuterestriction.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).ChildProcess_CanBreakAwayFromJob(killOnParentExit: False)); reproduced on baselinemain.Resolves #58069
Note
This PR description was generated with GitHub Copilot.