Commons JXPath
  1. Commons JXPath
  2. JXPATH-76

CompiledExpression not completely thread-safe

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.2 Final
    • Fix Version/s: 1.3
    • Labels:
      None

      Description

      Some classes in compiled expressions are not thread-safe, because they lazily initialize some information that should be initialized at object construction. The classes I've found are Path and ExpressionPath. I've attached a path that fixes the problem.

      When two threads tried to initialize such an object concurrently, it was possible that the expression was evaluated as simple expression when it should have been evaluated as complex expression. See the two stack traces below (using xpath expression //ExternalData/Order/@id). The first stack trace shows the bug in action, the second how it should look:

      caused by java.lang.UnsupportedOperationException: Not supported node test for attributes: node()
      at org.apache.commons.jxpath.ri.axes.SimplePathInterpreter.getNodeIterator(SimplePathInterpreter.java(Compiled Code))
      at org.apache.commons.jxpath.ri.axes.SimplePathInterpreter.doStepNoPredicatesStandard(SimplePathInterpreter.java(Compiled Code))
      at org.apache.commons.jxpath.ri.axes.SimplePathInterpreter.doStep(SimplePathInterpreter.java(Compiled Code))
      at org.apache.commons.jxpath.ri.axes.SimplePathInterpreter.interpretSimpleLocationPath(SimplePathInterpreter.java(Inlined Compiled Code))
      at org.apache.commons.jxpath.ri.compiler.Path.getSingleNodePointerForSteps(Path.java(Inlined Compiled Code))
      at org.apache.commons.jxpath.ri.compiler.LocationPath.computeValue(LocationPath.java(Compiled Code))
      at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getPointer(JXPathContextReferenceImpl.java(Compiled Code))
      at org.apache.commons.jxpath.ri.JXPathCompiledExpression.selectSingleNode(JXPathCompiledExpression.java(Inlined Compiled Code))

      This replaces the lines starting at Path.getSingleNodePointerForSteps:

      LocationPath(Path).searchForPath(EvalContext) line: 183
      LocationPath(Path).getSingleNodePointerForSteps(EvalContext) line: 159

      1. jxpath.patch
        3 kB
        Simon Raess

        Activity

        Hide
        Simon Raess added a comment -

        patch fixing (some?) multithreading bugs in compiled expressions

        Show
        Simon Raess added a comment - patch fixing (some?) multithreading bugs in compiled expressions
        Hide
        Matt Benson added a comment -

        It takes less analysis on my part (as a self-appointed, but non-expert, custodian of JXPath) to simply synchronize these methods, which I have done in SVN HEAD. Thanks for the report!

        Show
        Matt Benson added a comment - It takes less analysis on my part (as a self-appointed, but non-expert, custodian of JXPath) to simply synchronize these methods, which I have done in SVN HEAD. Thanks for the report!
        Hide
        Simon Raess added a comment -

        I've thought about that solution too, but the change without synchronized is pretty straightforward. Instead of lazy initialization, the initialization is simply moved to the constructor. There are no side-effects and the need of synchronization is completely removed. The cost of synchronization has to be paid for each expression evaluation. It is needed only on the first time. How else can I convince you?

        Show
        Simon Raess added a comment - I've thought about that solution too, but the change without synchronized is pretty straightforward. Instead of lazy initialization, the initialization is simply moved to the constructor. There are no side-effects and the need of synchronization is completely removed. The cost of synchronization has to be paid for each expression evaluation. It is needed only on the first time. How else can I convince you?

          People

          • Assignee:
            Unassigned
            Reporter:
            Simon Raess
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development