title: java Process' inputStream stuck
body: <p>Here's my scenario:</p>
  
  <ol>
  <li>process A spawns child process B and spins threads to drain B's outputs.</li>
  <li>process B spawns daemon process C and drains its outputs, too.</li>
  <li>process B finishes, daemon process still lives.</li>
  <li>process A finds out that process B exited via process.waitFor(). However, it's stuck on reading the input streams of process B. It's because B has started a daemon. The input stream receives EOF only when the process C exits.</li>
  </ol>
  
  <p>This only happens on Windows. I'm using the ProcessBuilder. Here're the solutions I came up with and I'd like to hear your feedback as none of the solutions I really like:</p>
  
  <ol>
  <li>I can use jna to spawn the daemon process C. This way I can create a process that is 'detached enough' and process A is not stuck on draining the streams from B. It works but I'm not very keen on that solution because it means some native code (and lots of that since I'm keen on consuming the inputs). Some inspiration how to do it via JNA is here: <a href="http://yajsw.sourceforge.net" rel="nofollow">http://yajsw.sourceforge.net</a> (however it contains way more stuff than mere process starting).</li>
  <li>Run on jre7. Jdk7 brings some new goodies to the ProcessBuilder, e.g. inheritIO() stuff that also solves my problem. Apparently, when inheritIO() is turned on, I can simply close all streams in the daemon process C (which I do anyway because it'a daemon) and that solves the problem. However, I need to run on jre5+</li>
  <li>Close the System.out and System.err in process B <em>before</em> spawning the daemon process C. Again, it solves the problem but I really need those streams to work in process B as I write useful stuff to them. No good. I hoped I could take advantage of this characteristic by placing some kind of a bootstrap process between B &amp; C but that didn't solve the problem.</li>
  <li>I don't have that problem on linux so could I only run on linux? No, I can't.</li>
  <li>Made the process A drain the outputs of process B in a non-blocking way. This somewhat works but it's not convenient. E.g. inputStream.read() is not interruptible. I could use inputStream.available() but it doesn't distinguish between EOF and zero-bytes-available. So the solution is only good if process A is never interested in B's output EOF. Also, this solution seems to be more CPU intensive and generally... feels awkward and not really bullet proof.</li>
  <li>Run process C in a --dry-run mode where it just checks if it can be started. It tries to start, sends welcome message and exits. It's no longer long-running so it will not block reads. Process B can gain enough confidence that C can be started and we can use relatively simple JNA code to spawn detached process without consuming its outputs (it's the consuming of outputs makes the JNA-related code messy and heavyweight). The only problem is that we no longer consumer process' C outputs but it can be solved by making C write to a well known file that process B can consume. This solution is more like a big and ugly workaround but kind of workable for us. Anyways, we are trying the solution 1) at the moment.</li>
  </ol>
  
  <p>I would really appreciate any hints!</p>