[Issue #218] StdOut was not being redirected properly and was causing the child process to hang.

pull/220/head
Paul Nikonowicz 2017-05-17 18:00:35 -04:00 committed by bot
parent 33dcc39539
commit 5fb03bb094
2 changed files with 39 additions and 8 deletions

View File

@ -138,9 +138,9 @@ namespace winsw.Util
ps.WorkingDirectory = workingDirectory ?? ps.WorkingDirectory;
ps.CreateNoWindow = false;
ps.UseShellExecute = false;
ps.RedirectStandardInput = true; // this creates a pipe for stdin to the new process, instead of having it inherit our stdin.
ps.RedirectStandardOutput = true;
ps.RedirectStandardError = true;
ps.RedirectStandardInput = false;
ps.RedirectStandardOutput = false;
ps.RedirectStandardError = false;
if (envVars != null)
{

View File

@ -4,9 +4,10 @@ using NUnit.Framework;
using winsw;
using System.IO;
using winsw.Util;
namespace winswTests.Util
{
using System.Collections.Generic;
namespace winswTests.Util
{
[TestFixture]
class ProcessHelperTest
@ -47,5 +48,35 @@ namespace winswTests.Util
Assert.That(!envVars.ContainsKey("computername"), "Test error: the environment parsing logic is case-insensitive");
}
}
}
[Test]
public void ShouldNotHangWhenWritingLargeStringToStdOut()
{
var tmpDir = FilesystemTestHelper.CreateTmpDirectory();
String scriptFile = Path.Combine(tmpDir, "print_lots_to_stdout.bat");
var lotsOfStdOut = string.Join("", _Range(1,1000));
File.WriteAllText(scriptFile, string.Format("echo \"{0}\"", lotsOfStdOut));
Process proc = new Process();
var ps = proc.StartInfo;
ps.FileName = scriptFile;
ProcessHelper.StartProcessAndCallbackForExit(proc);
var exited = proc.WaitForExit(5000);
if (!exited)
{
Assert.Fail("Process " + proc + " didn't exit after 5 seconds");
}
}
private string[] _Range(int start, int limit)
{
var range = new List<string>();
for(var i = start; i<limit; i++)
{
range.Add(i.ToString());
}
return range.ToArray();
}
}
}