From 5fb03bb094d0f4086bd1a405070fbc6482d6dbb7 Mon Sep 17 00:00:00 2001 From: Paul Nikonowicz Date: Wed, 17 May 2017 18:00:35 -0400 Subject: [PATCH] [Issue #218] StdOut was not being redirected properly and was causing the child process to hang. --- src/Core/WinSWCore/Util/ProcessHelper.cs | 6 +-- src/Test/winswTests/Util/ProcessHelperTest.cs | 41 ++++++++++++++++--- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/Core/WinSWCore/Util/ProcessHelper.cs b/src/Core/WinSWCore/Util/ProcessHelper.cs index 03327aa..a51b5dc 100644 --- a/src/Core/WinSWCore/Util/ProcessHelper.cs +++ b/src/Core/WinSWCore/Util/ProcessHelper.cs @@ -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) { diff --git a/src/Test/winswTests/Util/ProcessHelperTest.cs b/src/Test/winswTests/Util/ProcessHelperTest.cs index 9bff216..1bbcf7d 100644 --- a/src/Test/winswTests/Util/ProcessHelperTest.cs +++ b/src/Test/winswTests/Util/ProcessHelperTest.cs @@ -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(); + for(var i = start; i