diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Multiplexing.Windows.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Multiplexing.Windows.cs index 62ccd98e363d47..055960e06964b4 100644 --- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Multiplexing.Windows.cs +++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Multiplexing.Windows.cs @@ -44,9 +44,14 @@ private IEnumerable ReadPipesToLines( // nothing is pending, e.g. if QueueRead throws right after the overlapped was reset. // CancelPendingIOIfNeeded in the finally block must only act when this is true. bool outputPending = false, errorPending = false; + bool outputRefAdded = false, errorRefAdded = false; try { + // The handles are used across yields, so they must stay open even if the Process is disposed in the meantime. + outputHandle.DangerousAddRef(ref outputRefAdded); + errorHandle.DangerousAddRef(ref errorRefAdded); + outputPin = new PinnedGCHandle(outputByteBuffer); errorPin = new PinnedGCHandle(errorByteBuffer); @@ -224,6 +229,16 @@ private IEnumerable ReadPipesToLines( } } + if (outputRefAdded) + { + outputHandle.DangerousRelease(); + } + + if (errorRefAdded) + { + errorHandle.DangerousRelease(); + } + outputEvent?.Dispose(); errorEvent?.Dispose(); outputPin.Dispose(); diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs index 06f58ad3799978..4e9a6d858c8a72 100644 --- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs +++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs @@ -71,6 +71,8 @@ public partial class Process : Component private bool _disposed; private bool _standardInputAccessed; + private bool _standardOutputAccessed; + private bool _standardErrorAccessed; private StreamReadMode _outputStreamReadMode; private StreamReadMode _errorStreamReadMode; @@ -674,6 +676,7 @@ public StreamReader StandardOutput throw new InvalidOperationException(SR.CantMixSyncAsyncOperation); } + _standardOutputAccessed = true; return _standardOutput; } } @@ -700,6 +703,7 @@ public StreamReader StandardError throw new InvalidOperationException(SR.CantMixSyncAsyncOperation); } + _standardErrorAccessed = true; return _standardError; } } @@ -843,9 +847,10 @@ public void Close() // Only call close on the streams if the user cannot have a reference on them. // If they are referenced it is the user's responsibility to dispose of them. + // The streams used only by the ReadAll* methods are never exposed to the user. try { - if (_standardOutput != null && (_outputStreamReadMode == StreamReadMode.AsyncMode || _outputStreamReadMode == StreamReadMode.Undefined)) + if (_standardOutput != null && !_standardOutputAccessed) { if (_outputStreamReadMode == StreamReadMode.AsyncMode) { @@ -855,7 +860,7 @@ public void Close() _standardOutput.Close(); } - if (_standardError != null && (_errorStreamReadMode == StreamReadMode.AsyncMode || _errorStreamReadMode == StreamReadMode.Undefined)) + if (_standardError != null && !_standardErrorAccessed) { if (_errorStreamReadMode == StreamReadMode.AsyncMode) {