Package: neomutt
Version: 20250510+dfsg-2
Severity: grave
Justification: causes data loss
Tags: upstream patch
Forwarded: https://github.com/neomutt/neomutt/pull/5073

This is a data-loss bug in the Maildir backend. Since upstream commit
1ca157124 (April 2020, for neomutt issue #2077), a message whose file
disappears between two mailbox checks is kept in the index and marked
deleted+purge. If the file comes back under a new name with the same
canonical filename (which is what every flag change by another
program—offlineimap, mbsync, notmuch, a second NeoMutt—looks like when
it races with the check), the next check matches it and updates the
path and flags but keeps the deleted flag. The next sync ($, changing
folder, quitting) then unlinks the real file.

In my case 22 messages I had only read were unlinked locally and the
IMAP sync deleted them on the server (details in
https://github.com/neomutt/neomutt/issues/4332#issuecomment-6697941615).
They were recoverable only because Gmail keeps deleted mail in Trash.

A reproduction script that needs only one neomutt instance and a
temporary Maildir (no real mail) is attached. With 20250510+dfsg-2 it
prints "files after sync: 2" (one message unlinked); the expected
result is 3. The code is unchanged in 20260616+dfsg-1 and in upstream
main, which also reproduce.

I have sent a fix upstream: https://github.com/neomutt/neomutt/pull/5073

It makes NeoMutt do what OG Mutt does and removes vanished messages
from the index. A new NT_MAILBOX_PRUNE notification removes
quasi_deleted emails and nothing else, so pending user deletes and
other unsynced changes survive the reopen (unlike NT_MAILBOX_UPDATE,
which drops all deleted emails). mh had the same "message has
disappeared" case without any removal and now uses the same path. The
patch is attached and applies to the Debian source with minor offsets.

I tested this pretty thoroughly with a tmux-driven script against a
temp Maildir: a vanished-then-renamed file is no longer unlinked on
sync; d then $ still deletes; a file removed for good disappears from
the index with no dead entry; a message vanishing while open in the
pager doesn't crash; a pending D on another message survives the
reopen and is honored on sync; same vanish/return sequence on an MH
folder.

I think this is my first attempt to contribute to NeoMutt or Mutt, at
least at anything near this complexity, so I'd definitely appreciate
extra eyes on this one. Due to lack of familiarity, and as the commit
message says, the patch was also developed with generative AI (Claude
Fable 5.1), although more in a human-assistance sense.

-- System Information:
Debian Release: 13.7
  APT prefers stable-updates
  APT policy: (990, 'stable-updates'), (990, 'stable-security'), (990, 
'stable'), (500, 'unstable'), (100, 'unstable'), (100, 'testing'), (1, 
'experimental')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 6.12.113+deb13-amd64 (SMP w/20 CPU threads; PREEMPT)
Kernel taint flags: TAINT_USER, TAINT_WARN, TAINT_OOT_MODULE, 
TAINT_UNSIGNED_MODULE
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8) (ignored: LC_ALL 
set to en_US.UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /usr/bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled

Versions of packages neomutt depends on:
ii  libc6                2.41-12+deb13u4
ii  libgnutls30t64       3.8.9-3+deb13u4
ii  libgpgme11t64        1.24.2-3
ii  libgsasl18           2.2.2-1.1+deb13u2
ii  libgssapi-krb5-2     1.21.3-5+deb13u1
ii  libidn2-0            2.3.8-2
ii  liblmdb0             0.9.31-1+b2
ii  liblua5.4-0          5.4.7-1+b2
ii  liblz4-1             1.10.0-4
ii  libncursesw6         6.5+20250216-2
ii  libnotmuch5t64       0.39-1
ii  libpcre2-8-0         10.46-1~deb13u3
ii  libsqlite3-0         3.46.1-7+deb13u2
ii  libtinfo6            6.5+20250216-2
ii  libtokyocabinet9t64  1.4.48-15.1+b1
ii  libzstd1             1.5.7+dfsg-1
ii  sensible-utils       0.0.25
ii  zlib1g               1:1.3.dfsg+really1.3.1-1+b1

Versions of packages neomutt recommends:
ii  locales  2.41-12+deb13u4
ii  mailcap  3.74

Versions of packages neomutt suggests:
ii  aspell                          0.60.8.1-4
ii  ca-certificates                 20250419+deb13u1
ii  gnupg                           2.4.7-21+deb13u1
ii  ispell                          3.4.06-1
ii  openssl                         3.5.7-1~deb13u3
ii  postfix [mail-transport-agent]  3.10.13-0+deb13u1
ii  python3                         3.13.5-1
pn  urlview                         <none>

Versions of packages neomutt is related to:
ii  neomutt  20250510+dfsg-2+mako1

-- no debconf information


-- 
Benjamin Mako Hill
https://mako.cc/

Attachment: repro.sh
Description: Bourne shell script

From 14f97975513b2306bd7e81426fb4a52963db9285 Mon Sep 17 00:00:00 2001
From: Benjamin Mako Hill <[email protected]>
Date: Fri, 6 Nov 2026 15:23:57 -0800
Subject: [PATCH] maildir: drop vanished messages from the index instead of
 marking them deleted

Since 1ca157124 (#2077) in 2020, a Maildir message whose file
disappears between two mailbox checks is kept in the index and marked
deleted+purge.  If the file comes back under a new name with the same
canonical filename (which is what every flag change by another program,
such as offlineimap, mbsync, notmuch, or a second NeoMutt, looks like
when it races with the check), the next check matches it,
updates the path and flags, but keeps the deleted flag.  The next sync
then unlinks the real file. This means that messages the user had read
(but not flagged) were deleted (!!), and the IMAP sync propagated the
deletion to the server.

This patch removes the message from the index. A new NT_MAILBOX_PRUNE
notification removes these 'quasi_deleted' emails and nothing else, so
pending user deletes and other unsynced changes survive the reopen.
mh had the same "message has disappeared" case without any
removal. This patch means that it now uses the same path.

This patch fixes #4332, which is actually much worse than the original
issue suggests.

If the pruned message is the one shown in the pager, the pager returns
to the index rather than keep a handle whose position now belongs to a
neighbouring message.  In mh, a file whose content changed under the
same number is now treated as a new message (previously it produced a
duplicate index entry).

Note: I used an AI coding agent (Fable 5.1) as part of my workflow to
help prepare and review this change. That said, this code is (at most)
"AI assisted" and fully reflects my own work and judgement.
---
 core/mailbox.h      |   1 +
 debug/names.c       |   1 +
 mview.c         | 108 +++++++++++++++++++++++++++++++++++++-------
 index/shared_data.c |   4 ++
 maildir/mailbox.c   |  14 ++++--
 mh/mh.c             |   6 ++-
 6 files changed, 113 insertions(+), 21 deletions(-)

diff --git a/core/mailbox.h b/core/mailbox.h
index 5ad9ce3c5..9e09aa3dd 100644
--- a/core/mailbox.h
+++ b/core/mailbox.h
@@ -183,6 +183,7 @@ enum NotifyMailbox
   NT_MAILBOX_RESORT,     ///< Email list needs resorting
   NT_MAILBOX_UPDATE,     ///< Update internal tables
   NT_MAILBOX_UNTAG,      ///< Clear the 'last-tagged' pointer
+  NT_MAILBOX_PRUNE,      ///< Remove quasi-deleted Emails from the list
 };
 
 /**
diff --git a/debug/names.c b/debug/names.c
index 30fc61e47..ca095e46b 100644
--- a/debug/names.c
+++ b/debug/names.c
@@ -343,6 +343,7 @@ const char *name_notify_mailbox(int id)
     DEBUG_NAME(NT_MAILBOX_DELETE);
     DEBUG_NAME(NT_MAILBOX_DELETE_ALL);
     DEBUG_NAME(NT_MAILBOX_INVALID);
+    DEBUG_NAME(NT_MAILBOX_PRUNE);
     DEBUG_NAME(NT_MAILBOX_RESORT);
     DEBUG_NAME(NT_MAILBOX_UNTAG);
     DEBUG_NAME(NT_MAILBOX_UPDATE);
diff --git a/mview.c b/mview.c
index 660167ef4..1229c66ef 100644
--- a/mview.c
+++ b/mview.c
@@ -225,6 +225,94 @@ void mview_update(struct MailboxView *mv)
   mutt_sort_headers(mv, true);
 }
 
+/**
+ * remove_email - Detach an Email from a Mailbox's hash tables and queue it for freeing
+ * @param m Mailbox
+ * @param e Email
+ */
+static void remove_email(struct Mailbox *m, struct Email *e)
+{
+  if ((m->type == MUTT_NOTMUCH) || (m->type == MUTT_MH) ||
+      (m->type == MUTT_MAILDIR) || (m->type == MUTT_IMAP))
+  {
+    mailbox_size_sub(m, e);
+  }
+  /* remove message from the hash tables */
+  if (m->subj_hash && e->env->real_subj)
+    mutt_hash_delete(m->subj_hash, e->env->real_subj, e);
+  if (m->id_hash && e->env->message_id)
+    mutt_hash_delete(m->id_hash, e->env->message_id, e);
+  mutt_label_hash_remove(m, e);
+
+  if (m->type == MUTT_IMAP)
+    imap_notify_delete_email(m, e);
+
+  mailbox_gc_add(e);
+}
+
+/**
+ * prune_emails - Remove quasi-deleted Emails from a Mailbox
+ * @param mv Mailbox View
+ *
+ * Used when messages have vanished from the mailbox behind our back.
+ * Unlike update_tables(), this leaves deleted messages and pending changes alone.
+ */
+static void prune_emails(struct MailboxView *mv)
+{
+  if (!mv || !mv->mailbox)
+    return;
+
+  struct Mailbox *m = mv->mailbox;
+  const int padding = mx_msg_padding_size(m);
+
+  m->vcount = 0;
+  mv->vsize = 0;
+  int j = 0;
+  for (int i = 0; i < m->msg_count; i++)
+  {
+    struct Email *e = m->emails[i];
+    if (!e)
+      break;
+
+    if (e->quasi_deleted)
+    {
+      if (e->deleted)
+        m->msg_deleted--;
+      if (e->tagged)
+        m->msg_tagged--;
+      if (e->flagged)
+        m->msg_flagged--;
+      if (!e->read)
+      {
+        m->msg_unread--;
+        if (!e->old)
+          m->msg_new--;
+      }
+      e->vnum = -1;
+      e->visible = false;
+      remove_email(m, e);
+      m->emails[i] = NULL;
+      continue;
+    }
+
+    if (i != j)
+    {
+      m->emails[j] = e;
+      m->emails[i] = NULL;
+    }
+    e->msgno = j;
+    if (e->vnum != -1)
+    {
+      m->v2r[m->vcount] = j;
+      e->vnum = m->vcount++;
+      struct Body *b = e->body;
+      mv->vsize += b->length + b->offset - b->hdr_offset + padding;
+    }
+    j++;
+  }
+  m->msg_count = j;
+}
+
 /**
  * update_tables - Update a MailboxView's internal tables
  * @param mv Mailbox
@@ -296,22 +384,7 @@ static void update_tables(struct MailboxView *mv)
     }
     else
     {
-      if ((m->type == MUTT_NOTMUCH) || (m->type == MUTT_MH) ||
-          (m->type == MUTT_MAILDIR) || (m->type == MUTT_IMAP))
-      {
-        mailbox_size_sub(m, m->emails[i]);
-      }
-      /* remove message from the hash tables */
-      if (m->subj_hash && m->emails[i]->env->real_subj)
-        mutt_hash_delete(m->subj_hash, m->emails[i]->env->real_subj, m->emails[i]);
-      if (m->id_hash && m->emails[i]->env->message_id)
-        mutt_hash_delete(m->id_hash, m->emails[i]->env->message_id, m->emails[i]);
-      mutt_label_hash_remove(m, m->emails[i]);
-
-      if (m->type == MUTT_IMAP)
-        imap_notify_delete_email(m, m->emails[i]);
-
-      mailbox_gc_add(m->emails[i]);
+      remove_email(m, m->emails[i]);
       m->emails[i] = NULL;
     }
   }
