Uploaded image for project: 'Sling'
  1. Sling
  2. SLING-6584

Race condition in ModelAdapterFactory

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • Sling Models Impl 1.3.8
    • Sling Models Impl 1.4.0
    • Extensions
    • None

    Description

      There is a possible race condition in https://github.com/apache/sling/blob/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java#L807-L815 when two threads are trying to inject the same field, resulting in model not being constructed.

      try {
          if (!accessible) {
              field.setAccessible(true);
          }
          field.set(createdObject, result.getValue());
      } catch (Exception e) {
          return new ModelClassException("Could not inject field due to reflection issues", e);
      } finally {
          if (!accessible) {
              field.setAccessible(false);
          }
      }
      

      This is exposed by the unit test attached:

      org.apache.sling.models.impl.ModelAdapterFactory - Could not adapt to model
      org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class org.apache.sling.models.testmodels.classes.WithOneConstructorModel
      	at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:593)
      	at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:335)
      	at org.apache.sling.models.impl.ModelAdapterFactory.getAdapter(ModelAdapterFactory.java:211)
      	at org.apache.sling.models.impl.ConstructorTest$1ModelCreator.call(ConstructorTest.java:153)
      	at org.apache.sling.models.impl.ConstructorTest$1ModelCreator.call(ConstructorTest.java:149)
      	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      	at java.lang.Thread.run(Thread.java:745)
      	Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject private int org.apache.sling.models.testmodels.classes.WithOneConstructorModel.attribute
      		at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:598)
      		... 8 more
      	Caused by: org.apache.sling.models.factory.ModelClassException: Could not inject field due to reflection issues
      		at org.apache.sling.models.impl.ModelAdapterFactory.setField(ModelAdapterFactory.java:812)
      		at org.apache.sling.models.impl.ModelAdapterFactory.access$100(ModelAdapterFactory.java:112)
      		at org.apache.sling.models.impl.ModelAdapterFactory$SetFieldCallback.inject(ModelAdapterFactory.java:378)
      		at org.apache.sling.models.impl.ModelAdapterFactory.injectElement(ModelAdapterFactory.java:473)
      		at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:596)
      		... 8 more
      	Caused by: java.lang.IllegalAccessException: Class org.apache.sling.models.impl.ModelAdapterFactory can not access a member of class org.apache.sling.models.testmodels.classes.WithOneConstructorModel with modifiers "private"
      		at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:101)
      		at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:295)
      		at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:287)
      		at java.lang.reflect.Field.set(Field.java:755)
      		at org.apache.sling.models.impl.ModelAdapterFactory.setField(ModelAdapterFactory.java:810)
      		... 12 more
      

      Attachments

        1. SLING-6584.test.patch
          3 kB
          Vlad Bailescu

        Issue Links

          Activity

            People

              justinedelson Justin Edelson
              vladb Vlad Bailescu
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: