[
https://issues.apache.org/jira/browse/CAMEL-27531?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
shashank updated CAMEL-27531:
-----------------------------
Fix Version/s: 4.24.0
(was: 4.25.0)
> camel-mail - the mail of an exchange that completes after the poll
> (pollEnrich, ConsumerTemplate) is not marked with disconnect=true, or another
> mail is marked when the folder is closed in between
> ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> Key: CAMEL-27531
> URL: https://issues.apache.org/jira/browse/CAMEL-27531
> Project: Camel
> Issue Type: Bug
> Components: camel-mail
> Reporter: shashank
> Assignee: shashank
> Priority: Major
> Fix For: 4.24.0
>
>
> h3. What happens
> The consumer marks a mail (SEEN, DELETED, copyTo/moveTo) when its exchange
> completes. With {{pollEnrich}} or a {{ConsumerTemplate}}, the polling
> consumer only queues the exchange, and the exchange completes later on the
> caller's thread, while the poll thread goes on: it closes the folder
> ({{closeFolder=true}}), disconnects ({{disconnect=true}}) or, in the next
> poll, connects again.
> * With {{disconnect=true}} the poll sets the folder to null. The completion
> then fails with a {{NullPointerException}} in {{mailToCommit}}
> ({{folder.isOpen()}}), which {{UnitOfWorkHelper}} only logs: the mail is
> never marked and its idempotent key stays added. With {{delete=true}} every
> {{pollEnrich}} gets the same mail again and it is never deleted; with an
> idempotent repository the unseen mail is skipped as a duplicate for good. The
> same NPE happens when the poll re-creates the connection (ensureIsConnected)
> while an exchange of the previous poll completes.
> * {{mailToCommit}} checks that the mail object belongs to the current open of
> the folder, then {{processCommit}} checks {{folder.isOpen()}} again and, when
> the poll (or a dropped connection, or a stop) closed the folder in between,
> opens it itself and marks the same mail object. After an expunge its message
> number designates another mail: that mail is marked SEEN, or DELETED and
> expunged without having been routed. This is the case of CAMEL-25808,
> reopened by the second reopen in {{processCommit}}.
> * When marking fails ({{MessageRemovedException}}, {{FolderClosedException}},
> a failed copy, {{IllegalStateException}}), {{processCommit}} reports the
> error but leaves the idempotent key added, so the unmarked mail is skipped
> until the repository forgets the key, while {{mailToCommit}} settles the key
> in the same situation.
> * An exchange that completes after the consumer was stopped (the stop closed
> the folder and the store) opens the folder again: {{IMAPFolder.open}} gets a
> connection from {{IMAPStore.getProtocol}}, which logs in with the credentials
> the closed store kept without checking that the store is connected. The
> stopped consumer marks the mail and leaves an open IMAP connection that
> nobody closes.
> h3. How to reproduce
> GreenMail IMAP, one mail, {{disconnect=true&idempotentRepository=#repo}}:
> {{consumer.receive(uri)}} then {{consumer.doneUoW(exchange)}}: the mail is
> still unseen, its key is still in the repository, and the next {{receive}}
> returns null. With {{pollEnrich(uri + "&disconnect=true&delete=true")}} the
> same mail is enriched again on every call.
> With {{closeFolder=true&maxMessagesPerPoll=2}} and three mails, the exchange
> of the second mail completes on another thread after {{mailToCommit}}
> approved it, the poll closes the folder and another client expunges the first
> mail: the third mail, never retrieved, is marked SEEN (or, with
> {{delete=true}} and no other client, deleted).
> Found with a TLA+ model of the poll thread, the completions, folder
> close/reopen/disconnect, expunges and connection drops, and reproduced
> against the real consumer and GreenMail.
> h3. Fix
> The poll connects and opens the folder, and closes it and disconnects, while
> holding a lock of the consumer; the completion checks and marks the mail
> while holding the same lock, so the folder cannot be closed or replaced in
> between. The lock is never held while a mail is routed (the completion of a
> mail routed by the poll thread takes it too), and {{doStop}} closes the
> folder and the store while holding it. A completion after the stop does not
> open the folder or connect: the mail is not marked and its key is settled. A
> poll that is running when the consumer stops does not log in again either: it
> checks the stop under the lock before it connects, and again before it reads
> the message count and the UIDVALIDITY of the folder it opened (on a closed
> IMAP folder both send a STATUS through {{IMAPStore.getStoreProtocol}}, which
> logs in with the credentials the closed store kept); its later calls on the
> closed folder ({{search}}, {{getMessage}}, {{fetch}}, {{getUID}}, reading a
> mail) fail without connecting. When the completion finds no folder (the poll
> disconnected), it connects again and finds the mail by its UID (IMAP) or key
> (POP3), as after any other new open, marks it, and disconnects again with
> {{disconnect=true}}. {{processCommit}} no longer opens an IMAP folder again
> for a mail object that was already checked: a folder closed in between
> (dropped connection, stop) means the mail is not marked. In every case where
> the mail is not marked, the key is settled as on commit (removed by default,
> confirmed with {{idempotentRepositoryRemoveOnCommit=false}}).
> _Filed with Claude Code on behalf of allthingssecurity._
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)