shashank created CAMEL-27297:
--------------------------------

             Summary: camel-core - Aggregate EIP with optimistic locking and 
closeCorrelationKeyOnCompletion can start a new group for a key that is being 
closed
                 Key: CAMEL-27297
                 URL: https://issues.apache.org/jira/browse/CAMEL-27297
             Project: Camel
          Issue Type: Bug
          Components: camel-core
            Reporter: shashank
            Assignee: shashank


h3. What happens

With {{closeCorrelationKeyOnCompletion}} and {{optimisticLocking}}, a 
completion of a group (by size, predicate, timeout, interval or force 
completion) first removes the group from the repository and only then puts the 
key into the closed correlation keys ({{AggregateProcessor.doOnCompletion}}). 
With optimistic locking the aggregation lock is a {{NoLock}}, so the closed key 
check under the lock (CAMEL-24941) is not atomic with the remove and the close.

An exchange that reads the repository between the remove and the close finds no 
group and an open key, and starts a new group for the key that is then closed. 
That group is later sent as a second aggregated exchange for the closed key (by 
the completion timeout, the completion interval or a force completion), while a 
later exchange for the key is rejected with {{ClosedCorrelationKeyException}}. 
The exchange can check the key in that window, or check it before the remove 
and read the repository after it.

The default (pessimistic) locking is not affected: the lock is held from the 
check to the add, and from the remove to the close.

h3. How to reproduce

{{AggregateOptimisticLockingClosedKeyTest}} (camel-core): a 
{{MemoryAggregationRepository(true)}} pauses the completing thread right after 
its remove, and a second exchange for the key runs in that window. It is 
accepted and the repository keeps a group for the closed key, which a later 
completion timeout, interval or force completion sends as a second aggregate.

Found with a TLA+ model of the Aggregate EIP with optimistic locking.

h3. Fix

A completion marks the key as closing (a count per key, kept apart from the 
closed keys) before it removes the group, and clears the mark once the key is 
closed or the remove failed. With optimistic locking the key is checked again 
right after the group is read from the repository: a closing key makes the 
exchange try again (as a failed optimistic add does), a closed key rejects it. 
A failed remove never reopens a key that another completion closed, and an LRU 
closed keys cache cannot evict a mark.

Not covered: a group that completes on its first exchange is never stored, so 
two such exchanges can both be sent; an exchange that read "no group" before 
another group was even created, and adds after that group was completed and the 
key closed (a stall over the whole life of another group).

_Filed with Claude Code on behalf of allthingssecurity._



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

Reply via email to