Skip to content

fix(imap): apply flag changes to large folders on the server - #14328

Open
DerDreschner wants to merge 1 commit into
mainfrom
fix/imap-flag-long-uid-sets
Open

DerDreschner wants to merge 1 commit into
mainfrom
fix/imap-flag-long-uid-sets

Conversation

@DerDreschner

Copy link
Copy Markdown
Member

Context

Marking a folder with more than about 10k messages as read sent one UID STORE with every UID, unsorted. Dovecot rejects the command as too long, and Horde ignores a rejected STORE: the database shows the messages as read, the server keeps them unread.

Regression from 8994221 (JMAP part 2), main only; 5.13 uses a single STORE 1:* and isn't affected.

Approach

  • Sort the UIDs so they compress into ranges, and split them at Horde's command length (the one it uses for FETCH).
  • Log in before reading the capabilities; before login they lack CONDSTORE and with it the longer command length.

Left out: the chunks aren't atomic (the next sync reconciles the flags if a later chunk fails), and Horde still ignores NO/BAD on STORE (needs a _store override in HordeImapClient).

Testing

Unit tests for unsorted UIDs as one range, splitting sparse UIDs, empty input and a failing chunk.

Mark 20k messages as read (Dovecot, MariaDB, CLI):

Time Peak memory IMAP commands Unread on the server after
Consecutive UIDs, main 3.44 s 92.7 MB 5 20,000
Consecutive UIDs, this PR 4.15 s 111.3 MB 5 0
Sparse UIDs, main 3.06 s 92.7 MB 5 20,000
Sparse UIDs, this PR 3.76 s 95.4 MB 19 0

Slightly slower because the server now applies the change and reports the flag updates.

AI assistance

Assisted by ClaudeCode:claude-opus-5-5

Marking a folder with more than about 10,000 messages as read sent one
UID STORE with every UID, unsorted. Dovecot rejects such a long command
with BAD, and Horde ignores a rejected STORE, so the database showed the
messages as read while they stayed unread on the server.

Send the UIDs sorted, so they compress into ranges, and split them at the
command length Horde uses for its own FETCH. Log in before reading the
capabilities, because the list before login lacks CONDSTORE and with it
the longer command length.

Regression from 8994221 (JMAP part 2). 5.13 marks a folder as read
with a single STORE for all messages and is not affected.

Left out: the STORE chunks are not atomic, so if a later chunk fails, the
next sync reconciles the flags. Horde still ignores NO/BAD replies to
STORE; catching them needs a _store override in HordeImapClient.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: David Dreschner <david.dreschner@nextcloud.com>
ChristophWurst

This comment was marked as low quality.

@ChristophWurst ChristophWurst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — approve.

The sorting + split($capability->cmdlength) fixes the over-long UID STORE, and calling login() first is safe: it's idempotent in Horde (_isAuthenticated guard) and store() would log in anyway. Verified the Horde APIs against vendor (Ids::split(), cmdlength = 8000 with CONDSTORE else 2000) and ran MessageMapperTest locally — the four new tests pass.

Two optional follow-ups, non-blocking:

  • The root cause is still open: Horde ignores NO/BAD on STORE, so a rejected chunk is silent until the next sync. A _store override in HordeImapClient would catch it — worth a tracking issue linked here.
  • Would be nice to assert login() is invoked (e.g. $client->expects($this->once())->method('login')) to lock in the login-before-capability ordering.

On the 1:* question: markAllRead() still does a single STORE 1:* and is untouched, but markFolderAsRead can't just go back to it — since 8994221 the local DB flags are updated per message inside flagMessages(). A 1:* fast path would need a bulk UPDATE on the DB side alongside it to keep local state in sync. Happy to leave that as a separate optimization; this fix correctly covers the general case (large selections, tags, threads), not just mark-all-read.

Assisted by OpenCode:mistral-large-4

@nextcloud nextcloud deleted a comment from DerDreschner Nov 26, 2026
@ChristophWurst

Copy link
Copy Markdown
Member

Sorry, wrong comment got deleted 🙄

Assisted by void

@ChristophWurst

Copy link
Copy Markdown
Member

@DerDreschner please share your findings

@DerDreschner

Copy link
Copy Markdown
Member Author

We can, and it's worth it: in a prototype, Mark all as read on 20k unread messages took 1.3–1.5 s with 1:* and a single DB UPDATE, against 4.2 s and 20,227 DB statements on main with this PR. But it belongs in its own PR:

Why separately: since JMAP part 2, Mark all as read goes through IMessageConnector::flagMessages() with every cached message of the folder. 1:* needs a folder-level method on the connector instead, implemented by both protocols:

  • IMAP: one STORE 1:* +FLAGS (\Seen); IMAP\MessageMapper::markAllRead() is still there, unused.
  • JMAP has no equivalent. RFC 8620/8621 only allow Email/set per id, at most maxObjectsInSet per call, so JMAP needs a query for the unread ids plus chunked Email/set calls. Today's JMAP code doesn't chunk Email/set at all: Mark all as read above 500 messages fails with requestTooLarge on Stalwart. I'm adding chunked /set to jmap-client-php's Traverser, next to the /get paging that fix(jmap): sync and search large mailboxes beyond the server page limit #13957 uses, and will switch Mail to it.
  • DB: one UPDATE … WHERE mailbox_id = ? instead of loading every message and dispatching a MessageFlaggedEvent for each, whose listener then updates every row a second time.
  • Behavior: 1:* also marks mail that arrived after the last sync, as 5.13 did.

That's a connector interface change, a new mapper query and different event semantics, which deserves its own review rather than riding along with a regression fix.

Why this PR is needed anyway: addFlag()/removeFlag() aren't only used by Mark all as read. Flag and tag changes on a selection send their UIDs the same way, and a large selection exceeds the server's command length just the same: Dovecot answers BAD, Horde ignores that, and the database shows the change while the server never got it. A folder-level operation would take Mark all as read off this path, but the other callers still need it.

Assisted by ClaudeCode:claude-opus-5-5

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3. to review AI assisted This PR contains AI-assisted commits regression

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants