V1.0.0: Email id changes when a concurrent import's thread is merged (Email/import id no longer in Email/query or Email/changes)

Issue Description

On v1.0.0, when two JMAP (or IMAP) imports into the same account start the same new thread at the same moment, each import first gets its own thread. A ThreadRecheck after commit then merges the two threads. Because the JMAP email id is built from the thread id and the document id, the merged email gets a new id. Email/query and Email/changes list only the new id, and never report the old one as destroyed. The id Email/import returned still resolves through Email/get, but with the old threadId. RFC 8621 §4.1.1 makes both id and threadId immutable, so a client that stored the import-time id loses track of the email for good, and nothing signals it. It happens in about 1 in 5 parallel-import races on v1.0.0 and never on v0.16.27 (0 of 15 runs). SMTP delivery doesn’t take this path.

Expected Behavior

RFC 8621 §4.1.1 marks id and threadId as immutable. The id Email/import returned should stay the email’s id after the thread is merged. Failing that, Email/changes should report the old id as destroyed and the new one as created, so a client that stored the import’s id can follow it.

Actual Behavior

for JMAP and IMAP ingests, ingest.rs:809-830 keeps a PendingThread. After commit, ThreadRecheck (message/ingest/recheck.rs, scheduling at line 111) finds the second thread created for the same messages and queues MergeThreads. The merged email moves to the other thread, and since the JMAP id is built from thread id and document id, its id changes. Meanwhile Email/get still accepts the old id, with the stale threadId.

Reproduction Steps

Two JMAP clients, in parallel, on one new account, each:

  1. Email/get with ids: [] → state: "n"
  2. upload the same message (same Message-ID) and Email/import it into the inbox
  3. Email/set a keyword on the returned id
  4. Email/changes from "n" until hasMoreChanges is false

In about 1 in 5 runs, one import returns an id that never appears again:

Email/import  -> created {"e": {"id": "iaaaaac", "threadId": "c"}}   newState "sam"
Email/changes (since "n") -> created ["eaaaaab", "eaaaaac"], updated [], newState "say"
Email/query   -> ids ["eaaaaab", "eaaaaac"]
Email/get ["iaaaaac", "eaaaaac"] -> both found: iaaaaac threadId "c", eaaaaac threadId "b"

Asking Email/changes again later from "n" gives the same result. The same two clients against v0.16.27 never produce this (0 of 15 runs).

Stalwart Version

v1.x

Installation Method

Docker

Database Backend

RocksDB

Blob Storage

RocksDB

Search Engine

Internal

Directory Backend

Internal

I have reviewed the documentation and FAQ and confirm that my issue is NOT addressed there.

on

I have searched this support forum (open and closed topics) and confirm this is not a duplicate.

on

I understand that topics in this category are triaged by a bot first but a human reply will follow up. If I’d prefer a human-only reply, I’ll add the no-ai tag to my topic.

on

Your description matches the merge path in the source, but I can’t find anything in the docs or code that promises a fix or a workaround.

In crates/services/src/task_manager/tasks/merge_threads.rs, the MergeThreads task picks one thread id to keep. It then deletes the other threads with log_container_delete(SyncCollection::Thread). For each message in the losing threads, it rewrites MessageData.thread_id to the surviving id. As you describe, the JMAP email id is built from the thread id and document id, so those messages get a new JMAP id. I only read the first 200 lines of the file. I haven’t confirmed whether the rest of the batch writes a changelog entry for the emails, so I can’t say if Email/changes is meant to report anything for the moved messages. In your trace it reports nothing for the old id.

That means v1.0 is currently violating the RFC 8621 §4.1.1 immutability of id and threadId in this race. A client can’t recover the import-time id from Email/changes or Email/query. Until this is addressed, a client could look the email up again by Message-ID with Email/query and a header filter. That is a workaround on the client side, not a fix.

This needs a human to confirm and triage. Your report includes the version, the 1-in-5 rate against 0 of 15 on v0.16.27, and the trace, which is the information they need.

This is an automated reply from the Stalwart Help Bot. Other community members may follow up if this answer is incomplete or wrong.

The id change itself is intended. A JMAP Email id includes its thread, and RFC 8621 section 3 says that when a message has to move to another thread, the server must delete it and insert it again with a new id. That is what happens when the thread recheck merges the two threads your parallel imports created. The merge logs the old id as destroyed and the new one as created.

Two things on our side made it look like the email went missing:

  1. Email/get (and Email/set, Email/copy, SearchSnippet/get) looked emails up by the document part of the id only. The destroyed id kept resolving and came back with its old threadId. These now return notFound for an id whose thread no longer matches.
  2. Email/import read its newState after the merge task had already been scheduled. If the merge committed first, the state you got back already included it, so Email/changes from that state never reported the imported id as destroyed. newState now reflects the import’s own commit, so asking for changes from it gives you the old id in destroyed and the new one in created.

From your earlier state n, the old id was created and destroyed within the same window, and RFC 8620 section 5.2 says such ids should be left out. That part of what you saw is expected and stays as it is.

The fix ships in the next weekly Stalwart release.