JDK-8351045 : ClassValue::remove cannot ensure computation observes up-to-date state
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang.invoke
  • Affected Version: 25
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2025-03-03
  • Updated: 2025-03-27
  • Resolved: 2025-03-11
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 25
25 b14Fixed
Related Reports
Causes :  
Description
ClassValue::remove does not halt ongoing computations. As a result, a later-installed value may not observe the state changes that happens-before the removal call if the computation began before the remove call, which is almost impossible to guarantee without external synchronization, defeating the point of ClassValue.

See example:

AtomicInteger input = new AtomicInteger(0);
ClassValue<Integer> cv = new ClassValue<>() {
    @Override
    protected Integer computeValue(Class<?> type) {
        // must get early to represent using outdated input
        int v = input.get();
        try {
            Thread.sleep(COMPUTE_TIME_MILLIS);
        } catch (InterruptedException ex) {
            throw new RuntimeException(ex);
        }
        return v;
    }
};
var innocuous = Thread.startVirtualThread(() -> cv.get(int.class));
var refreshInput = Thread.startVirtualThread(() -> {
    input.incrementAndGet();
    cv.remove(int.class); // Let's recompute with updated inputs!
});
try {
    innocuous.join();
    refreshInput.join();
} catch (InterruptedException ex) {
    throw new RuntimeException(ex);
}
assertEquals(1, input.get(), "input not updated");
assertEquals(1, cv.get(int.class), "CV not using up-to-date input");
Comments
Changeset: e71f3274 Branch: master Author: Chen Liang <liach@openjdk.org> Date: 2025-03-11 19:51:47 +0000 URL: https://git.openjdk.org/jdk/commit/e71f3274a9de4006bc8cdfe4ba1bd12a8867a11a
11-03-2025

A pull request was submitted for review. Branch: master URL: https://git.openjdk.org/jdk/pull/23866 Date: 2025-03-03 15:24:05 +0000
03-03-2025