-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warn against Process.StandardOutput and Process.StandardError #114
Comments
Using async Task ReadOutput(StreamReader reader)
{
string line;
while ((line = await reader.ReadLineAsync()) != null)
{
// Do stuff with line.
}
}
Process process = ...
var task = Task.WhenAll(
ReadOutput(process.StandardOutput),
ReadOutput(process.StandardError));
process.WaitForExit();
await task; |
This is not safe at all, you have called WaitForExit() before reading from StandardOutput, when the stdio buffer fills up it will wait for you to read from the buffer but you will not because you are waiting for the process to extit => deadlock. |
Oops, out of order (fixed). I usually create a |
This is why we need a warning, you can't even get the code right the first time when discussing how to get the code right.... |
Though using the events has it's own "gotcha's". You need to call |
There are a lot of ways to do it wrong, and also ways to do it right, both of which use these properties. I'm thinking perhaps we should simply warn against calling Would that suffice for the majority case? |
It is best practice to read process IO asynchronously to avoid deadlocks using OutputDataReceived/BeginOutputReadLine and ErrorDataRecieved/BeginErrorReadLine.
Warn against any access to Process.StandardOutput and Process.StandardError, suggesting to use the asynchronous operations instead.
The text was updated successfully, but these errors were encountered: