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

   ## Summary
   
   _Claude Code on behalf of gnodet_
   
   CAMEL-23945: `LangChain4jAgentProducer.process()` had a race condition when 
using `AgentFactory` — the factory-created agent was written to the shared 
`agent` instance field and then re-read. Since a producer is a singleton per 
endpoint and `process()` must be thread-safe, concurrent exchanges selecting 
different agents could see each other's agents.
   
   **The race was inadvertently fixed on `main`** in CAMEL-23697 (commit 
`03d8361bbb74`, 2026-06-17) which refactored the method to use a local variable:
   
   ```java
   // Before (race): wrote to instance field, then re-read it
   if (agentFactory != null) {
       agent = agentFactory.createAgent(exchange);
   }
   String response = agent.chat(aiAgentBody, toolProvider);
   
   // After (safe): local variable, thread-confined
   Agent agent = agentFactory != null ? agentFactory.createAgent(exchange) : 
this.agent;
   Result<String> result = agent.chat(aiAgentBody, toolProvider);
   ```
   
   This PR adds a **regression test** that:
   - Configures a `PerExchangeAgentFactory` that creates a distinct agent per 
exchange (based on a header)
   - Sends 50 concurrent exchanges through a single producer endpoint using a 
`CountDownLatch` to maximise contention
   - Verifies each exchange received the response from its own agent, not from 
another thread's agent
   
   ## Test plan
   
   - [x] New test `LangChain4jAgentProducerConcurrencyTest` passes (1 test, 0 
failures)
   - [x] All existing module tests pass (27 tests, 0 failures)
   - [x] Code formatting validated (`-Psourcecheck validate`)
   
   > **Note**: The JIRA issue also mentions the fix is needed on 
`camel-4.18.x`. A separate backport PR should be created for that maintenance 
branch with the actual code fix (local variable pattern) since `camel-4.18.x` 
still has the racy instance-field write.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   
   Co-Authored-By: Claude Opus 4.6 <[email protected]>


-- 
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