Return-Path: X-Original-To: apmail-commons-user-archive@www.apache.org Delivered-To: apmail-commons-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A346F108F6 for ; Fri, 13 Sep 2013 21:58:20 +0000 (UTC) Received: (qmail 14185 invoked by uid 500); 13 Sep 2013 21:58:19 -0000 Delivered-To: apmail-commons-user-archive@commons.apache.org Received: (qmail 14118 invoked by uid 500); 13 Sep 2013 21:58:19 -0000 Mailing-List: contact user-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Users List" Delivered-To: mailing list user@commons.apache.org Received: (qmail 14110 invoked by uid 99); 13 Sep 2013 21:58:19 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 13 Sep 2013 21:58:19 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of garydgregory@gmail.com designates 209.85.214.44 as permitted sender) Received: from [209.85.214.44] (HELO mail-bk0-f44.google.com) (209.85.214.44) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 13 Sep 2013 21:58:13 +0000 Received: by mail-bk0-f44.google.com with SMTP id mz10so706000bkb.3 for ; Fri, 13 Sep 2013 14:57:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=oNdMzUUR6JKCHTR+WkLX4D+qPpKIu+ku86PQZ2xom0o=; b=s7jKU9svVGBD4HMPzbwEEFaKTESqfzhrFWHl3NC2T5gDqnrhb2sO7Kb2vN/NbjAGdd OnYoD1rIEk0g+Xx3laa2fkyFOp2YtOA6AoNLY8btMs873wNPsPsF5uVckg7xMxSVMMvx IhyfblUiK9VcWqybaPUI+KxNtWvN2AfeaAsEktPxWRkyHKGTyvadjnCNXuE7Elr983bN t2xkV/By1R52XINgTKI37s9Nst2OAV+KRNA49gZreJizmsg/jLXIyaVrwgU21/H8h906 Or5aoints8gm8/5CoE40EEeJ6akaY2vwZhSyDUBvV/QkAOwGoSuw6DRDo+5+gfC8yCva lcWw== MIME-Version: 1.0 X-Received: by 10.204.167.140 with SMTP id q12mr12986061bky.2.1379109473080; Fri, 13 Sep 2013 14:57:53 -0700 (PDT) Received: by 10.205.6.7 with HTTP; Fri, 13 Sep 2013 14:57:52 -0700 (PDT) In-Reply-To: References: Date: Fri, 13 Sep 2013 17:57:52 -0400 Message-ID: Subject: Re: [jxpath] race in getNodePointerFactories() ? From: Gary Gregory To: Commons Users List Content-Type: multipart/alternative; boundary=bcaec52c5e7340176204e64af203 X-Virus-Checked: Checked by ClamAV on apache.org --bcaec52c5e7340176204e64af203 Content-Type: text/plain; charset=UTF-8 In the case of JXPathContext, the statics are private, so the change would be safe. I'm not going to do that now because I see a unit test failure (as mentioned in a previous message). Gary On Fri, Sep 13, 2013 at 5:40 PM, sebb wrote: > On 13 September 2013 22:28, Gary Gregory wrote: > > On Fri, Sep 13, 2013 at 4:27 PM, sebb wrote: > > > >> On 13 September 2013 19:06, Gary Gregory > wrote: > >> > Hello David, > >> > > >> > Can you test with the 1.4-SNAPSHOT code from trunk? (You'll have to > check > >> > it out and build it). > >> > >> The method JXPathContextReferenceImpl.getNodePointerFactories() is not > >> synchronized in trunk either, and the static field is not volatile. > >> Furthermore, the method returns the array without copying it, so > >> callers can change entries in it. > >> > >> Also the field is created with a lock on the class, but is set to null > >> with a lock on the static feld nodeFactories. > >> And of course read with no lock at all. > >> > >> Not good; safe publication requires that reader and writer lock the > >> same object. And writers must use the same object for mutual > >> exclusion. > >> > >> The code is extremely fragile. > >> > >> > In trunk, NodePointer.java:80) is: > >> > > >> > pointer = new NullPointer(name, locale); > >> > > >> > which can't throw an NPE. > >> > >> 1.3 has the following code: > >> > >> NodePointerFactory[] factories = > >> JXPathContextReferenceImpl.getNodePointerFactories(); > >> for (int i = 0; i < factories.length; i++) { // <= line 80 > >> > >> The for loop is at line 86 in trunk. > >> > >> > I have not looked at this code in a while, now that multi-core CPUs > are > >> > common place I am not surprised to see issues like this. > >> > > >> > I am not even sure why the statics in JXPathContext are lazy loaded > (at > >> > least they are volatile). It would simplify the code to just init the > >> > statics in-line. > >> > >> Mutable static fields are generally very bad for thread-safety. > >> > > > > I was not clear: The JXPathContext statics are only writing to by the > > getters for lazy-init. Why not init them in the decl and make them final? > > I was referring to JXPathContextReferenceImpl which is where the NPE > originated. > > But JXPathContext could do with some work as well. > Could use the IODH idiom for the lazy-init. > > Unfortunately it has several protected mutable fields. > These should have been private, especially since they seem to have > public getters/setters. > Fixing that will break compat, unless the class can be shown to be > internal only. > > > Gary > > > >> > >> > Thank you, > >> > Gary > >> > > >> > > >> > On Thu, Sep 12, 2013 at 10:11 PM, David Ferry > >> wrote: > >> > > >> >> Hi > >> >> > >> >> We're using Apache JXPath 1.3. We have multi-threaded code running > >> >> JXPathContext.newContext > >> >> > >> >> We're having an occasional NullPointerException coming out of > >> >> NodePointer.newNodePointer > >> >> > >> >> We've had a look at the code for this class, and wonder if > >> >> getNodePointerFactories is not synchronising correctly. > >> >> > >> >> Other operations that read or write the attribute nodeFactoryArray > ... > >> >> synchronize on nodeFactories. > >> >> > >> >> But in getNodePointerFactories() ... it just returns the variable > >> without > >> >> locking. > >> >> This makes us think that there is no happens-before in the > >> >> getNodePointerFactories() method. > >> >> > >> >> I've put a snippet of the stack trace below. > >> >> > >> >> Apologies if this has come up before, I didn't manage to find it > after a > >> >> while searching. > >> >> > >> >> Regards, > >> >> -David > >> >> > >> >> java.lang.NullPointerException > >> >> at > >> >> > >> > org.apache.commons.jxpath.ri.model.NodePointer.newNodePointer(NodePointer.java:80) > >> >> at > >> >> > >> > org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.(JXPathContextReferenceImpl.java:193) > >> >> at > >> >> > >> > org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.(JXPathContextReferenceImpl.java:167) > >> >> at > >> >> > >> > org.apache.commons.jxpath.ri.JXPathContextFactoryReferenceImpl.newContext(JXPathContextFactoryReferenceImpl.java:39) > >> >> at > >> >> > >> > org.apache.commons.jxpath.JXPathContext.newContext(JXPathContext.java:416) > >> >> > >> > > >> > > >> > > >> > -- > >> > E-Mail: garydgregory@gmail.com | ggregory@apache.org > >> > Java Persistence with Hibernate, Second Edition< > >> http://www.manning.com/bauer3/> > >> > JUnit in Action, Second Edition > >> > Spring Batch in Action > >> > Blog: http://garygregory.wordpress.com > >> > Home: http://garygregory.com/ > >> > Tweet! http://twitter.com/GaryGregory > >> > >> --------------------------------------------------------------------- > >> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org > >> For additional commands, e-mail: user-help@commons.apache.org > >> > >> > > > > > > -- > > E-Mail: garydgregory@gmail.com | ggregory@apache.org > > Java Persistence with Hibernate, Second Edition< > http://www.manning.com/bauer3/> > > JUnit in Action, Second Edition > > Spring Batch in Action > > Blog: http://garygregory.wordpress.com > > Home: http://garygregory.com/ > > Tweet! http://twitter.com/GaryGregory > > --------------------------------------------------------------------- > To unsubscribe, e-mail: user-unsubscribe@commons.apache.org > For additional commands, e-mail: user-help@commons.apache.org > > -- E-Mail: garydgregory@gmail.com | ggregory@apache.org Java Persistence with Hibernate, Second Edition JUnit in Action, Second Edition Spring Batch in Action Blog: http://garygregory.wordpress.com Home: http://garygregory.com/ Tweet! http://twitter.com/GaryGregory --bcaec52c5e7340176204e64af203--