[ 
https://issues.apache.org/jira/browse/GROOVY-7414?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14532417#comment-14532417
 ] 

Paul King edited comment on GROOVY-7414 at 5/7/15 10:42 AM:
------------------------------------------------------------

I agree those methods and the underlying classes aren't perfect. Do you have 
some improvements in mind?

Here is a typical use case:
{code}
def outputBuffer = new StringBuffer()
def errorBuffer  = new StringBuffer()

zipProcess   = 'gzip -c'.execute()
unzipProcess = 'gunzip -c'.execute()

unzipProcess.consumeProcessOutput(outputBuffer, errorBuffer)
zipProcess.consumeProcessErrorStream(errorBuffer)

zipProcess | unzipProcess
zipProcess.withWriter { writer ->
  writer << 'Hello World'
}
unzipProcess.waitForOrKill(1000)

println 'Output: ' + outputBuffer   // Output: Hello World
println 'Error : ' + errorBuffer
{code}
The {{withWriter}} method forks a new thread and buffers the stream. What would 
you prefer to see?



was (Author: paulk):
I agree those methods and the underlying classes aren't perfect. Do you have 
some improvements in mind?

Here is a typical use case:
{code}
def outputBuffer = new StringBuffer()
def errorBuffer  = new StringBuffer()

zipProcess   = 'gzip -c'.execute()
unzipProcess = 'gunzip -c'.execute()

unzipProcess.consumeProcessOutput(outputBuffer, errorBuffer)
zipProcess.consumeProcessErrorStream(errorBuffer)

zipProcess | unzipProcess
zipProcess.withWriter { writer ->
  writer << 'Hello World '
}
unzipProcess.waitForOrKill(1000)

println 'Output: ' + outputBuffer
println 'Error : ' + errorBuffer
{code}
The {{withWriter}} method forks a new thread and buffers the stream. What would 
you prefer to see?


> Process output not always consumed before waitFor() returns
> -----------------------------------------------------------
>
>                 Key: GROOVY-7414
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7414
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-jdk
>    Affects Versions: 2.4.3
>         Environment: Linux junk 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt9-2 
> (2015-04-13) x86_64 GNU/Linux running in a VirtualBox VM
>            Reporter: Sergey Gromov
>            Assignee: Guillaume Laforge
>
> Sometimes the {{Process.consumeProcessOutput(Appendable, Appendable)}} and 
> {{Process.waitFor()}} combination does not work as expected. More 
> specifically, sometimes {{waitFor()}} returns before all input is actually 
> written into {{Appendable}}.
> The following script reproduces the problem:
> {code:title=bug.gy}
> 100.times {
>         def out = new StringBuilder()
>         def err = new StringBuilder()
>         def p = 'echo stuff'.execute()
>         p.consumeProcessOutput(out, err)
>         p.waitFor()
>         def s = out.toString()
>         if (s != 'stuff\n') {
>                 println 'Bug!'
>                 println 'OUT: ' + s.inspect()
>         }
> }
> {code}
> The ability to reproduce this issue seems to depend on the system 
> performance. E.g. I can easily reproduce it in my virtual Linux box. The 
> above script fails 2 or 3 times on every launch. OTOH it's quite problematic 
> to reproduce on my host Windows machine even though I increased the number of 
> tries to 1000. Nevertheless I have reproduced it on Windows, too, a couple of 
> times.
> I think this is major because I'm not aware of any viable workaround. The 
> only thing that works is to sleep a bit after waitFor() which is often 
> unacceptable, and it's impossible to guess for how long to sleep.
> Note that {{Process.consumeProcessOutput(OutputStream, OutputStream)}} 
> exhibits the same broken behavior.
> Also note that {{Process.waitForProcessOutput(arg, arg)}} methods work 
> flawlessly.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to