Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit 5fc0e49

Browse files
authored
Don't write stdout and stderr in parallel (#82)
1 parent 57d429d commit 5fc0e49

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

src/Valet/Services/ProcessService.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Valet.Services;
55

66
public class ProcessService : IProcessService
77
{
8+
private static readonly object ConsoleWriterLock = new();
9+
810
public async Task RunAsync(
911
string filename,
1012
string arguments,
@@ -89,11 +91,21 @@ private static void ReadStream(StreamReader reader, bool output, CancellationTok
8991
{
9092
while (!ctx.IsCancellationRequested)
9193
{
92-
int current;
93-
while ((current = reader.Read()) >= 0)
94+
int current = reader.Read();
95+
96+
if (current >= 0)
9497
{
95-
if (output)
96-
Console.Write((char)current);
98+
lock (ConsoleWriterLock)
99+
{
100+
while (current >= 0)
101+
{
102+
if (output)
103+
{
104+
Console.Write((char)current);
105+
}
106+
current = reader.Read();
107+
}
108+
}
97109
}
98110
}
99111
}, ctx);

0 commit comments

Comments
 (0)