Thread.dispatchUncaughtException possible NPE?

Andrey Turbanov turbanoff at gmail.com
Mon Jan 17 09:37:04 UTC 2022


Hello.
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?




Andrey Turbanov


More information about the core-libs-dev mailing list