RFR: 8304878: ConcurrentModificationException in javadoc tool
Jonathan Gibbons
jjg at openjdk.org
Mon Jun 5 21:46:51 UTC 2023
On Mon, 5 Jun 2023 10:47:59 GMT, Pavel Rappo <prappo at openjdk.org> wrote:
> Although its name might suggest otherwise, java.util.ConcurrentModificationException (CME) is not necessarily thrown in multithreaded context. For example, a map might throw CME if it detects modification in a midst of an operation which is supposed to be exclusive:
>
> jshell> Map<Integer, Integer> m = new HashMap<>();
> ...> m.computeIfAbsent(0, key -> m.put(1, 2));
> m ==> {}
> | Exception java.util.ConcurrentModificationException
> | at HashMap.computeIfAbsent (HashMap.java:1229)
> | at (#2:1)
>
> Somewhat similar happens in the reproducer: a computation of a tree path for a package element eventually modifies the very map that the result of that computation is to be put in. The map detects that and throws CME.
>
> Here are more details. The tree path is to be found for the `bar` package element. The tree path is computed and put into the cache in one go (HashMap.computeIfAbsent). Usually that works fine, but not this time. Because the `bar` package resides in an implicitly referred module (test.bar), that package is discovered late. The computation triggers the `bar` package completion/parsing, which eventually calls back to JavadocEnter.visitTopLevel, which in turn puts an alternatively computed tree path for the `bar` package into that same cache. When the call stack returns to computeIfAbsent, that cache modification is detected and CME is thrown.
>
> I can see three obvious ways of fixing this bug:
>
> 1. Replace computeIfAbsent with an equivalent combination of simpler methods.
> 2. Use a map whose computeIfAbsent can withstand non-exclusive modifications.
> 3. Restructure the code so it avoids such modifications altogether.
>
> (3) Is quite risky/complicated and not really needed at this time. (2) While it's unlikely to have noticeable performance overhead, ConcurrentHashMap feels like symptomatic treatment that is also out of place given that JavaDoc is single-threaded.
>
> (1) seems the most reasonable. One potential problem with (1) though is that future refactoring may reintroduce the issue again, but in a way that the regression test won't catch.
Nice fix. Thanks for the detailed explanation and suggested solutions
test/langtools/jdk/javadoc/doclet/testLatePackageDiscovery/TestLatePackageDiscovery.java line 65:
> 63: "--module", "test.foo",
> 64: "-nodeprecated");
> 65: checkExit(Exit.OK);
(Optional)
Since this is about ensuring we don't get CME again,
either explicitly turn on exception checking (even though it is on by default)
or insert a comment noting than exception checking is on by default
-------------
Marked as reviewed by jjg (Reviewer).
PR Review: https://git.openjdk.org/jdk/pull/14310#pullrequestreview-1463535349
PR Review Comment: https://git.openjdk.org/jdk/pull/14310#discussion_r1218625711
More information about the javadoc-dev
mailing list