Skip to content
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

8332084: Ensure JdkConsoleImpl.restoreEcho visibility in a shutdown hook #19184

Closed
wants to merge 12 commits into from
26 changes: 18 additions & 8 deletions src/java.base/share/classes/jdk/internal/io/JdkConsoleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ public char[] readPassword(Locale locale, String format, Object ... args) {
synchronized(readLock) {
installShutdownHook();
try {
restoreEcho = echo(false);
synchronized(restoreEchoLock) {
restoreEcho = echo(false);
}
} catch (IOException x) {
throw new IOError(x);
}
Expand All @@ -140,8 +142,11 @@ public char[] readPassword(Locale locale, String format, Object ... args) {
ioe = new IOError(x);
} finally {
try {
if (restoreEcho)
restoreEcho = echo(true);
synchronized(restoreEchoLock) {
if (restoreEcho) {
restoreEcho = echo(true);
}
}
} catch (IOException x) {
if (ioe == null)
ioe = new IOError(x);
Expand All @@ -154,7 +159,7 @@ public char[] readPassword(Locale locale, String format, Object ... args) {
if (reader instanceof LineReader lr) {
lr.zeroOut();
}
} catch (IOException x) {
} catch (IOException _) {
// ignore
}
throw ioe;
Expand All @@ -178,13 +183,15 @@ private void installShutdownHook() {
new Runnable() {
public void run() {
try {
if (restoreEcho) {
echo(true);
synchronized(restoreEchoLock) {
if (restoreEcho) {
echo(true);
}
}
} catch (IOException x) { }
} catch (IOException _) { }
}
});
} catch (IllegalStateException e) {
} catch (IllegalStateException _) {
// shutdown is already in progress and readPassword is first used
// by a shutdown hook
}
Expand All @@ -209,6 +216,8 @@ public Charset charset() {
private final Charset charset;
private final Object readLock;
private final Object writeLock;
// Must not block while holding this. It is used in the shutdown hook.
private final Object restoreEchoLock;
private final Reader reader;
private final Writer out;
private final PrintWriter pw;
Expand Down Expand Up @@ -379,6 +388,7 @@ public JdkConsoleImpl(Charset charset) {
this.charset = charset;
readLock = new Object();
writeLock = new Object();
restoreEchoLock = new Object();
out = StreamEncoder.forOutputStreamWriter(
new FileOutputStream(FileDescriptor.out),
writeLock,
Expand Down