shashank created CAMEL-27411:
--------------------------------

             Summary: camel-core - parallel Multicast, Split, Recipient List: 
the exchange can complete before the last part is aggregated, and a timeout can 
report the wrong index to the AggregationStrategy
                 Key: CAMEL-27411
                 URL: https://issues.apache.org/jira/browse/CAMEL-27411
             Project: Camel
          Issue Type: Bug
          Components: camel-core
            Reporter: shashank
            Assignee: shashank


h3. What happens

*1. A part can be lost (parallelProcessing, ordered or streaming).* 
{{MulticastTask.aggregateCompleted}} decides that it is done with 
{{nbAggregated.incrementAndGet() >= nbExchangeSent.get() && allSent.get()}}. 
The sender task increments {{nbExchangeSent}} and then sets {{allSent}} for the 
last part, without the lock. When the thread that aggregates a part reads 
{{nbExchangeSent}} just before the sender numbers the last part, and reads 
{{allSent}} just after the sender set it, it sees every part as aggregated and 
completes the exchange. The last part is still processed, but its result is 
never aggregated (the aggregation loop stops once the exchange is done). 
Nothing is logged and no exception is set: the route goes on with an incomplete 
aggregate. No timeout, failure or {{stopOnException}} is needed: a plain 
{{multicast().parallelProcessing()}} or {{split().parallelProcessing()}} can 
hit it. The window is small (the aggregating thread must be preempted between 
two adjacent reads), so it is rare: a two-endpoint parallel multicast run about 
14 million times gave one result with a part missing.

*2. Streaming timeout reports the wrong index.* {{MulticastTask.timeout()}} 
uses the number of aggregated exchanges ({{nbAggregated}}) as the next index to 
report to {{AggregationStrategy.timeout(exchange, index, total, timeout)}}. In 
streaming mode the exchanges are aggregated in the order they complete, so the 
aggregated indexes are not always the first ones. Example: a parallel streaming 
multicast to a (slow), b and c with a timeout. b and c are aggregated, so 
{{nbAggregated}} is 2, and the timeout reports index 2 (c, which was 
aggregated) and never index 0 (a, the one that timed out). A strategy that uses 
the index (for example to put a placeholder for the missing branch) marks the 
wrong branch. In ordered mode the same happens only when the timeout finds a 
completed sub-exchange whose index property an EIP inside the branch changed (a 
nested multicast or split leaves the index of its own last part): the timeout 
reads the index from that property.

h3. How to reproduce

1: a parallel streaming split of two parts whose iterator is slow to produce 
the second part, with the aggregating thread held for a moment between its read 
of {{nbExchangeSent}} and its read of {{allSent}} (done with a debugger 
breakpoint, no code change): the split completes with {{[p0-done]}} instead of 
{{[p0-done, p1-done]}}.

2: 
{{multicast(strategy).parallelProcessing().streaming().timeout(2000).to("direct:a",
 "direct:b", "direct:c")}} where direct:a waits past the timeout: 
{{strategy.timeout}} is called with index 2 instead of 0.

Found with a TLA+ model of the parallel multicast task (sender, completion 
threads, aggregation lock and the timeout task).

h3. Fix

1: read {{allSent}} before {{nbExchangeSent}}. The sender sets {{allSent}} 
after its last increment, so once {{allSent}} is read as true the count is 
final.

2: when there is a timeout, the task keeps the index of each sub-exchange it 
sends, so the timeout does not read the index property of the sub-exchange (an 
EIP inside the branch, such as a nested multicast or split, can change it). In 
streaming mode it also records the indexes that were aggregated or reported (a 
{{BitSet}} guarded by the aggregation lock), and the timeout reports the 
indexes that are not in it. Ordered mode still takes the number of aggregated 
exchanges as the next index, which is right there.

_Filed with Claude Code on behalf of allthingssecurity._



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to