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

Claude Brisson commented on VELOCITY-934:
-----------------------------------------

The design choices never changed: the engine is threadsafe, but the context 
isn't. We might expect that contexts become threadsafe when used in a read-only 
mode, alas it's not the case with the current code.

The concurrent modifications that pose a problem here are a mechanism by which 
directives (`#foreach` in this case) put their own scope (`$foreach`) in the 
context before rendering and remove it afterwards.

I see two workarounds:

1. If the `$foreach` scope is not needed, it's possible to deactivate it using 
the setting `context.scope_control.foreach = false`.

2. If it is needed (which is the case here), the natural workaround is to use a 
wrapping context:

    @Benchmark
    public String benchmark() {
        Writer writer = new StringWriter();
        template.merge(new VelocityContext(context), writer);
        return writer.toString();
    }

Please note that the initial context content will *not* be copied, only wrapped 
as an inner context in the new one (this is not a copy constructor).

My proposal is to add those details to the documentation.


> NullPointerException under high concurrency
> -------------------------------------------
>
>                 Key: VELOCITY-934
>                 URL: https://issues.apache.org/jira/browse/VELOCITY-934
>             Project: Velocity
>          Issue Type: Bug
>          Components: Engine
>    Affects Versions: 1.7.x, 2.2
>         Environment: Windows 10 Professional
>            Reporter: Andreas Hager
>            Priority: Major
>
> I adjusted the mboseke/template-benchmark to profile throughput on AMD Ryzen 
> 5950x:
> [https://github.com/casid/template-benchmark/tree/ryzen-5950x]
> With 32 concurrent threads, I can reliably reproduce a runtime exception in 
> the Apache Velocity benchmark:
> {code:java}
> java.lang.NullPointerException
>         at 
> org.apache.velocity.runtime.directive.Directive.postRender(Directive.java:240)
>         at 
> org.apache.velocity.runtime.directive.Foreach.clean(Foreach.java:325)
>         at 
> org.apache.velocity.runtime.directive.Foreach.render(Foreach.java:292)
>         at 
> org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.java:301)
>         at 
> org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:423)
>         at org.apache.velocity.Template.merge(Template.java:358)
>         at org.apache.velocity.Template.merge(Template.java:262)
>         at com.mitchellbosecke.benchmark.Velocity.benchmark(Velocity.java:34)
>         at 
> com.mitchellbosecke.benchmark.generated.Velocity_benchmark_jmhTest.benchmark_thrpt_jmhStub(Velocity_benchmark_jmhTest.java:122)
>         at 
> com.mitchellbosecke.benchmark.generated.Velocity_benchmark_jmhTest.benchmark_Throughput(Velocity_benchmark_jmhTest.java:69)
>         at jdk.internal.reflect.GeneratedMethodAccessor1.invoke(Unknown 
> Source)
>         at 
> java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>         at java.base/java.lang.reflect.Method.invoke(Method.java:566)
>         at 
> org.openjdk.jmh.runner.BenchmarkHandler$BenchmarkTask.call(BenchmarkHandler.java:430)
>         at 
> org.openjdk.jmh.runner.BenchmarkHandler$BenchmarkTask.call(BenchmarkHandler.java:412)
>         at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
>         at 
> java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
>         at 
> java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
>         at java.base/java.lang.Thread.run(Thread.java:834)
> {code}
> This initially happened with version 1.7, but after updating to 2.2 the 
> problem is still there. It looks like there is some threading issue under 
> high concurrency.
> This is the velocity benchmark class:
> [https://github.com/casid/template-benchmark/blob/ryzen-5950x/src/main/java/com/mitchellbosecke/benchmark/Velocity.java]
> And this is the velocity template:
> [https://github.com/casid/template-benchmark/blob/ryzen-5950x/src/main/resources/templates/stocks.velocity.html]
>  
>  
>  
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@velocity.apache.org
For additional commands, e-mail: dev-h...@velocity.apache.org

Reply via email to