Repository navigation
Close the redirected streams used by Process.ReadAll* and RunAndCaptureText on Dispose - #137657
Conversation
The ReadAll* methods, and the RunAndCaptureText(Async) methods built on top of them, put the redirected standard output and error into synchronous read mode, so Process.Dispose left them open for the caller although the caller never obtained them. Track whether the streams were obtained from the StandardOutput/StandardError getters instead, like for StandardInput. Co-authored-by: Copilot <[email protected]> Copilot-Session: 8cfdacb8-9380-470b-83d0-2598d9255b9b
|
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. |
|
Tagging subscribers to this area: @dotnet/area-system-diagnostics-process |
Process.Dispose now closes the streams used by ReadAllLines, so the Windows iterator must hold references to the handles across yields, like the Unix one. Otherwise, disposing the Process during the enumeration closes a handle with a pending overlapped read, and the cleanup in the iterator's finally block throws before freeing its resources. Co-authored-by: Copilot <[email protected]> Copilot-Session: 8cfdacb8-9380-470b-83d0-2598d9255b9b
@jkotas I will later update the Process SKILL to ensure such bugs are discovered as well |
There was a problem hiding this comment.
🟡 Changes recommended
Deterministic regression tests are missing for stream disposal across the sync/async ReadAll variants and Windows iterator cleanup when disposal occurs with a pipe read pending.
1 open issue
What changed in this PR
This PR fixes redirected-stream cleanup in System.Diagnostics.Process for ReadAll* and RunAndCaptureText, while preserving caller ownership of readers obtained through the public getters.
Changes:
- Track whether callers obtained standard output and error readers, and close unexposed readers during disposal.
- Keep Windows pipe handles alive across
ReadAllLinesyields until iterator cleanup completes.
| File | Description |
|---|---|
src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Multiplexing.Windows.cs |
Retains and releases pipe handles around line enumeration. |
src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs |
Tracks reader ownership to close internally consumed streams. |
Review effort: Balanced
…re-dispose-streams # Conflicts: # src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Multiplexing.Windows.cs
|
/ba-g the failure is unrelated Details |
|
/backport to release/11.0 |
|
Started backporting to |
File a known issue on this? https://github.com/dotnet/runtime/blob/main/docs/workflow/ci/failure-analysis.md#what-to-do-if-you-determine-the-failure-is-unrelated |

Process.Disposecloses the redirected standard output and error only when the caller can't have a reference to them. It used to decide that by the read mode of each stream.ReadAllText,ReadAllBytesandReadAllLines(and theirAsyncversions) put the streams into the same synchronous mode as theStandardOutput/StandardErrorgetters. So their streams were never closed byDispose, although the caller never got a reference to them.This affects every
Processthat uses these APIs, and alsoProcess.RunAndCaptureText/RunAndCaptureTextAsync, which callReadAllText(Async)on aProcessthey never expose. Their pipes stayed open until the GC finalized them. On Windows, before #137507, that was never. A user reported the growth in #137501 (comment).Fix
Processnow tracks whether the caller obtained each stream from theStandardOutput/StandardErrorgetters, in the same way it already does forStandardInput(_standardInputAccessed).Disposecloses every stream that the caller didn't obtain:BeginOutputReadLine/BeginErrorReadLineStandardOutput/StandardErrorgetterReadAll*without the getterBecause
Disposecan now close the streams used byReadAllLines, the WindowsReadAllLinesiterator now holds references to both pipe handles until the enumeration ends (DangerousAddRef/DangerousRelease), as the Unix iterator andReadAllText/ReadAllBytesalready did. Without that, disposing theProcessduring the enumeration would close a handle with an overlapped read still pending, and the iterator's cleanup would throwObjectDisposedExceptionbefore freeing its resources.Testing
System.Diagnostics.Process.Testssuite passes on Windows x64 (Release), exceptChildProcess_CanBreakAwayFromJob, which also fails without this change in my environment.Note
This PR description was generated with the help of GitHub Copilot.