Issue of pipe being closed in gradle

I am trying to run gradle in my workspace from last few days and every time i run i get this wired message of pipe being closed.

I tried increasing heap size hoping it will fix but no success yet. went throgh this Broken pipe error when running Gradle test still no luck.

please let me know if anyone have faced such issue earlier.

I tried few

below is Exception i get -

Could not write standard input into: Gradle Test Executor 2.
java.io.IOException: The pipe is being closed
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:315)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82
)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
at org.gradle.process.internal.streams.ExecOutputHandleRunner.run(ExecOu
tputHandleRunner.java:50)
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecut
orImpl$1.run(DefaultExecutorFactory.java:64)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:617)
at java.lang.Thread.run(Thread.java:745)

I get this exception when a sub-process started by Project.exec() closes its input stream before reaching EOF. There seems to be no way to get Gradle to ignore this, even though it is often a non-error condition (for example, using the “head” command in *NIX as the example below indicates, or using the “file” command to determine a file’s type—typically from the first few bytes of content). Here is a build.gradle file that can be used to reproduce in *NIX:

defaultTasks << 'test'

task test << {
   byte[] data = new byte[4096]
   for (int i = 0; i < data.length; ++i) {
      data[i] = 0xff & i
   }

   ByteArrayOutputStream os = new ByteArrayOutputStream()

   int status = exec({
      commandLine "head", "-c", "10"
      ignoreExitValue = true
      standardInput   = new ByteArrayInputStream(data)
      standardOutput  = os
      errorOutput     = os
   }).exitValue

   int n = os.toByteArray().length
   println "Exit status $status and $n bytes of output"
}

The example correctly writes “Exit status 0 and 10 bytes of output” to standard out, but also dumps an exception to standard error as indicated in the OP. Here is my “gradle --version” info:

------------------------------------------------------------
Gradle 2.4
------------------------------------------------------------

Build time:   2015-05-05 08:09:24 UTC
Build number: none
Revision:     5c9c3bc20ca1c281ac7972643f1e2d190f2c943c

Groovy:       2.3.10
Ant:          Apache Ant(TM) version 1.9.4 compiled on April 29 2014
JVM:          1.8.0_45 (Oracle Corporation 25.45-b02)
OS:           Linux 4.0.5-1-ARCH amd64

Here’s a workaround if anyone needs it, though it uses the far more verbose standard Java APIs for threads and processes:

defaultTasks << 'test'

task test << {
   byte[] data = new byte[4096]
   for (int i = 0; i < data.length; ++i) {
      data[i] = 0xff & i
   }

   ByteArrayOutputStream os = new ByteArrayOutputStream()

   Process proc = new ProcessBuilder("head", "-c", "10").start()

   Thread stdInThread = new Thread({
      proc.outputStream.withStream { procIn ->
         try {
            procIn.write(data)
         } catch (IOException ex) {
            // ignore
         }
      }
   })
   Thread stdOutThread = new Thread({
      proc.inputStream.withStream { procOut ->
         byte[] buff = new byte[4096]
         for (;;) {
            int nr = procOut.read(buff)
            if (nr <= 0) {
               break
            } else {
               synchronized (os) {
                  os.write(buff, 0, nr)
               }
            }
         }
      }
   })
   Thread stdErrThread = new Thread({
      proc.errorStream.withStream { procErr ->
         byte[] buff = new byte[4096]
         for (;;) {
            int nr = procErr.read(buff)
            if (nr <= 0) {
               break
            } else {
               synchronized (os) {
                  os.write(buff, 0, nr)
               }
            }
         }
      }
   })

   stdInThread.start()
   stdOutThread.start()
   stdErrThread.start()

   int status = proc.waitFor()
   stdOutThread.join()
   stdErrThread.join()
   stdInThread.join()

   int n = os.toByteArray().length
   println "Exit status $status and $n bytes of output"
}

(By the way, using “stdIn”, “stdOut”, and “stdErr” instead of “procIn”, “procOut”, and “procErr” causes VERY interesting errors relating to streams already being closed; it seems that local variables aren’t quite the highest priority in terms of the lexical namespace—a DSL/AST problem…?)

I suggest this be moved to the Bugs section.