JLine blocked when reading after end-of-file (original) (raw)
While investigating an Apache Hive issue, I noticed that executing a script file blocks when using a JLine 3.x version >= 3.30.8. I managed to reproduce it with the latest jline-3.x branch (b832cde) with a unit test:
In org.jline.reader.impl.LineReaderTest#terminalLineInfiniteLoop, add the check
assertThrows(EndOfFileException.class, lineReader::readLine);
at the end of the test method.
It would also be nice to add explicit tests for TerminalProvider[exec] and TerminalProvider[jni]. I've implemented such tests in my fork. I had to add the JVM arg --enable-native-access=ALL-UNNAMED so that the provider is actually loaded.
I've debugged it a bit and found the following:
- In org.jline.terminal.impl.AbstractPty.PtyInputStream,
int r = in.read();returns -1 at some point - Only r >= 0 are returned directly; otherwise we check for the timeout; if it is not timed out, we execute the loop again
- Therefore a value of r=-1 will not be returned by org.jline.terminal.impl.AbstractPty.PtyInputStream#read; that might be by design of the PtyInputStream, so maybe another kind of InputStream needs to be used instead.