Federico Mariani created CAMEL-24690:
----------------------------------------
Summary: camel-python (Jython) - PythonExpression is not thread
safe
Key: CAMEL-24690
URL: https://issues.apache.org/jira/browse/CAMEL-24690
Project: Camel
Issue Type: Bug
Affects Versions: 4.22.0
Reporter: Federico Mariani
{{PythonExpression}} creates one {{PythonInterpreter}} per expression and
{{evaluate}} does
{code:java}
compiler.set("exchange", exchange);
compiler.set("body", exchange.getMessage().getBody());
...
PyObject out = compiler.eval(compiledExpression);
...
finally { compiler.cleanup(); }
{code}
with no synchronization. Two exchanges evaluated concurrently on the same
expression interleave the {{set()}} calls on the interpreter's shared globals,
so thread A can evaluate with thread B's body, and {{cleanup()}} from one
thread runs while the other is mid-eval. {{PythonLanguage.evaluate}} has the
same race on the shared interpreter.
Also: every result is stringified with {{out.toString()}} so lists, dicts and
numbers lose their type, and all failures, including runtime ones, are reported
as {{ExpressionIllegalSyntaxException}}.
Suggested fix: run the compiled {{PyCode}} with per-call locals
({{Py.runCode(code, locals, globals)}}) instead of mutating the interpreter
globals, or synchronize / use a ThreadLocal interpreter; and convert results
through {{__tojava__}} instead of {{toString}}. Found while reviewing the
scripting languages for the benchmark comparison (not benchmarked).
----
_Found during a benchmark review of camel-groovy and the other scripting
languages (camel-performance-tests branch camel-groovy-perf,
tests/camel-jmh/groovy-results, JMH 1.37, JDK 21, one M1 Pro laptop). A
candidate fix with tests exists on the local apache/camel branch
camel-perf-all-fixes; a PR follows._
_Claude Code on behalf of Croway_
--
This message was sent by Atlassian Jira
(v8.20.10#820010)