Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ private IEnumerable<ProcessOutputLine> 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<byte[]>(outputByteBuffer);
errorPin = new PinnedGCHandle<byte[]>(errorByteBuffer);

Expand Down Expand Up @@ -224,6 +229,16 @@ private IEnumerable<ProcessOutputLine> ReadPipesToLines(
}
}

if (outputRefAdded)
{
outputHandle.DangerousRelease();
}

if (errorRefAdded)
{
errorHandle.DangerousRelease();
}

outputEvent?.Dispose();
errorEvent?.Dispose();
outputPin.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -674,6 +676,7 @@ public StreamReader StandardOutput
throw new InvalidOperationException(SR.CantMixSyncAsyncOperation);
}

_standardOutputAccessed = true;
return _standardOutput;
}
}
Expand All @@ -700,6 +703,7 @@ public StreamReader StandardError
throw new InvalidOperationException(SR.CantMixSyncAsyncOperation);
}

_standardErrorAccessed = true;
return _standardError;
}
}
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down
Loading