Skip to content
Merged
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
27 changes: 19 additions & 8 deletions src/System.Management.Automation/engine/remoting/client/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1542,14 +1542,25 @@ internal void AssertNotDisposed()
/// </summary>
internal void CloseAllStreams()
{
if (_resultsOwner) _results.Complete();
if (_outputOwner) _output.Complete();
if (_errorOwner) _error.Complete();
if (_progressOwner) _progress.Complete();
if (_verboseOwner) _verbose.Complete();
if (_warningOwner) _warning.Complete();
if (_debugOwner) _debug.Complete();
if (_informationOwner) _information.Complete();
// The Complete() method includes raising public notification events that third parties can
// handle and potentially throw exceptions on the notification thread. We don't want to
// propagate those exceptions because it prevents this thread from completing its processing.
if (_resultsOwner) { try { _results.Complete(); } catch (Exception e) { TraceException(e); } }
if (_outputOwner) { try { _output.Complete(); } catch (Exception e) { TraceException(e); } }
if (_errorOwner) { try { _error.Complete(); } catch (Exception e) { TraceException(e); } }
if (_progressOwner) { try { _progress.Complete(); } catch (Exception e) { TraceException(e); } }
if (_verboseOwner) { try { _verbose.Complete(); } catch (Exception e) { TraceException(e); } }
if (_warningOwner) { try { _warning.Complete(); } catch (Exception e) { TraceException(e); } }
if (_debugOwner) { try { _debug.Complete(); } catch (Exception e) { TraceException(e); } }
if (_informationOwner) { try { _information.Complete(); } catch (Exception e) { TraceException(e); } }
}

private static void TraceException(Exception e)
{
using (PowerShellTraceSource tracer = PowerShellTraceSourceFactory.GetTraceSource())
{
tracer.TraceException(e);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,12 @@ private void WriteInput(object inputValue)
/// <param name="nonblocking">Write in a non-blocking manner</param>
private void WriteJobResults(bool nonblocking)
{
if (_job != null)
if (_job == null)
{
return;
}

try
{
PipelineStoppedException caughtPipelineStoppedException = null;
_job.PropagateThrows = _propagateErrors;
Expand Down Expand Up @@ -1756,7 +1761,12 @@ private void WriteJobResults(bool nonblocking)
session.Name, session.InstanceId));
}
}

}
}
finally
{
if (_job.JobStateInfo.State == JobState.Disconnected)
{
// Allow Invoke-Command to end even though not all remote pipelines
// finished.
HandleThrottleComplete(null, null);
Expand Down