Article View: linux.debian.bugs.dist
Article #3611Bug#1153301: neomutt: Maildir messages removed or renamed externally are marked deleted and unlinked on sync (data loss)
From: Benj. Mako Hill
Date: Sat, 07 Nov 2026 01:10
Date: Sat, 07 Nov 2026 01:10
479 lines
19832 bytes
19832 bytes
--6hniwmzkjmcsblvx
Content-Type: multipart/mixed; protected-headers=v1;
boundary="vv7wzplnsppfmrun"
Content-Disposition: inline
Subject: neomutt: Maildir messages removed or renamed externally are marked
deleted and unlinked on sync (data loss)
MIME-Version: 1.0
--vv7wzplnsppfmrun
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
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=E2=80=94offlineimap, mbsync, notmuch, a second NeoMutt=E2=80=94look=
s 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
=66rom 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, 'sta=
ble'), (500, 'unstable'), (100, 'unstable'), (100, 'testing'), (1, 'experim=
ental')
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_UNSIGNE=
D_MODULE
Locale: LANG=3Den_US.UTF-8, LC_CTYPE=3Den_US.UTF-8 (charmap=3DUTF-8) (ignor=
ed: 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
--=20
Benjamin Mako Hill
https://mako.cc/
--vv7wzplnsppfmrun
Content-Type: application/x-sh
Content-Disposition: attachment; filename="repro.sh"
Content-Transfer-Encoding: quoted-printable
#!/bin/bash=0A# Reproduce: a Maildir message whose file briefly vanishes ke=
eps its D flag=0A# after the file reappears renamed, and the next sync unli=
nks the real file.=0A# Usage: repro.sh [NEOMUTT_BINARY] (default: neomutt=
)=0A# Needs tmux. Uses only synthetic mail in a temp dir.=0Aset -u=0ANM=3D$=
{1:-neomutt}=0AW=3D$(mktemp -d)=0AM=3D$W/inbox=0Amkdir -p $M/cur $M/new $M/=
tmp=0Acat > $W/muttrc <<'EOF'=0Aset mbox_type=3DMaildir=0Aset delete=3Dyes=
=0Aset move=3Dno=0Aset mark_old=3Dno=0Aset maildir_check_cur=3Dyes=0Aset co=
nfirm_append=3Dno=0Aset quit=3Dyes=0Aset wait_key=3Dno=0AEOF=0Afor n in 1 2=
3; do=0A printf 'From: t%s@example.org\nTo: me@example.org\nSubject: msg%=
s\nMessage-ID: <r%s@example.org>\nDate: Fri, 6 Nov 2026 10:0%s:00 -0800\n\n=
body\n' $n $n $n $n \=0A > "$M/new/1000000$n.R1.host,U=3D$n"=0Adone=0AS=
=3Dnmrepro$$=0Atmux new-session -d -s $S -x 120 -y 30 "$NM -n -F $W/muttrc =
-f $M"=0Asleep 2=0Acnt() { ls $M/cur $M/new | grep -c 'host,U=3D'; }=0Aecho=
"files at start: $(cnt)"=0A# 1. file for msg2 vanishes; let NeoMutt check =
(keypress after a pause)=0AF=3D$(ls $M/new | grep 'U=3D2$')=0Amv "$M/new/$F=
" $W/held=0Atmux send-keys -t $S j; sleep 3; tmux send-keys -t $S k; sleep =
2=0Aecho "after vanish: $(tmux capture-pane -p -t $S | grep -E ' msg2$' | =
cut -c1-40)"=0A# 2. file reappears renamed into cur/ with the S flag (what =
a flag sync does)=0Amv $W/held "$M/cur/$F:2,S"=0Atmux send-keys -t $S j; sl=
eep 3; tmux send-keys -t $S k; sleep 2=0Aecho "after return: $(tmux captur=
e-pane -p -t $S | grep -E ' msg2$' | cut -c1-40)"=0Aecho "files before sync=
: $(cnt)"=0A# 3. sync=0Atmux send-keys -t $S '$'; sleep 2=0Aecho "files aft=
er sync: $(cnt) (3 =3D correct, 2 =3D BUG: msg2 unlinked)"=0Als $M/cur $=
M/new=0Atmux kill-session -t $S=0Aecho "workdir: $W"=0A
--vv7wzplnsppfmrun
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment;
filename="maildir-drop-vanished-messages.patch"
Content-Transfer-Encoding: quoted-printable
=46rom 14f97975513b2306bd7e81426fb4a52963db9285 Mon Sep 17 00:00:00 2001
=46rom: Benjamin Mako Hill <mako@atdot.cc>
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
};
=20
/**
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);
}
=20
+/**
+ * remove_email - Detach an Email from a Mailbox's hash tables and queue i=
t for freeing
+ * @param m Mailbox
+ * @param e Email
+ */
+static void remove_email(struct Mailbox *m, struct Email *e)
+{
+ if ((m->type =3D=3D MUTT_NOTMUCH) || (m->type =3D=3D MUTT_MH) ||
+ (m->type =3D=3D MUTT_MAILDIR) || (m->type =3D=3D 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 =3D=3D 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 change=
s alone.
+ */
+static void prune_emails(struct MailboxView *mv)
+{
+ if (!mv || !mv->mailbox)
+ return;
+
+ struct Mailbox *m =3D mv->mailbox;
+ const int padding =3D mx_msg_padding_size(m);
+
+ m->vcount =3D 0;
+ mv->vsize =3D 0;
+ int j =3D 0;
+ for (int i =3D 0; i < m->msg_count; i++)
+ {
+ struct Email *e =3D 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 =3D -1;
+ e->visible =3D false;
+ remove_email(m, e);
+ m->emails[i] =3D NULL;
+ continue;
+ }
+
+ if (i !=3D j)
+ {
+ m->emails[j] =3D e;
+ m->emails[i] =3D NULL;
+ }
+ e->msgno =3D j;
+ if (e->vnum !=3D -1)
+ {
+ m->v2r[m->vcount] =3D j;
+ e->vnum =3D m->vcount++;
+ struct Body *b =3D e->body;
+ mv->vsize +=3D b->length + b->offset - b->hdr_offset + padding;
+ }
+ j++;
+ }
+ m->msg_count =3D 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 =3D=3D MUTT_NOTMUCH) || (m->type =3D=3D MUTT_MH) ||
- (m->type =3D=3D MUTT_MAILDIR) || (m->type =3D=3D 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->em=
ails[i]);
- if (m->id_hash && m->emails[i]->env->message_id)
- mutt_hash_delete(m->id_hash, m->emails[i]->env->message_id, m->ema=
ils[i]);
- mutt_label_hash_remove(m, m->emails[i]);
-
- if (m->type =3D=3D 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] =3D 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 Notify=
Callback *nc)
if (nc->event_subtype =3D=3D NT_MAILBOX_DELETE)
shared->mailbox =3D NULL;
=20
+ // The current Email is about to be removed from the Mailbox
+ if ((nc->event_subtype =3D=3D NT_MAILBOX_PRUNE) && shared->email && shar=
ed->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 m=
ay
+ * 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 =3D true;
- e->deleted =3D true;
- e->purge =3D true;
+ e->quasi_deleted =3D true;
}
else
{
@@ -689,9 +692,12 @@ static enum MxStatus maildir_check(struct Mailbox *m)
/* destroy the file name hash */
mutt_hash_free(&hash_names);
=20
- /* 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);
+ }
=20
/* 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 =3D true;
+ e->quasi_deleted =3D true;
}
}
=20
@@ -1025,9 +1026,12 @@ static enum MxStatus mh_check(struct Mailbox *m)
=20
mutt_hash_free(&fnames);
=20
- /* 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);
+ }
=20
/* Incorporate new messages */
num_new =3D mh_move_to_mailbox(m, &mha);
--=20
2.47.3
--vv7wzplnsppfmrun--
--6hniwmzkjmcsblvx
Content-Type: application/pgp-signature; name="signature.asc"
-----BEGIN PGP SIGNATURE-----
iQIzBAABCAAdFiEEfGsMgrD+o7iQi1uRu+m/omm+WTUFAmrudsIACgkQu+m/omm+
WTU9yg//Z6geNe0HDM52qqha2cINrVhqdwNgFzM1lmCr0NlyRdsk7Sp27Lp7QGIE
EiPrCofJP7JAt0FZUe7Vz/ocrbkt610Ircm+r0RSYxM0KE4v0dtuVH9N7pIKHfir
xuMw7TnetVyMZKrLaQwriUpxDkTqEgaWmBoZMDfqxSmnSZxjU5fUBsrMyzoUQinU
UUYdTd3JJmbaIQk6QZfXXwyuRyycrfjF2iG5bWnxRWBxGfiChS240Z9FSKp+6XpP
VtNYtoQEbHRvtlC8kv1gtFN1En4Hh4YhZJDYnWe/qVXltLn+MchdE7LduThR+5/Y
6FWqiD2/dbEPjbRUPVVOpR/1imZ+hu6sKgooFHzPfp70/LK8Lcr05UaWdSnPQh6U
MHshyMOhJIruoc8lupC15bvWfC/C7mP3nwoR0T5cBzHKI0jXmI9IPehmajeYUJgC
mI44NuZaEz2HVF4eEqFY2GDPh3TzjYEYr8QmAFgYUL616wVgSVEXzRTOta88So0d
MH6Y42L410JweigpJhKV8vgTgB/tGiIAQvze1xj22ojBFMbWcU1OxXWefOJAUrBs
uVHnIiBnwH8oO5rmtfudphzsXp4AmpyxfyVWztVPg34aXYr+SkfiVpbXAfnxWOqJ
Zr7cdIZwFp/UrMSJqSpV8FYnp/M+20eSElAQzT0CZd11lciD4Xo=
=w7JB
-----END PGP SIGNATURE-----
--6hniwmzkjmcsblvx--
Technical Details
Message-ID:
<NWD6p-aiBU-1@gated-at.bofh.it>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.corradoroberto.it!bofh.it!news.nic.it!robomodShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.corradoroberto.it!bofh.it!news.nic.it!robomod From: "Benj. Mako Hill" <mako@atdot.cc> Newsgroups: linux.debian.bugs.dist Subject: Bug#1153301: neomutt: Maildir messages removed or renamed externally are marked deleted and unlinked on sync (data loss) Date: Sat, 07 Nov 2026 02:10:01 +0100 Message-ID: <NWD6p-aiBU-1@gated-at.bofh.it> X-Original-To: Debian Bug Tracking System <submit@bugs.debian.org> X-Mailbox-Line: From debian-bugs-dist-request@lists.debian.org Sat Nov 7 01:05:12 2026 Old-Return-Path: <debbugs@buxtehude.debian.org> X-Spam-Flag: NO X-Spam-Score: -1.45 Reply-To: "Benj. Mako Hill" <mako@atdot.cc>, 1153301@bugs.debian.org Resent-To: debian-bugs-dist@lists.debian.org Resent-Cc: mutt@packages.debian.org X-Debian-Pr-Message: report 1153301 X-Debian-Pr-Package: neomutt X-Debian-Pr-Keywords: patch upstream X-Debian-Pr-Source: neomutt MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="6hniwmzkjmcsblvx" Content-Disposition: inline X-Greylist: delayed 502 seconds by postgrey-1.37 at buxtehude; Sat, 07 Nov 2026 01:03:01 UTC X-Debian-Message: from BTS X-Mailing-List: <debian-bugs-dist@lists.debian.org> archive/latest/2001191 List-ID: <debian-bugs-dist.lists.debian.org> List-URL: <https://lists.debian.org/debian-bugs-dist/> Approved: robomod@news.nic.it Lines: 479 Organization: linux.* mail to news gateway Sender: robomod@news.nic.it X-Original-Date: Fri, 6 Nov 2026 16:54:31 -0800 X-Original-Message-ID: <zpnlehll6tedtoj545zadkqwouygjh7uaqxw2vuebtc4pow56t@3kaqhlpuga6x>