Skip to content

Commit 22c36ad

Browse files
authored
fix processex.cs possible problems (#22347)
- `_output ` May be executed in synchronous and asynchronous situations - Process Is a nullable
1 parent 78d6ce2 commit 22c36ad

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/Shared/Process/ProcessEx.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,11 @@ public void WaitForExit(bool assertSuccess, TimeSpan? timeSpan = null)
197197
var exited = Exited.Wait(timeSpan.Value);
198198
if (!exited)
199199
{
200-
_output.WriteLine($"The process didn't exit within the allotted time ({timeSpan.Value.TotalSeconds} seconds).");
200+
lock (_testOutputLock)
201+
{
202+
_output.WriteLine($"The process didn't exit within the allotted time ({timeSpan.Value.TotalSeconds} seconds).");
203+
}
204+
201205
_process.Dispose();
202206
}
203207
else if (assertSuccess && _process.ExitCode != 0)
@@ -227,13 +231,16 @@ public void Dispose()
227231
_process.KillTree();
228232
}
229233

230-
_process.CancelOutputRead();
231-
_process.CancelErrorRead();
234+
if (_process != null)
235+
{
236+
_process.CancelOutputRead();
237+
_process.CancelErrorRead();
232238

233-
_process.ErrorDataReceived -= OnErrorData;
234-
_process.OutputDataReceived -= OnOutputData;
235-
_process.Exited -= OnProcessExited;
236-
_process.Dispose();
239+
_process.ErrorDataReceived -= OnErrorData;
240+
_process.OutputDataReceived -= OnOutputData;
241+
_process.Exited -= OnProcessExited;
242+
_process.Dispose();
243+
}
237244
}
238245
}
239246
}

0 commit comments

Comments
 (0)