Skip to content

fix(sync): list a cached mailbox while it syncs - #14342

Open
DerDreschner wants to merge 2 commits into
mainfrom
fix/list-mailbox-during-sync
Open

DerDreschner wants to merge 2 commits into
mainfrom
fix/list-mailbox-during-sync

Conversation

@DerDreschner

@DerDreschner DerDreschner commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Context

Ref #14341 (the silent 15 s retry while a sync holds the lock)

The message list returned 409 for any mailbox with an active sync lock. The folder showed the loading skeleton and retried every 15 s for as long as a sync ran: up to 5 minutes on main, the whole sync with #14339. The check comes from 6d2d36a, when the initial sync still deleted the cache first; bff314c removed that three weeks later.

Approach

  • List a cached mailbox while it syncs. A mailbox that isn't cached yet still returns 409 while locked, so the client waits for another process's initial sync.
  • Read the mailbox again after its messages and previews (one primary-key select), so a cache reset committed in between returns 409 instead of an empty list.
  • Reset the cache (clear cache, UIDVALIDITY change, no mod-sequences) in one transaction. Before, recipients, messages and tokens were committed separately, so the list could show messages without a sender, or none, while the mailbox still counted as cached.
  • Clear the tokens with an explicit update: saving the entity skipped them when it was loaded before a sync stored them, leaving a "cached" mailbox without messages.

Left out:

  • The client retries a locked sync every 1.5 s, each with an IMAP login, and now gets there right after opening a folder during a sync. Separate PR.
  • A threaded list can show new messages of a running sync before their threads are built. That already happens on main between a mailbox's sync and the end of the account sync, when threads are rebuilt.
  • JMAP's clearCache resets in the same non-atomic order (JMAP never took these locks).
  • A reset is now one transaction, recipients included; on Galera that is one large write set for very large mailboxes.

Testing

Unit tests for listing while locked (cached, not cached, expired lock, a reset committed while loading previews) and for the reset (commit order, UIDVALIDITY path, rollback without saving empty tokens). Mapper integration test for clearing the tokens of a stale entity on SQLite and MariaDB. Integration tests with Dovecot and Stalwart.

Dovecot, MariaDB, 20k messages:

main list change only this PR
12 list requests during a 30 s sync of 20k new messages 12 × 409 12 × listed 12 × listed, new messages appear chunk by chunk
40 list requests during "clear cache" 409 2 × 20 messages without a sender complete list, then 409 / not cached
~3,000 list requests during 60 resets of a 300-message folder 409 1 × empty list 0 × empty list
6,000 list requests (4 clients, every 50 ms) during 3 QRESYNC syncs — — 0 errors

Benchmark:

main this PR
List request on an idle folder, median of 3 × 500 1.52–1.72 ms 1.64–1.85 ms
"Clear cache", median of 5 338 ms 306 ms
Partial sync of 20k new messages, list polled every second (3 runs) 28.0 / 28.8 / 28.3 s 28.4 / 28.0 / 27.9 s

AI assistance

Assisted by ClaudeCode:claude-opus-5-5

Resetting the cache of a mailbox (clear cache, UIDVALIDITY change, no
mod-sequence support) deleted the recipients in chunks, then the
messages, and only then cleared the sync tokens, each committed on its
own. In between, the mailbox still counted as cached while its messages
had no sender or were gone, and a failure in between left it that way.

Run the delete and the token reset in one transaction, retried on
deadlocks. Clear the tokens with an explicit update instead of saving
the entity: the entity setter skips values it already holds, so a
mailbox loaded before a sync stored its tokens kept them while all its
messages were deleted. The entity only gets the empty tokens after the
commit, because unlocking saves it.

Left out: JMAP's clearCache resets in the same non-atomic order. The
recipients stay in the transaction; deleting them after the commit
would keep it smaller for very large mailboxes, but would leave orphans
to the cleanup job when it fails.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: David Dreschner <david.dreschner@nextcloud.com>
The message list refused any mailbox with an active sync lock, so the
folder showed a loading skeleton and retried every 15 s for as long as
a sync ran: up to 5 minutes, or the whole sync once the locks are
refreshed during long syncs (#14339). The check comes from 6d2d36a,
when the initial sync still deleted the cache first; bff314c removed
that three weeks later.

Keep refusing mailboxes that are not cached yet, so the client waits
for another process's initial sync instead of starting one too. Read
the mailbox again after its messages and previews, so a cache reset
committed in between returns 409 instead of an empty list.

Left out: the client retries a locked sync every 1.5 s, each retry
logging in to IMAP, and now gets there right after opening a folder
during a sync; that retry gets its own change. A threaded list can show
new messages of a running sync before their threads are built, as it
already did between a mailbox's sync and the end of the account sync.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: David Dreschner <david.dreschner@nextcloud.com>
@DerDreschner DerDreschner self-assigned this Nov 26, 2026
@nextcloud-command nextcloud-command added the AI assisted This PR contains AI-assisted commits label Nov 26, 2026
@DerDreschner

Copy link
Copy Markdown
Member Author

/backport to stable5.13

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 backport-request bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants