1

We have a 32-bit process A and it has to start another java jar which has to run in 64 bit process. They communicate though a socket. This system works fine.

But in some rare cases we see the second process (B) hangs until we close the process (A) that launches it. While I was debugging this issue I wanted to read input stream of process B to see what messages are being outputted and seen reading from the input stream of process B solved the issue. So we span up a dumb thread which solely reads input stream of the process B and everything worked fine.

The issue was very weird but we had to move on. We thought it has something to do with console output buffer size or something like that. But we have seen this issue appeared on a newly installed machine although we read the input stream of the started process (B).

Although it happens very rare we want to be absolutely sure why this happens. What might be the reason? Is there a default setting that we are not aware of? Have you encountered such a case before?

Quick summary:

  • process A runs in a 32-bit process
  • process B runs in a 64-bit process
  • process A starts process B by issuing Runtime.exec by pointing to the 64-bit javaw.exe
  • process B hangs until we close process A
  • we see process B appears in the task manager
idursun
  • 6,261
  • 1
  • 37
  • 51

2 Answers2

2

You need to consume both the stadout and stderr from a spawned process, and you need to do this simultaneously, otherwise the spawned process can block waiting for the parent process to consume that output.

More info here.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • I've seen this article before asking the question but wanted to be sure if it's the only possible reason. Reading from the error stream was the only missing piece in our case. – idursun Nov 22 '12 at 07:05
0

I had process hang issue. For me, I wanted stdout and stderr logs from the spawned subprocess. the spawned subprocess was getting hang due to use of read blocking call 'readLine()' from parent process. Following code was causing problem for me.

   BufferedReader processInputReader =  new BufferedReader(new InputStreamReader(process.getInputStream()));
       while ((line = processInputReader.readLine()) != null){
           bw.write(line+"\n"); //write to temp file 
       }
       processInputReader.close();

       //get error stream
       processInputReader =  new BufferedReader(new InputStreamReader(process.getErrorStream()));
       while ((line = processInputReader.readLine()) != null){
           bw.write(line+"\n"); //write to temp file 
       }
       processInputReader.close();

I have changed stream read logic by referring to https://ostermiller.org/utils/src/ExecHelper.java.html. Now I am not seeing hang issue anymore.

dnyanesh
  • 101
  • 1
  • 6