shashank created CAMEL-27529:
--------------------------------

             Summary: camel-mail - consumers that share an idempotent 
repository route the same mail twice with the default 
idempotentRepositoryRemoveOnCommit=true
                 Key: CAMEL-27529
                 URL: https://issues.apache.org/jira/browse/CAMEL-27529
             Project: Camel
          Issue Type: Bug
          Components: camel-mail
            Reporter: shashank
            Assignee: shashank


h3. What happens

The {{idempotentRepository}} option is documented as the way to cluster 
consuming from the same mailbox. By default 
({{idempotentRepositoryRemoveOnCommit=true}}) the key of a mail is removed once 
the mail is marked SEEN (or deleted), since the mark prevents it from being 
consumed again. But another consumer can search the unseen mails before the 
first one marks the mail, and add the key after the first one removed it: the 
add succeeds and the mail is routed a second time. Only the DELETED flag is 
checked before routing, so with the default {{delete=false}} nothing stops it. 
One consumer whose exchanges complete after the poll (pollEnrich, 
ConsumerTemplate) has the same window between its next search and the 
completion of an earlier exchange.

h3. How to reproduce

Two routes on one GreenMail mailbox with one mail, sharing a 
{{MemoryIdempotentRepository}}; route B's {{mailUidGenerator}} (called between 
the search and the add) waits until route A routed the mail, marked it SEEN and 
removed its key. Route B then routes the same mail too.

Found with a TLA+ model of the search, the idempotent add and the commit with 
two consumers ("a mail that is SEEN is never routed" fails), and reproduced 
against the real consumer.

h3. Fix

After the key was added, the consumer asks the server whether the mail is SEEN 
now (a SEARCH restricted to that one mail, one round trip per mail whose key 
was added) and skips it, removing the key again, when it is. The cached flags 
of the mail object cannot be used: IMAPMessage fetches them once per object, 
and with {{closeFolder=false}} the next poll gets the mail objects of an 
earlier poll back, with the flags loaded then (for instance when the consumer 
routed the mail before and the exchange failed). This applies with IMAP, the 
default unseen search ({{unseen=true}}, no custom {{searchTerm}}) and 
{{peek=true}}; {{setPeek}} is now applied before the key is generated, so a 
custom {{mailUidGenerator}} that reads the mail does not flag it SEEN itself. 
POP3 has no SEEN flag, and a custom search term or {{unseen=false}} does not 
say that SEEN means consumed: the component page now says that consumers 
sharing a repository then need {{idempotentRepositoryRemoveOnCommit=false}}.

_Filed with Claude Code on behalf of allthingssecurity._



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

Reply via email to