Skip to content

CAMEL-23945: Fix producer race on shared agent field (backport to 4.18.x) - #25063

Merged
davsclaus merged 1 commit into
apache:camel-4.18.xfrom
gnodet:fix/CAMEL-23945-backport-4.18.x
Jul 24, 2026
Merged

CAMEL-23945: Fix producer race on shared agent field (backport to 4.18.x)#25063
davsclaus merged 1 commit into
apache:camel-4.18.xfrom
gnodet:fix/CAMEL-23945-backport-4.18.x

Conversation

@gnodet

@gnodet gnodet commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

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)

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

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

  • New LangChain4jAgentProducerConcurrencyTest passes (1 test)
  • All existing unit tests in the module pass (7 total)
  • CI green

🤖 Generated with Claude Code

…on test

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@davsclaus
davsclaus marked this pull request as ready for review July 24, 2026 06:52
@davsclaus davsclaus added this to the 4.18.4 milestone Jul 24, 2026
@davsclaus davsclaus added the bug Something isn't working label Jul 24, 2026
@davsclaus
davsclaus merged commit cacb8f2 into apache:camel-4.18.x Jul 24, 2026
3 checks passed
@gnodet
gnodet deleted the fix/CAMEL-23945-backport-4.18.x branch July 24, 2026 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants