Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10142

Infinite loop when clearing Parameterized Type Cache

    XMLWordPrintableJSON

Details

    Description

      I have application that essentially makes concurrent calls to a method similar to the one below

      Script compileConcurrently(String source) {
        Script = (Script) groovyClassLoader.parseClass(source).newInstance();
        GenericsUtils.clearParameterizedTypeCache();
        StaticTypeCheckingSupport.clearExtensionMethodCache();
        return script;
      }

      I am running into an issue where one thread gets stuck in an infinite loop in theĀ GenericsUtils::clearParameterizedTypeCache method while other threads are waiting in GroovyClassLoader::parseClass

      I see PARAMETERIZED_TYPE_CACHE is backed by an access-ordered LinkedHashMap guarded with ReadWriteLock. I think the root cause is concurrent LinkedHashMap::get calls modifies the order of the map entries and creates a loop.

      See LinkedHashMap api docs

      Note that this implementation is not synchronized. If multiple threads access a linked hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:
      
         Map m = Collections.synchronizedMap(new LinkedHashMap(...));
      
      A structural modification is any operation that adds or deletes one or more mappings or, in the case of access-ordered linked hash maps, affects iteration order. In insertion-ordered linked hash maps, merely changing the value associated with a key that is already contained in the map is not a structural modification. In access-ordered linked hash maps, merely querying the map with get is a structural modification. ) 
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            test3232 Leon A
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: