Skip to content

Commit 6ac5ec1

Browse files
authored
fix: NonBlockingInputStream keeps thread alive after EOF (fixes #1879) (#1903)
NonBlockingInputStreamImpl was exiting its reading thread permanently when the underlying stream returned -1. On terminal fds, a -1 from FileInputStream.read() (native read() returning 0 bytes) is not permanent EOF and can occur when VMIN=0. The thread now stays alive and can service subsequent reads, matching NonBlockingReaderImpl behavior.
1 parent 1ef5bed commit 6ac5ec1

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

terminal/src/main/java/org/jline/utils/NonBlockingInputStreamImpl.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,6 @@ private void run() {
217217
threadIsReading = false;
218218
notify();
219219
}
220-
221-
// If end of stream, exit the loop thread
222-
if (byteRead < 0) {
223-
return;
224-
}
225220
}
226221
} catch (Throwable t) {
227222
Log.warn("Error in NonBlockingInputStream thread", t);

terminal/src/test/java/org/jline/utils/NonBlockingTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,27 @@ void testPumpBulkReadLargePayload() throws IOException {
326326
assertArrayEquals(payload, result);
327327
assertTrue(sawBulk, "At least one bulk read (n > 1) should have occurred");
328328
}
329+
330+
@Test
331+
void testNonBlockingInputStreamRecoverAfterEof() throws IOException {
332+
java.io.InputStream in = new java.io.InputStream() {
333+
private int callCount = 0;
334+
335+
@Override
336+
public int read() {
337+
int n = callCount++;
338+
if (n == 0) {
339+
return -1;
340+
}
341+
return 'A';
342+
}
343+
};
344+
NonBlockingInputStreamImpl nbis = new NonBlockingInputStreamImpl("test", in);
345+
try {
346+
assertEquals(-1, nbis.read(200));
347+
assertEquals('A', nbis.read(200));
348+
} finally {
349+
nbis.close();
350+
}
351+
}
329352
}

0 commit comments

Comments
 (0)