[jira] [Created] (VELOCITY-992) VelocityEngine multi-thread deadlock

"vincent royer (Jira)" <[email protected]>
Newsgroups gmane.comp.jakarta.velocity.devel
Message-ID <[email protected]>
vincent royer created VELOCITY-992:
--------------------------------------

             Summary: VelocityEngine multi-thread deadlock
                 Key: VELOCITY-992
                 URL: https://issues.apache.org/jira/browse/VELOCITY-992
             Project: Velocity
          Issue Type: Bug
          Components: Engine
    Affects Versions: 2.4.1
            Reporter: vincent royer


When using one VelocityEngine instance in a multithread app, I get the following dead lock:

"{*}javamail-executor-thread-1{*}@20270" tid=0xe0 nid=NA waiting for monitor entry
  java.lang.Thread.State: BLOCKED
     waiting for {*}javamail-executor-thread-2{*}@20366 to release lock on <0x5094> (a int[])
      at org.apache.velocity.runtime.directive.RuntimeMacro.init(RuntimeMacro.java:133)
      at org.apache.velocity.runtime.parser.node.ASTDirective.init(ASTDirective.java:197)
      - locked <0x5093> (a org.apache.velocity.runtime.parser.node.ASTDirective)
      at org.apache.velocity.runtime.parser.node.SimpleNode.init(SimpleNode.java:400)
      at org.apache.velocity.runtime.parser.node.ASTprocess.init(ASTprocess.java:64)
      at org.apache.velocity.Template.initDocument(Template.java:230)
      at org.apache.velocity.Template.process(Template.java:146)
      at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:429)
      at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:346)
      at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1696)
      at org.apache.velocity.runtime.directive.Parse.getTemplate(Parse.java:356)
      at org.apache.velocity.runtime.directive.Parse.render(Parse.java:220)
      at org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.java:304)
      at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:439)
      at org.apache.velocity.Template.merge(Template.java:358)
      at org.apache.velocity.Template.merge(Template.java:262)
      at org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEngine.java:295)
      at io.micronaut.views.velocity.VelocityViewsRenderer.render(VelocityViewsRenderer.java:92)

"{*}javamail-executor-thread-2{*}@20366" tid=0xe2 nid=NA runnable
  java.lang.Thread.State: RUNNABLE
      at org.apache.velocity.runtime.directive.RuntimeMacro.init(RuntimeMacro.java:133)
      at org.apache.velocity.runtime.parser.node.ASTDirective.init(ASTDirective.java:197)
      - locked <{*}0x5095{*}> (a org.apache.velocity.runtime.parser.node.ASTDirective)
      at org.apache.velocity.runtime.parser.node.SimpleNode.init(SimpleNode.java:400)
      at org.apache.velocity.runtime.parser.node.ASTprocess.init(ASTprocess.java:64)
      at org.apache.velocity.Template.initDocument(Template.java:230)
      at org.apache.velocity.Template.process(Template.java:146)
      at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:429)
      at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:346)
      at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1696)
      at org.apache.velocity.runtime.directive.Parse.getTemplate(Parse.java:356)
      at org.apache.velocity.runtime.directive.Parse.render(Parse.java:220)
      at org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.java:304)
      at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:439)
      at org.apache.velocity.Template.merge(Template.java:358)
      at org.apache.velocity.Template.merge(Template.java:262)
      at org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEngine.java:295)
      at io.micronaut.views.velocity.VelocityViewsRenderer.render(VelocityViewsRenderer.java:92)

 

This is probably because the reentrant Node.init() is synchronized in the ASTDirective.
{color:#b3ae60}@Override
{color}{color:#cf8e6d}public synchronized {color}Object {color:#56a8f5}init{color}(InternalContextAdapter context, Object data)
{color:#cf8e6d}throws {color}TemplateInitException
{
Token t;

{color:#7a7e85}/* method is synchronized to avoid concurrent directive initialization **/
{color}{color:#7a7e85}
{color}{color:#7a7e85} {color}{color:#cf8e6d}if {color}(!{color:#c77dbb}isInitialized{color})
See [https://github.com/apache/velocity-engine/blob/master/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTDirective.java#L101]

My velocity template defines a runtime macro for i18n, processed as an ASTDirective, and the content of the macro is also processed as an ASTDirective, causing the reentrant deadlock.
#macro(msg $key) $messages.getMessage($key, $key, $locale) #end
#parse("templates/header.vm")



--
This message was sent by Atlassian Jira
(v8.20.10#820010)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.