8146274: Thread spinning on WeakHashMap.getEntry() with concurrent use of nashorn
authorhannesw
Mon, 18 Jan 2016 10:25:36 +0100
changeset 35322 eff26d265a9d
parent 35005 2dc4c11fe488
child 35323 0ef5f8b7cd23
8146274: Thread spinning on WeakHashMap.getEntry() with concurrent use of nashorn Reviewed-by: mhaupt, attila
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/PropertyListeners.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/Functions.properties
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/PropertyListeners.java	Wed Jul 05 21:13:10 2017 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/PropertyListeners.java	Mon Jan 18 10:25:36 2016 +0100
@@ -54,7 +54,13 @@
      */
     PropertyListeners(final PropertyListeners listener) {
         if (listener != null && listener.listeners != null) {
-            this.listeners = new WeakHashMap<>(listener.listeners);
+            this.listeners = new WeakHashMap<>();
+            // We need to copy the nested weak sets in order to avoid concurrent modification issues, see JDK-8146274
+            synchronized (listener) {
+                for (final Map.Entry<Object, WeakPropertyMapSet> entry : listener.listeners.entrySet()) {
+                    this.listeners.put(entry.getKey(), new WeakPropertyMapSet(entry.getValue()));
+                }
+            }
         }
     }
 
@@ -228,7 +234,15 @@
 
     private static class WeakPropertyMapSet {
 
-        private final WeakHashMap<PropertyMap, Boolean> map = new WeakHashMap<>();
+        private final WeakHashMap<PropertyMap, Boolean> map;
+
+        WeakPropertyMapSet() {
+            this.map = new WeakHashMap<>();
+        }
+
+        WeakPropertyMapSet(final WeakPropertyMapSet set) {
+            this.map = new WeakHashMap<>(set.map);
+        }
 
         void add(final PropertyMap propertyMap) {
             map.put(propertyMap, Boolean.TRUE);
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/Functions.properties	Wed Jul 05 21:13:10 2017 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/Functions.properties	Mon Jan 18 10:25:36 2016 +0100
@@ -24,7 +24,7 @@
 
 Object.isSealed=tells if an object is sealed or not
 
-Object.isFrozen=tells if an object is fronzen or not
+Object.isFrozen=tells if an object is frozen or not
 
 Object.isExtensible=tells if an object is extensible or not
 
@@ -32,7 +32,7 @@
 
 Object=creates a new script object or converts given value as a script object
 
-Object.prototype.toString=returns a string representing of this object
+Object.prototype.toString=returns a string representation of this object
 
 Object.prototype.hasOwnProperty=tells whether this object has the specified property or not
 
@@ -42,3 +42,12 @@
 
 Object.bindProperties=binds the source object's properties to the target object (nashorn extension)
 
+Function=creates a new function with the given parameters and function body
+
+Function.prototype.toString=returns a string representation of this function
+
+Function.prototype.apply=invokes the function with the given this-reference and arguments array
+
+Function.prototype.call=invokes the function with the given this-reference and arguments
+
+Function.prototype.bind=returns a new function with bound this-reference and arguments