Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 825352f

Browse files
author
Sergei Vorobev
committed
Separate Done() and Dispose() in ProcessInputWriter NativeCommandProcessor.cs
1 parent 32837f0 commit 825352f

1 file changed

Lines changed: 32 additions & 19 deletions

File tree

src/System.Management.Automation/engine/NativeCommandProcessor.cs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,7 @@ internal void Add(object input)
17011701
{
17021702
if (_stopping || _streamWriter == null)
17031703
{
1704-
// if _streamWriter is already null, then we already called Done()
1704+
// if _streamWriter is already null, then we already called Dispose()
17051705
// so we should just discard the input.
17061706
return;
17071707
}
@@ -1736,7 +1736,7 @@ private void AddTextInputFromFormattedArray(Array formattedObjects)
17361736
{
17371737
// we are assuming that process is already finished
17381738
// we should just stop processing at this point
1739-
this.Done();
1739+
this.Dispose();
17401740
// stop foreach execution
17411741
break;
17421742
}
@@ -1753,7 +1753,7 @@ private void AddXmlInput(object input)
17531753
{
17541754
// we are assuming that process is already finished
17551755
// we should just stop processing at this point
1756-
this.Done();
1756+
this.Dispose();
17571757
}
17581758
}
17591759

@@ -1812,36 +1812,26 @@ internal void Stop()
18121812
_stopping = true;
18131813
}
18141814

1815-
/// <summary>
1816-
/// This method wait for writer thread to finish.
1817-
/// </summary>
1818-
internal void Done()
1815+
internal void Dispose()
18191816
{
1820-
// we allow call Done() multiply times.
1817+
// we allow call Dispose() multiply times.
18211818
// For example one time from ProcessRecord() code path,
18221819
// when we detect that process already finished
1823-
// and once from Complete() code path.
1824-
// Even though Done() could be called multiple times,
1825-
// the calls are on the same thread, so there is no race condition.
1820+
// and once from Done() code path.
1821+
// Even though Dispose() could be called multiple times,
1822+
// the calls are on the same thread, so there is no race condition
18261823
if (_pipeline != null)
18271824
{
1828-
var finalResults = _pipeline.End();
18291825
_pipeline.Dispose();
18301826
_pipeline = null;
1831-
// AddTextInputFromFormattedArray can recursively call Done(),
1832-
// if the downstream process already exited.
1833-
// to Prevent it, we first finalize the pipeline and set it to null,
1834-
// then calling the result processing for the last time
1835-
AddTextInputFromFormattedArray(finalResults);
18361827
}
18371828

18381829
if (_xmlSerializer != null)
18391830
{
1840-
_xmlSerializer.Done();
18411831
_xmlSerializer = null;
18421832
}
18431833

1844-
// streamWriter is present, only if we call Start method
1834+
// streamWriter can be null if we didn't call Start method
18451835
if (_streamWriter != null)
18461836
{
18471837
try
@@ -1857,6 +1847,29 @@ internal void Done()
18571847
_streamWriter = null;
18581848
}
18591849
}
1850+
1851+
internal void Done()
1852+
{
1853+
if (_inputFormat == NativeCommandIOFormat.Xml)
1854+
{
1855+
if (_xmlSerializer != null)
1856+
{
1857+
_xmlSerializer.Done();
1858+
}
1859+
}
1860+
else // Text
1861+
{
1862+
// if _pipeline == null, we already called Dispose(),
1863+
// for example, because downstream process finished
1864+
if (_pipeline != null)
1865+
{
1866+
var finalResults = _pipeline.End();
1867+
AddTextInputFromFormattedArray(finalResults);
1868+
}
1869+
}
1870+
1871+
Dispose();
1872+
}
18601873
}
18611874

18621875
#if !CORECLR // There is no GUI application on OneCore, so powershell on OneCore should always have a console attached.

0 commit comments

Comments
 (0)