Skip to content

8358552: EndOfFileException in System.in.read() and IO.readln() etc. in JShell #25713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,8 @@ private synchronized String doReadUserLine(String prompt, Character mask) throws
return in.readLine(prompt.replace("%", "%%"), mask);
} catch (UserInterruptException ex) {
throw new InterruptedIOException();
} catch (EndOfFileException ex) {
return null; // Signal that Ctrl+D or similar happened
} finally {
in.setParser(prevParser);
in.setHistory(prevHistory);
Expand All @@ -1051,7 +1053,11 @@ private synchronized String doReadUserLine(String prompt, Character mask) throws

public char[] readPassword(String prompt) throws IOException {
//TODO: correct behavior w.r.t. pre-read stuff?
return doReadUserLine(prompt, '\0').toCharArray();
String line = doReadUserLine(prompt, '\0');
if (line == null) {
throw new UserInterruptException("");
}
return line.toCharArray();
}

@Override
Expand Down
20 changes: 19 additions & 1 deletion test/langtools/jdk/jshell/InputUITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/*
* @test
* @bug 8356165
* @bug 8356165 8358552
* @summary Check user input works properly
* @modules
* jdk.compiler/com.sun.tools.javac.api
Expand All @@ -38,6 +38,7 @@
* @run testng/othervm -Dstderr.encoding=UTF-8 -Dstdin.encoding=UTF-8 -Dstdout.encoding=UTF-8 InputUITest
*/

import java.util.Map;
import java.util.function.Function;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -67,4 +68,21 @@ public void testUserInputWithSurrogates() throws Exception {
}, false);
}

public void testCloseInputSinkWhileReadingUserInputSimulatingCtrlD() throws Exception {
var snippets = Map.of(
"System.in.read()", " ==> 110",
"System.console().reader().read()", " ==> 110",
"System.console().readLine()", " ==> null",
"System.console().readPassword()", " ==> null",
"IO.readln()", " ==> \"null\""
// TODO , "System.in.readAllBytes()", " ==> " // ... hangs forever
);
for (var snippet : snippets.entrySet()) {
doRunTest((inputSink, out) -> {
inputSink.write(snippet.getKey() + "\n");
inputSink.close(); // Does not work: inputSink.write("\u0004"); // CTRL + D
waitOutput(out, patternQuote(snippet.getValue()), patternQuote("EndOfFileException"));
}, false);
}
}
}
8 changes: 8 additions & 0 deletions test/langtools/jdk/jshell/UITesting.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,19 @@ protected void doRunTest(Test test, boolean setUserInput) throws Exception {
});

Writer inputSink = new OutputStreamWriter(input.createOutput(), StandardCharsets.UTF_8) {
boolean closed = false;
@Override
public void write(String str) throws IOException {
if (closed) return; // prevents exception thrown due to closed writer
super.write(str);
flush();
}

@Override
public void close() throws IOException {
super.close();
closed = true;
}
};

runner.start();
Expand Down