Skip to content

Add StandardInputHandle/OutputHandle/ErrorHandle SafeFileHandle properties and LeaveHandlesOpen to ProcessStartInfo#125848

Draft
Copilot wants to merge 28 commits intomainfrom
copilot/add-standard-input-output-properties
Draft

Add StandardInputHandle/OutputHandle/ErrorHandle SafeFileHandle properties and LeaveHandlesOpen to ProcessStartInfo#125848
Copilot wants to merge 28 commits intomainfrom
copilot/add-standard-input-output-properties

Conversation

Copy link
Contributor

Copilot AI commented Mar 20, 2026

Description

Adds three new SafeFileHandle? properties and a LeaveHandlesOpen property to ProcessStartInfo that allow passing file handles directly to child processes, bypassing the RedirectStandard* pipe-based mechanism.

var psi = new ProcessStartInfo("grep") { ArgumentList = { "test" } };
SafeFileHandle.CreateAnonymousPipe(out SafeFileHandle readPipe, out SafeFileHandle writePipe);
psi.StandardInputHandle = readPipe;
psi.StandardOutputHandle = File.OpenHandle("output.txt", FileMode.Create, FileAccess.Write);
Process.Start(psi);

By default, Process.Start disposes the standard handles after starting the child process. Set LeaveHandlesOpen to true to keep them open for the caller to manage.

Changes

  • ProcessStartInfo.cs: New StandardInputHandle, StandardOutputHandle, StandardErrorHandle auto-properties (SafeFileHandle?) with XML docs. Remarks document that handles do not need to be inheritable (the runtime duplicates them as inheritable), and list typical APIs (SafeFileHandle.CreateAnonymousPipe, File.OpenHandle, File.OpenNullHandle, Console.OpenStandardInputHandle/OpenStandardOutputHandle/OpenStandardErrorHandle), interaction with LeaveHandlesOpen, and constraints. New LeaveHandlesOpen property (bool, default false) controlling whether handles are disposed after process start.
  • ref/System.Diagnostics.Process.cs: Public API surface additions for StandardInputHandle, StandardOutputHandle, StandardErrorHandle, and LeaveHandlesOpen
  • Process.cs validation: When any Standard*Handle is set, UseShellExecute must be false and the corresponding RedirectStandard* must also be false. Reuses existing SR.CantRedirectStreams error message for the UseShellExecute check. A ValidateHandle helper method checks each provided handle for IsClosed (throws ObjectDisposedException) and IsInvalid (throws ArgumentException), following the same pattern as FileStream.ValidateHandle.
  • Process.cs pipe creation: New handles take priority over default stdin/stdout/stderr inheritance. Handles are always disposed after starting the child process unless LeaveHandlesOpen is true.
  • Strings.resx: New error strings CantSetHandleAndRedirect and Arg_InvalidHandle
  • ProcessHandlesTests.cs: Tests for pipe redirection, inherited handles, piping, LeaveHandlesOpen behavior with File.OpenHandle, and validation error cases. Includes a LeaveHandlesOpen_KeepsHandleOpen test that verifies handles remain open after Process.Start when LeaveHandlesOpen is true, using hostname as a cross-platform command and File.OpenHandle for the output file.

💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

Copilot AI and others added 17 commits March 18, 2026 22:14
…ation and stream setup to Process.cs

Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
…nterop.Pipe.cs to csproj

Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
…eAnonymousPipe directly, simplify Windows StartWithCreateProcess

Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
- STARTF_USESTDHANDLES is always provided now
- we may use more flags in the future
- so ProcessWindowStyle tests should check only if STARTF_USESHOWWINDOW (1) was applied
… 0/1/2; remove Android checks from Process.cs

Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
…rning fd 0/1/2; remove Android checks from Process.cs"

This reverts commit bba0144.
…artInfo

Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
@dotnet-policy-service
Copy link
Contributor

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

@adamsitnik adamsitnik added the NO-REVIEW Experimental/testing PR, do NOT review it label Mar 20, 2026
Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

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

@copilot please apply my suggestions and add one more test that uses File.OpenHandle to open a file handle for writing, set it via StandardOutput property and set LeaveHandlesOpen to true and verify that after starting the process the handle is still opened. Please choose command line utility that is available on every OS (hostname? whoami? pwd?)

…() calls, add LeaveHandlesOpen test

Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/6c515d2e-34b7-483e-a1e5-2b0fae3caad8
Copy link
Contributor Author

Copilot AI commented Mar 23, 2026

@copilot please apply my suggestions and add one more test that uses File.OpenHandle to open a file handle for writing, set it via StandardOutput property and set LeaveHandlesOpen to true and...

Applied both suggestions and added the LeaveHandlesOpen_KeepsHandleOpen test in 017f286. The test uses hostname (available on all OSes), opens a file via File.OpenHandle, sets LeaveHandlesOpen = true, and asserts the handle is still open after Process.Start.

/// </summary>
/// <remarks>
/// <para>
/// The handle does not need to be inheritable; the runtime will make it inheritable as needed.
Copy link
Member

@jkotas jkotas Mar 23, 2026

Choose a reason for hiding this comment

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

Is "as needed" correct? The implementation in the other PR is duplicating it as inheritable unconditionally.

Copy link
Member

Choose a reason for hiding this comment

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

Not yet, that is why I want to introduce SafeFileHandle.IsInheritable in #125838.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated wording to "the runtime will duplicate it as inheritable" in 7920b52.

@adamsitnik adamsitnik changed the base branch from copilot/refactor-startcore-method-arguments to main March 23, 2026 17:29
- add more tests
- fix bugs
- handle edge cases
Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

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

@copilot please address my feedback

/// </summary>
/// <remarks>
/// <para>
/// The handle does not need to be inheritable; the runtime will make it inheritable as needed.
Copy link
Member

Choose a reason for hiding this comment

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

Not yet, that is why I want to introduce SafeFileHandle.IsInheritable in #125838.

…/ErrorHandle, fix docs, add handle validation

Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/7c3e65ca-2e62-4821-b8d2-0bc9bde12049
Copy link
Contributor Author

Copilot AI commented Mar 23, 2026

@copilot please address my feedback

All feedback addressed in 7920b52: renamed properties to *Handle, fixed "discard input" → "provide an empty input", added default-behavior note for null, changed "close" → "dispose" in docs, updated inheritability wording, and added ValidateHandle helper for invalid/closed checks.

Copilot AI changed the title Add StandardInput/Output/Error SafeFileHandle properties and LeaveHandlesOpen to ProcessStartInfo Add StandardInputHandle/OutputHandle/ErrorHandle SafeFileHandle properties and LeaveHandlesOpen to ProcessStartInfo Mar 23, 2026
Copilot AI requested a review from adamsitnik March 23, 2026 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Diagnostics.Process NO-REVIEW Experimental/testing PR, do NOT review it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants