fix: batch and compress UID FETCH for large mailboxes#2
Open
furriest wants to merge 1 commit into
Open
Conversation
fetchSortMetadata() and the hasAttachment post-filter each built a single UID FETCH command by joining every matching UID with commas (e.g. UID FETCH 1,2,3,...,2505 (...)). On mailboxes with a few thousand messages this produces a command line long enough that some IMAP servers (observed with MDaemon) reject it or drop the connection outright. The resulting IMAP error wasn't surfaced as a JMAP error, so JMAP clients saw an empty mailbox instead of a fetch failure. Fix: sort UIDs, chunk them into batches of 200, and compress each batch into IMAP sequence-set ranges (e.g. "100:105,110,204:210") instead of a flat comma list. Cuts both the number of round-trips and the command length for large, mostly-contiguous mailboxes. Applies to both fetchSortMetadata() (Email/query sort/fetch path) and the hasAttachment post-filter, which had the same pattern.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Mailboxes with a few thousand messages (~2500 in the reported case, against Kerio Connect) appeared empty in JMAP clients while
Mailbox/getcorrectly reported the total message count.Root cause
fetchSortMetadata()and thehasAttachmentpost-filter each build a UID FETCH command by joining every matching UID with commas:For a mailbox with thousands of messages this produces a command line long enough to exceed the line-length limits some IMAP servers enforce more strictly than Dovecot/Gmail (observed with MDaemon). The FETCH fails or the connection drops, and the resulting error isn't surfaced as a JMAP-level error — the client just sees an empty result set.
Fix
compressUidsToRanges), e.g.[100,101,102,110]→"100:102,110", instead of a flat comma-separated list.fetchSortMetadata(theEmail/querysort/fetch path) and thehasAttachmentpost-filter, which had the same pattern.Testing
npx tsc -p tsconfig.jsonpasses clean.Email/queryresult, now returns correctly paginated results.Happy to add a regression test against the integration compose stack if useful — wasn't sure whether you'd rather fake a large mailbox in
compose.test.ymlor mock at theImapFlowlevel.