I got very common thread hang condition although I don't understand why it happens. I have 2 threads: main() and errorStreamReaderThread
In the main thread:
- execute external process via Runtime.exec()
- create new errorStreamReaderThread to consume error output stream
- perform manipulations on input and output streams of external process( execute commands and read output of the commands)
- send exit command to external process
- set interrupt flag for errorStreamReaderThread
- waitFor() for external process to terminate
- return rc of the external process
In the errorStreamReaderThread:
- perform Buffered readLine on error stream while !interruptedFlag &&(line=br.readLine())!=null (errorStream is passed from external process from main)
- perform log error stream
I expect that even though readLine() block on stream - as soon as originator of this stream is finished (external process in this case) this readLine should be interrupted.
Observed behavior - everything works fine 95% of the time - no hangs, both threads: main and error threads are finished and program finished. - about 5% it hangs - main thread is finished (waitFor is returned) - errorStreamReaderThread is blocked on BufferedReader.readLine (in fact the error stream for this buffered reader is no longer exists, since external process is finished)
Additional environment factor is that this java class is executed as oracle java function (it is wrapped in the oracle)
Please let me know what I'm missing here and why I got this readLine hangs on the non existing errorStream
thank you