gnodet opened a new pull request, #25063:
URL: https://github.com/apache/camel/pull/25063

   _Claude Code on behalf of gnodet_
   
   ## Summary
   
   Backport of the fix for CAMEL-23945 to the `camel-4.18.x` branch.
   
   On `main`, the race condition in `LangChain4jAgentProducer.process()` was 
inadvertently fixed by commit `03d8361bbb7` (CAMEL-23697), which refactored the 
method to use a local variable. On `camel-4.18.x`, the original race still 
exists: `agentFactory.createAgent(exchange)` writes to the shared `this.agent` 
instance field, so concurrent exchanges can see each other's agents.
   
   ### The race (before this PR)
   ```java
   if (agentFactory != null) {
       agent = agentFactory.createAgent(exchange);  // writes to shared field
   }
   AiAgentBody<?> aiAgentBody = agent.processBody(messagePayload, exchange);
   ```
   
   Between the write and the read, another thread can overwrite `this.agent` 
with a different exchange's agent, causing the first exchange to use the wrong 
agent.
   
   ### The fix
   ```java
   Agent agent = agentFactory != null ? agentFactory.createAgent(exchange) : 
this.agent;
   AiAgentBody<?> aiAgentBody = agent.processBody(messagePayload, exchange);
   ```
   
   A method-local variable ensures each exchange uses only its own 
factory-created agent.
   
   ### Changes
   - **`LangChain4jAgentProducer.java`**: Changed `process()` to use a local 
`Agent agent` variable instead of writing to the shared instance field
   - **`LangChain4jAgentProducerConcurrencyTest.java`**: Added regression test 
that sends 50 concurrent exchanges through a single producer endpoint, each 
with a distinct factory-created agent. Verifies that every exchange receives 
the response from its own agent, not from another exchange's agent.
   
   ## Test plan
   - [x] New `LangChain4jAgentProducerConcurrencyTest` passes (1 test)
   - [x] All existing unit tests in the module pass (7 total)
   - [ ] CI green
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to