You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@abdera.apache.org by "David Calavera (JIRA)" <ji...@apache.org> on 2008/05/02 23:40:55 UTC

[jira] Updated: (ABDERA-155) Abdera class "this" reference escapes during construction, leading to race conditions and NPEs

     [ https://issues.apache.org/jira/browse/ABDERA-155?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

David Calavera updated ABDERA-155:
----------------------------------

    Attachment: abdera_singleton.diff

> Abdera class "this" reference escapes during construction, leading to race conditions and NPEs
> ----------------------------------------------------------------------------------------------
>
>                 Key: ABDERA-155
>                 URL: https://issues.apache.org/jira/browse/ABDERA-155
>             Project: Abdera
>          Issue Type: Bug
>    Affects Versions: 0.4.0
>         Environment: OS X, JVM 1.5, 2 x 2.8 GHz quad-core Xeon
>            Reporter: Todd Wells
>            Priority: Critical
>         Attachments: abdera_singleton.diff
>
>
> I have hit a number of NullPointerExceptions when using Abdera in a very generic and straightforward fashion.  The same code doesn't cause the NPEs in all cases, it seems to depend on the environment in which it's executing, which leads to a strong suspicion of a race condition.  I've seen an NPE in code as simple as this groovy code:
> def abdera = new Abdera()
> Entry entry = abdera.newEntry()  /* NPE here, Caused by: java.lang.NullPointerException at org.apache.abdera.Abdera.newEntry(Abdera.java:114)
> Another example was an NPE when calling ClientResponse.getDocument(), Caused by: java.lang.NullPointerException at
> org.apache.abdera.protocol.client.AbstractClientResponse.getDocument(AbstractClientResponse.java:96)
> When I run the code in IDEA (calling from a unit test or executing a custom ant task) it works fine. When I'm calling it from inside of a custom ant task running at the command line, I get the null pointer exceptions that I've described.  This supports the notion of it being a race condition -- the NPE's are highly squirrelly and environment-specific.
> Digging into the Abdera code, I believe I've found the source of the problems.  The constructor of the Abdera class calls newFactory(), newParser(), newXPath(), newParserFactory() and newWriter().  In every one of these methods, a reference to "this" is passed to a different class (or multiple classes, as it would appear).  The fundamental problem here is since this code is being called from the constructor the "this" reference is not yet guaranteed to be a completely constructed object. This can result in unpredictable behavior, such as the NPE mentioned in this bug.  Since the classes (ServiceUtil and AbderaConfiguration) that get references to "this" appear to be using "this" in a multi-threaded fashion, this can result in race conditions.
> For a detailed discussion, I recommend reviewing this article "Safe construction techniques" by Brian Goetz: http://www.ibm.com/developerworks/java/library/j-jtp0618.html, or he has a more detailed discussion of this problem in his book "Java Concurrency in Practice".  I will quote his article here: "One of the mistakes that can introduce a data race into your class is to expose the this reference to another thread before the constructor has completed. Sometimes the reference is explicit, such as directly storing this in a static field or collection, but other times it can be implicit, such as when you publish a reference to an instance of a non-static inner class in a constructor. Constructors are not ordinary methods -- they have special semantics for initialization safety. An object is assumed to be in a predictable, consistent state after the constructor has completed, and publishing a reference to an incompletely constructed object is dangerous."
> This is a serious set of bugs in the Abdera class and the resultant problems will probably become more prevalent as machines become faster.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Re: General Question about IDs

Posted by James M Snell <ja...@gmail.com>.

M Harris wrote:
> I just answered my own question.  The Ids can't be changed by the user (right?) and I think it would be best to do a GET first and then make sure the entry Ids match before pursuing.
> 

Right.

- James

> M Harris <md...@yahoo.com> wrote: This is not necessarily about Abdera, but about the Atom Publishing Protocol in general regarding entry Ids.  I know there are some people on the mailing list who have developed their sites using Abdera and Atom.  Are you storing the Entry Ids in a database or other structure?  I was wondering how exactly to handle editing a resource.
> 
> I guess I am wondering how to ensure that the link during an edit of a resource is infact that Entry itself the client is trying to edit.
> 
> Taken from an article online just for reference:
> 
>                
> PUT /blog/entries/1 HTTP/1.1
> Host: example.org
> Content-Type: application/atom+xml; charset=utf-8
> Content-Length: nnnn
> If-Match: "/blog/entries/1?1"
> If-Unmodified-Since: Sat, 12 Aug 2006 13:40:03 GMT
> 
> 
> 
>   tag:example.org,2006:/blog/entries/1
> Atom-Powered Robots Run Crazy  
>   
> 
>   
> 
>   2006-08-12T13:40:03Z
>   John
>   Some different text.
> 
> 
> 
> 
> How should it know that 
> 
> /blog/entries/1
>  and the entry 
> 
> 
> tag:example.org,2006:/blog/entries/1
>  
> Mean the same? In this case the Id happens to be the URL of the entry but from what I have read the id does not have to be any part of the URL.  If someone were to do a PUT to the same URL but with this ID (2007 instead of 2008)
> 
> tag:example.org,2007:/blog/entries/1
>  Does it overwrite the id?  
> 
> 
> 
>        
> ---------------------------------
> Be a better friend, newshound, and know-it-all with Yahoo! Mobile.  Try it now.
> 
>        
> ---------------------------------
> Be a better friend, newshound, and know-it-all with Yahoo! Mobile.  Try it now.

Re: General Question about IDs

Posted by M Harris <md...@yahoo.com>.
I just answered my own question.  The Ids can't be changed by the user (right?) and I think it would be best to do a GET first and then make sure the entry Ids match before pursuing.

M Harris <md...@yahoo.com> wrote: This is not necessarily about Abdera, but about the Atom Publishing Protocol in general regarding entry Ids.  I know there are some people on the mailing list who have developed their sites using Abdera and Atom.  Are you storing the Entry Ids in a database or other structure?  I was wondering how exactly to handle editing a resource.

I guess I am wondering how to ensure that the link during an edit of a resource is infact that Entry itself the client is trying to edit.

Taken from an article online just for reference:

               
PUT /blog/entries/1 HTTP/1.1
Host: example.org
Content-Type: application/atom+xml; charset=utf-8
Content-Length: nnnn
If-Match: "/blog/entries/1?1"
If-Unmodified-Since: Sat, 12 Aug 2006 13:40:03 GMT



  tag:example.org,2006:/blog/entries/1
Atom-Powered Robots Run Crazy  
  

  

  2006-08-12T13:40:03Z
  John
  Some different text.




How should it know that 

/blog/entries/1
 and the entry 


tag:example.org,2006:/blog/entries/1
 
Mean the same? In this case the Id happens to be the URL of the entry but from what I have read the id does not have to be any part of the URL.  If someone were to do a PUT to the same URL but with this ID (2007 instead of 2008)

tag:example.org,2007:/blog/entries/1
 Does it overwrite the id?  



       
---------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile.  Try it now.

       
---------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile.  Try it now.

General Question about IDs

Posted by M Harris <md...@yahoo.com>.
This is not necessarily about Abdera, but about the Atom Publishing Protocol in general regarding entry Ids.  I know there are some people on the mailing list who have developed their sites using Abdera and Atom.  Are you storing the Entry Ids in a database or other structure?  I was wondering how exactly to handle editing a resource.

I guess I am wondering how to ensure that the link during an edit of a resource is infact that Entry itself the client is trying to edit.

Taken from an article online just for reference:

               
PUT /blog/entries/1 HTTP/1.1
Host: example.org
Content-Type: application/atom+xml; charset=utf-8
Content-Length: nnnn
If-Match: "/blog/entries/1?1"
If-Unmodified-Since: Sat, 12 Aug 2006 13:40:03 GMT

<?xml version="1.0" ?>
<entry xmlns="http://www.w3.org/2005/Atom" >
  <id>tag:example.org,2006:/blog/entries/1</id>
  <title>Atom-Powered Robots Run Crazy</title>
  <link href="http://example.org/2003/12/13/atom03"/>
  <link rel="edit" href="http://example.org/blog/entries/1" />
  <updated>2006-08-12T13:40:03Z</updated>
  <author><name>John</name></author>
  <summary>Some different text.</summary>
</entry>



How should it know that 

/blog/entries/1
 and the entry 


<id>tag:example.org,2006:/blog/entries/1</id>
 
Mean the same? In this case the Id happens to be the URL of the entry but from what I have read the id does not have to be any part of the URL.  If someone were to do a PUT to the same URL but with this ID (2007 instead of 2008)

<id>tag:example.org,2007:/blog/entries/1</id>
 Does it overwrite the id?  



       
---------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile.  Try it now.