JDK-8190949 : Infinite loop of ConcurrentHashMap in recursive scene
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util.concurrent
  • Affected Version: 8u141
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: os_x
  • CPU: x86
  • Submitted: 2017-11-06
  • Updated: 2017-11-09
  • Resolved: 2017-11-08
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Darwin mysh.local 16.6.0 Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELEASE_X86_64 x86_64

A DESCRIPTION OF THE PROBLEM :
ConcurrentHashMap infinite loops in one condition


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
run the code

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
print result and exit
ACTUAL -
infinite loop

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CommonTest {
    private static Map<Integer, Integer> cache = new ConcurrentHashMap<>();

    public static void main(String[] args) {
        System.out.println(fibonacci(80));
    }

    public static int fibonacci(Integer i) {
        if (i == 0 || i == 1) {
            return i;
        }
        return cache.computeIfAbsent(i, (key) -> {
            System.out.println("Compute fibonacci " + key);
            return fibonacci(key - 1) + fibonacci(key - 2);
        });
    }
}


---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
replace computeIfAbsent with get-set



Comments
If you follow the chain of duplicates you get to a bulk update in JDK 9. It's unclear whether the reported problem is actually fixed or is a usage error. JDK-8074374 has a better description of the underlying problem
09-11-2017