Skip to content

Commit 71327c0

Browse files
author
John Luo
authored
Try synchronizing dispose and output (#20341)
1 parent 85b1a73 commit 71327c0

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/Shared/Process/ProcessEx.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ internal class ProcessEx : IDisposable
2626
private readonly StringBuilder _stderrCapture;
2727
private readonly StringBuilder _stdoutCapture;
2828
private readonly object _pipeCaptureLock = new object();
29+
private readonly object _testOutputLock = new object();
2930
private BlockingCollection<string> _stdoutLines;
3031
private TaskCompletionSource<int> _exited;
3132
private CancellationTokenSource _stdoutLinesCancellationSource = new CancellationTokenSource(TimeSpan.FromMinutes(5));
33+
private bool _disposed = false;
3234

3335
public ProcessEx(ITestOutputHelper output, Process proc)
3436
{
@@ -135,7 +137,13 @@ private void OnErrorData(object sender, DataReceivedEventArgs e)
135137
_stderrCapture.AppendLine(e.Data);
136138
}
137139

138-
_output.WriteLine("[ERROR] " + e.Data);
140+
lock (_testOutputLock)
141+
{
142+
if (!_disposed)
143+
{
144+
_output.WriteLine("[ERROR] " + e.Data);
145+
}
146+
}
139147
}
140148

141149
private void OnOutputData(object sender, DataReceivedEventArgs e)
@@ -150,7 +158,13 @@ private void OnOutputData(object sender, DataReceivedEventArgs e)
150158
_stdoutCapture.AppendLine(e.Data);
151159
}
152160

153-
_output.WriteLine(e.Data);
161+
lock (_testOutputLock)
162+
{
163+
if (!_disposed)
164+
{
165+
_output.WriteLine(e.Data);
166+
}
167+
}
154168

155169
if (_stdoutLines != null)
156170
{
@@ -204,6 +218,11 @@ private static string GetNugetPackagesRestorePath() => (string.IsNullOrEmpty(Env
204218

205219
public void Dispose()
206220
{
221+
lock (_testOutputLock)
222+
{
223+
_disposed = true;
224+
}
225+
207226
if (_process != null && !_process.HasExited)
208227
{
209228
_process.KillTree();

0 commit comments

Comments
 (0)