----- Original Message ----- > From: "Andrey Turbanov" <turban...@gmail.com> > To: "core-libs-dev" <core-libs-dev@openjdk.java.net> > Sent: Monday, January 17, 2022 10:37:04 AM > Subject: Thread.dispatchUncaughtException possible NPE?
> Hello. Hello Andrey, > I see that Thread.dispatchUncaughtException calls > getUncaughtExceptionHandler() which reads volatile field twice: > https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/Thread.java#L1978 > > private volatile UncaughtExceptionHandler uncaughtExceptionHandler; > > public UncaughtExceptionHandler getUncaughtExceptionHandler() { > return uncaughtExceptionHandler != null ? > uncaughtExceptionHandler : group; > } > > private void dispatchUncaughtException(Throwable e) { > getUncaughtExceptionHandler().uncaughtException(this, e); > } > > > I wonder if it's possible to get a NPE here? Another thread could > change uncaughtExceptionHandler between 2 volatile reads. > Or JVM somehow forbid this? yes, it's a bug, the field should be read only once. > > > > > Andrey Turbanov Rémi