JDK-8075006 : Threads spinning infinitely in WeakHashMap.get running test262parallel
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2015-03-11
  • Updated: 2015-09-29
  • Resolved: 2015-03-13
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 8 JDK 9
8u60Fixed 9 b56Fixed
Description
When running test262parallel, it sometimes hangs indefinitely. Looks like this happens because sometimes threads try to read the protoHistory of the same PropertyMap at the same time it is being modified, and this unfortunately can lead to an infinite loop in WeakHashMap due to the way it is written (it is not threadsafe):

at java.util.WeakHashMap.get(WeakHashMap.java:403)
at jdk.nashorn.internal.runtime.PropertyMap.checkProtoHistory(PropertyMap.java:687)
at jdk.nashorn.internal.runtime.PropertyMap.changeProto(PropertyMap.java:886)
at jdk.nashorn.internal.runtime.ScriptObject.setProto(ScriptObject.java:1288)
...

It seems that in a multithreaded environment, PropertyMap.history and .protoHistory would need to be threadsafe. They're both caches with weak keys and soft values (WeakHashMap<K, SoftReference<V>>) and WeakHashMap is unfortunately not threadsafe.

Comments
Added noreg-hard as this bug only manifests itself rarely, in a test suite that takes several minutes to run.
13-03-2015

Currently the only solution is to synchronize on the WeakHashMap, which is a shame as it (a) imposes a performance penalty on single-threaded use of Nashorn and (b) most of the accesses are reads, not writes. Nashorn, being in the boot class path obviously can't use third-party libraries, but I know that there are reliably working cache implementations out there that allow you to build caches with weak keys and soft values and reads of already cached values that happen concurrently with each other and with any addition of new values to the cache. It'd be awesome if we had something like this in the base Java platform.
11-03-2015