@@ -342,6 +415,9 @@ int mview_mailbox_observer(struct NotifyCallback *nc)
     case NT_MAILBOX_UPDATE:
       update_tables(mv);
       break;
+    case NT_MAILBOX_PRUNE:
+      prune_emails(mv);
+      break;
     case NT_MAILBOX_RESORT:
       mutt_sort_headers(mv, true);
       break;
diff --git a/index/shared_data.c b/index/shared_data.c
index 4f5a8f568..77f092560 100644
--- a/index/shared_data.c
+++ b/index/shared_data.c
@@ -107,6 +107,10 @@ static int index_shared_mailbox_observer(struct NotifyCallback *nc)
   if (nc->event_subtype == NT_MAILBOX_DELETE)
     shared->mailbox = NULL;
 
+  // The current Email is about to be removed from the Mailbox
+  if ((nc->event_subtype == NT_MAILBOX_PRUNE) && shared->email && shared->email->quasi_deleted)
+    index_shared_data_set_email(shared, NULL);
+
   mutt_debug(LL_NOTIFY, "relay NT_MAILBOX to shared data observers\n");
   notify_send(shared->notify, nc->event_type, nc->event_subtype, nc->event_data);
   return 0;
diff --git a/maildir/mailbox.c b/maildir/mailbox.c
index 29814b831..393329a94 100644
--- a/maildir/mailbox.c
+++ b/maildir/mailbox.c
@@ -673,10 +673,13 @@ static enum MxStatus maildir_check(struct Mailbox *m)
     {
       /* This message disappeared, so we need to simulate a "reopen"
        * event.  We know it disappeared because we just scanned the
-       * subdirectory it used to reside in.  */
+       * subdirectory it used to reside in.
+       *
+       * Drop it from the index rather than marking it deleted: the file may
+       * be back under a new name (e.g. a flag change by another program) by
+       * the next check, and a deleted flag would make the sync unlink it.  */
       occult = true;
-      e->deleted = true;
-      e->purge = true;
+      e->quasi_deleted = true;
     }
     else
     {
@@ -689,9 +692,12 @@ static enum MxStatus maildir_check(struct Mailbox *m)
   /* destroy the file name hash */
   mutt_hash_free(&hash_names);
 
-  /* If we didn't just get new mail, update the tables. */
+  /* Remove the vanished messages and rebuild the tables. */
   if (occult)
+  {
+    mailbox_changed(m, NT_MAILBOX_PRUNE);
     mailbox_changed(m, NT_MAILBOX_RESORT);
+  }
 
   /* do any delayed parsing we need to do. */
   maildir_delayed_parsing(m, &mda, NULL);
diff --git a/mh/mh.c b/mh/mh.c
index 9cdb0e0cf..16b723d10 100644
--- a/mh/mh.c
+++ b/mh/mh.c
@@ -1018,6 +1018,7 @@ static enum MxStatus mh_check(struct Mailbox *m)
     else /* message has disappeared */
     {
       occult = true;
+      e->quasi_deleted = true;
     }
   }
 
@@ -1025,9 +1026,12 @@ static enum MxStatus mh_check(struct Mailbox *m)
 
   mutt_hash_free(&fnames);
 
-  /* If we didn't just get new mail, update the tables. */
+  /* Remove the vanished messages and rebuild the tables. */
   if (occult)
+  {
+    mailbox_changed(m, NT_MAILBOX_PRUNE);
     mailbox_changed(m, NT_MAILBOX_RESORT);
+  }
 
   /* Incorporate new messages */
   num_new = mh_move_to_mailbox(m, &mha);
-- 
2.47.3

Attachment: signature.asc
Description: PGP signature

Reply via email to