Skip to content
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

Read stderr asynchronously to prevent hang if many errors logged #70

Merged
merged 1 commit into from
Nov 15, 2023
Merged
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
14 changes: 7 additions & 7 deletions Rubjerg.Graphviz/GraphvizCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;

namespace Rubjerg.Graphviz;

Expand Down Expand Up @@ -64,7 +65,11 @@ public static (byte[] stdout, string stderr) Exec(Graph input, string format = "
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardError = true;

StringBuilder stderr = new StringBuilder();
process.ErrorDataReceived += (_, e) => stderr.AppendLine(e.Data);

_ = process.Start();
process.BeginErrorReadLine();

// Write to stdin
using (StreamWriter sw = process.StandardInput)
Expand All @@ -78,12 +83,6 @@ public static (byte[] stdout, string stderr) Exec(Graph input, string format = "
stdout = memoryStream.ToArray();
}

// Read from stderr
string stderr;
using (StreamReader sr = process.StandardError)
// Let's use unix line endings for consistency with stdout
stderr = sr.ReadToEnd().Replace("\r\n", "\n");

process.WaitForExit();

if (process.ExitCode != 0)
Expand All @@ -94,7 +93,8 @@ public static (byte[] stdout, string stderr) Exec(Graph input, string format = "
else
{
// Process completed successfully.
return (stdout, stderr);
// Let's use unix line endings for consistency with stdout
return (stdout, stderr.ToString().Replace("\r\n", "\n"));
}
}
}
Loading