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

Race condition in ThreadLocalCleaner initialization code

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Blocker
    • Resolution: Fixed
    • None
    • Commons Threads 3.2.16
    • Commons
    • None

    Description

      There is a race condition in the ThreadLocalCleaner initialization code that can lead to the fields not being fully initialized when read.

      The stack trace is similar to

      Exception in thread "sling-oak-observation-2" java.lang.NullPointerException
              at org.apache.sling.commons.threads.impl.ThreadLocalCleaner.copy(ThreadLocalCleaner.java:110)
              at org.apache.sling.commons.threads.impl.ThreadLocalCleaner.saveOldThreadLocals(ThreadLocalCleaner.java:230)
              at org.apache.sling.commons.threads.impl.ThreadLocalCleaner.<init>(ThreadLocalCleaner.java:155)
              at org.apache.sling.commons.threads.impl.ThreadPoolExecutorCleaningThreadLocals.beforeExecute(ThreadPoolExecutorCleaningThreadLocals.java:58)
              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1139)
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
              at java.lang.Thread.run(Thread.java:748)

      Assume that two threads, T1 and T2 enter the constructor at more or less the same time. The following events can then occur, leading to the above invalid access

      T1 T2
      check threadLocalsField (is null)  
      invoke initReflectionFields, load threadLocalsField  
        check threadLocalsField (is not null)
        invoke saveOldTreadLocals
        invoke copy

      At this point, the tableField field is null since initReflectionFields has not yet completed, which leads to the above NPE.

      edit: formatting

      Attachments

        Issue Links

          Activity

            People

              rombert Robert Munteanu
              rombert Robert Munteanu
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: