[ 
https://issues.apache.org/jira/browse/VELOCITY-536?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12486096
 ] 

Nathan Bubna commented on VELOCITY-536:
---------------------------------------

Looking more carefully at this, rather than trusting my first look...

Ok, i see that there is potential for a second thread to jump ahead of the 
first one which is busy performing the meat of the init.  This second thread 
then might run into trouble if it tries to do anything with the instance before 
the first thread finishes the setup/initialization.

So, Christopher, you were right, there is an issue here, just not the original 
one.  Sorry, and thanks for pressing me to keep thinking about it. :)

So, *if* the user is running things on pre-1.5, then JIT/VM can re-order the 
setting of init=true within the synchronized block.   Then, *if* the VM decides 
to do that and another thread comes along and finds init=true, thus skipping 
the synchronization, it could pass up the original thread which is still busy 
performing the initialization.  *If* it then gets on to where it tries to use 
the incompletely initialized (but fully instantiated ;) instance, it could get 
into trouble.

So, while Lei's patch completely eliminates the chances of having the 
initialization code called multiple times, it does introduce the chance of even 
less-frequent (and thus much harder to debug) synchronization problems for 
those running on older JVMs.  Apply Murphy's Law, and we can be sure it will 
happen. :(   We'd better just synchronize the block/method, without trying to 
skip it via double checking.   If anyone's interested, we could leave a note 
that once we require 1.5, we can use DCL with the volatile keyword happily.

> Velocity Engine throws NullPointer Exception when two people click on the 
> same page at the same time for the first time
> -----------------------------------------------------------------------------------------------------------------------
>
>                 Key: VELOCITY-536
>                 URL: https://issues.apache.org/jira/browse/VELOCITY-536
>             Project: Velocity
>          Issue Type: Bug
>          Components: Engine
>    Affects Versions: 1.5
>            Reporter: Lei Gu
>         Attachments: 536-patch.txt, ASTDirective.java, ASTSetDirective.java, 
> VelocimacroProxy.java
>
>
> Multi-thread concurrency issue
> During our concurrency testing, we observed NullPointer exceptions being 
> thrown when two people hit the same page at the same time for the first time. 
> Upon further investigation, it turns out that we need to synchronize the init 
> method on ASTDirective, ASTSetDirective, and render method on 
> ASTSetDirective, and VelocimacroProxy.
>  Basically, the problem is introduced as the following; when two threads 
> attempts to parse and render the same template at the same time. Thread1 
> finishes parsing first and proceeds to the render method call, while thread 2 
> is still busy parsing and will overwrite the existing parse tree that is 
> being used by thread 1 for rendering purpose. Thus under certainly condition 
> a NullPointer exception will be thrown. 

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


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to