Skip to content

Commit d4d623b

Browse files
committed
flush output buffers for child process to stop hangs when large outputs written
1 parent e94b772 commit d4d623b

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

Tocsoft.GraphQLCodeGen.Cli/Program.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ public static void MainApplication(CommandLineApplication app)
101101

102102
IEnumerable<CodeGeneratorSettings> settings = settingsLoader.GenerateSettings(loaderSettings, sourceArgument.Values);
103103
HashSet<string> generatedFiles = new HashSet<string>();
104-
var sw = Stopwatch.StartNew();
105-
106104
var semaphore = new SemaphoreSlim(Environment.ProcessorCount, Environment.ProcessorCount);
107105
var tasks = settings.Select(Generate).ToList();
108106

@@ -128,7 +126,6 @@ Task Generate(CodeGeneratorSettings s)
128126
}
129127

130128
await Task.WhenAll(tasks);
131-
sw.Stop();
132129

133130
if (inMsbuildMode)
134131
{

Tocsoft.GraphQLCodeGen.MsBuild/GenerateGraphQLClient.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public class GenerateGraphQLClient : Task
3232

3333
public override bool Execute()
3434
{
35-
// Debugger.Launch();
3635
// list all source settings files
3736
// upldate this to include *.gql/*.graphql if/when i update it to support metadata in config file
3837
IEnumerable<ITaskItem> settings =
@@ -76,13 +75,34 @@ public override bool Execute()
7675
try
7776
{
7877
Directory.CreateDirectory(tempFolder);
79-
Process process = Process.Start(new ProcessStartInfo(realexe, arguments)
78+
Process process = new Process
8079
{
81-
UseShellExecute = false,
82-
CreateNoWindow = true,
83-
RedirectStandardOutput = true,
84-
RedirectStandardError = true
85-
});
80+
StartInfo = new ProcessStartInfo(realexe, arguments)
81+
{
82+
UseShellExecute = false,
83+
CreateNoWindow = true,
84+
RedirectStandardOutput = true,
85+
RedirectStandardError = true
86+
},
87+
EnableRaisingEvents = true
88+
};
89+
90+
// we have to consume the output buffers otherwise they fill up
91+
// and the child process hangs.
92+
StringBuilder standardout = new StringBuilder();
93+
StringBuilder errorout = new StringBuilder();
94+
process.OutputDataReceived += (s, e) =>
95+
{
96+
standardout.AppendLine(e.Data);
97+
};
98+
process.ErrorDataReceived += (s, e) =>
99+
{
100+
errorout.AppendLine(e.Data);
101+
};
102+
103+
process.Start();
104+
process.BeginOutputReadLine();
105+
process.BeginErrorReadLine();
86106

87107
// default timeout of 5 seconds
88108
int timeout = 5000;
@@ -106,7 +126,7 @@ public override bool Execute()
106126
return false;
107127
}
108128

109-
var errors = (process.StandardError.ReadToEnd() ?? "").Trim();
129+
var errors = errorout.ToString().Trim();
110130
if (!string.IsNullOrWhiteSpace(errors))
111131
{
112132
var errorLines = errors
@@ -139,7 +159,7 @@ public override bool Execute()
139159
return false;
140160
}
141161

142-
string filesoutput = process.StandardOutput.ReadToEnd();
162+
string filesoutput = standardout.ToString();
143163
this.Log.LogMessage(MessageImportance.High, "generated = {0}", filesoutput);
144164

145165
IEnumerable<string> files = filesoutput
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
4-
<GraphQLCodeGenAssemblyFolder Condition=" '$(MSBuildRuntimeType)' == 'Core'">$(MSBuildThisFileDirectory)bin\Debug\netcoreapp1.0</GraphQLCodeGenAssemblyFolder>
5-
<GraphQLCodeGenAssemblyFolder Condition=" '$(MSBuildRuntimeType)' != 'Core'">$(MSBuildThisFileDirectory)bin\Debug\net461</GraphQLCodeGenAssemblyFolder>
6-
<GraphQLCodeGenRootCliFolder>$(MSBuildThisFileDirectory)..\Tocsoft.GraphQLCodeGen.Cli\bin\Debug</GraphQLCodeGenRootCliFolder>
4+
<GraphQLCodeGenConfiguration>Debug</GraphQLCodeGenConfiguration>
5+
<GraphQLCodeGenAssemblyFolder Condition=" '$(MSBuildRuntimeType)' == 'Core'">$(MSBuildThisFileDirectory)bin\$(GraphQLCodeGenConfiguration)\netcoreapp1.0</GraphQLCodeGenAssemblyFolder>
6+
<GraphQLCodeGenAssemblyFolder Condition=" '$(MSBuildRuntimeType)' != 'Core'">$(MSBuildThisFileDirectory)bin\$(GraphQLCodeGenConfiguration)\net461</GraphQLCodeGenAssemblyFolder>
7+
<GraphQLCodeGenRootCliFolder>$(MSBuildThisFileDirectory)..\Tocsoft.GraphQLCodeGen.Cli\bin\$(GraphQLCodeGenConfiguration)</GraphQLCodeGenRootCliFolder>
78
</PropertyGroup>
89
<Import Project="$(MSBuildThisFileDirectory)\build\Tocsoft.GraphQLCodeGen.MsBuild.props"/>
910
</Project>

0 commit comments

Comments
 (0)