You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Greg Ward <gw...@python.net> on 2005/08/05 02:41:06 UTC

Problem with persistent page property and concurrent access

I'm getting a Tapestry error from concurrent access to a page with a
persistent property, where both accesses attempt to mutate the property. I
know I'm not the first to see this, since I did some google'ing earlier
today -- but I didn't find any good solutions.  (Oh yeah, I'm using Tapestry
3.0.3 under Tomcat 4.1.30.)

I have distilled the problem to a tiny test application, ConcurrencyTest,
that demonstates the problem -- I will attach a .war file so you can see for
youself.  (The attached .war file doesn't include Tapestry or dependent
libs; for a full standalone app, get
http://starship.python.net/~gward/ConcurrencyTest-full.war .)

First, to trigger the error, all I have to do is double-click a link that
mutates the persistent page property.  When I do that, the second request
fails with:

org.apache.tapestry.ApplicationRuntimeException
Page recorder for page Home is locked after a commit(), but received a
change to property stuff of component Home.
Stack Trace:
    * org.apache.tapestry.record.PageRecorder.observeChange(PageRecorder.java:119)
    * org.apache.tapestry.Tapestry.fireObservedChange(Tapestry.java:1339)
    * org.apache.tapestry.AbstractComponent.fireObservedChange(AbstractComponent.java:304)
    * ConcurrencyTest$Enhance_16.setStuff(ConcurrencyTest$Enhance_16.java)
    * ConcurrencyTest.permute(ConcurrencyTest.java:32)
    * sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    * sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    * sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    * java.lang.reflect.Method.invoke(Method.java:324)
    * org.apache.tapestry.listener.ListenerMap.invokeTargetMethod(ListenerMap.java:257)
    * org.apache.tapestry.listener.ListenerMap.access$100(ListenerMap.java:46)
    [...]

... or at least I assume it's the second request failing.  Since my mutating
request features a Thread.sleep(1000) call (to make the race condition
easier to hit), it's fairly predictable, but I can imagine things being more
confusing in a real-world situation.

The nub of the problem can be demonstrated with three snippets: first, from
the page descriptor (Home.page):

    <property-specification name="stuff" type="java.util.List" persistent="yes"/>

Second, from the template (Home.html):

  <a href="" jwcid="@DirectLink" listener="ognl:listeners.permute">permute this list</a>
    (double-click link to see the exception)

Finally, from the page class (ConcurrencyTest.java):

    public void permute( IRequestCycle cycle ) throws InterruptedException
    {
        System.out.println( "ConcurrencyTest.permute: page = " + this + 
                            ", page recorder = " + this.getChangeObserver() );
        Thread.sleep( 1000 );
        List stuff = getStuff();
        Object top = stuff.remove( 0 );
        stuff.add( top );
        setStuff( stuff );
        
        cycle.activate( this );
    }

As you can see, every click of the "permute this list" link takes 1 sec to
run (think slow database query), and mutates the persistent page property
'stuff' (think caching result of query).

As long as the user is patient and waits for request i to complete before
submitting request i+1, this works dandy.  But as soon as someone gets
impatient, or accidentally double-clicks the hyperlink, boom! the app blows
up with the above stack trace.  Thanks to the sleep(1000) call, this is
absolutely 100% reproducible -- easiest race condition in the world.

After doing some digging, I am inclined to think this is a Tapestry bug.
(If not, shouldn't the docs warn against mutating persistent properties?  If
you can't do it during concurrent requests, you can't do it period.  In
which case, what is the point of persistent properties?) My main evidence
for this is the output of my System.out call.  Here's what happens when I
submit two sequential requests (patient user):

ConcurrencyTest.permute: page = ConcurrencyTest$Enhance_16@159054d[Home], page recorder = org.apache.tapestry.record.SessionPageRecorder@b3b6a6
ConcurrencyTest.permute: page = ConcurrencyTest$Enhance_16@159054d[Home], page recorder = org.apache.tapestry.record.SessionPageRecorder@b3b6a6

Tapestry reuses the same page instance, as expected; naturally, that page
has the same PageRecorder instance (from getChangeObserver()) in the two
calls.  No problem.

But when I double-click the hyperlink (impatient user), I see this in
Tomcat's log:

ConcurrencyTest.permute: page = ConcurrencyTest$Enhance_16@159054d[Home], page recorder = org.apache.tapestry.record.SessionPageRecorder@b3b6a6
ConcurrencyTest.permute: page = ConcurrencyTest$Enhance_16@a6cbf7[Home], page recorder = org.apache.tapestry.record.SessionPageRecorder@b3b6a6

Tapestry uses two distinct page objects to handle the concurrent requests,
as expected ... but they share the same PageRecorder!  No wonder there are
concurrency problems: the PageRecorder is locked by the first request, and
still locked when the second request comes in (from a *different* page
instance) -- so ka-boom!

Should different page instances really be sharing the same PageRecorder?  If
that's the intended design, it seems rather dangerous to me, considering
that PageRecorder is 1) touchy about locks and 2) obviously not written for
concurrent access.

        Greg

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org