python / python/cpython

Windows single-file mailbox rewrite can remove the mailbox when the fallback rename fails

Ouverte
#156,699 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

OS-windows stdlib topic-email type-bug
Langage dominant
Python
Étoiles
77.2k
Forks
35.9k
Métriques de merge des PR
Métriques de PR en attente

Description

Bug report

Bug description

Summary

On Windows, _singlefileMailbox.flush() cannot rename a temporary rewrite over an existing mailbox with os.rename(). Its FileExistsError fallback deletes the original mailbox and then performs a second rename. If that second filesystem operation fails, the mailbox pathname has already been removed.

The affected mailbox classes are mailbox.mbox, mailbox.MMDF, and mailbox.Babyl. The temporary replacement normally remains, so the contents are not necessarily irretrievable, but the original mailbox pathname is missing. This is a Windows-specific failure path because it depends on Windows os.rename() refusing to replace an existing destination.

Reproduction Code
import errno
import glob
import mailbox
import os
import tempfile
from unittest.mock import patch

with tempfile.TemporaryDirectory() as directory:
    path = os.path.join(directory, "mailbox")
    box = mailbox.mbox(path)
    first = box.add(b"Subject: first\n\nfirst\n")
    box.add(b"Subject: second\n\nsecond\n")
    box.flush()
    box.remove(first)

    # Simulate Windows rejecting the first replacement attempt, followed by
    # an I/O failure while the fallback performs its second rename.
    with patch(
        "mailbox.os.rename",
        side_effect=[
            FileExistsError(errno.EEXIST, "target exists"),
            OSError(errno.EIO, "injected second rename failure"),
        ],
    ):
        try:
            box.flush()
        except OSError as error:
            print("flush:", type(error).__name__, error.errno)

    print("mailbox exists:", os.path.exists(path))
    print(
        "temporary files:",
        [os.path.basename(name) for name in glob.glob(path + ".*")],
    )

Observed on a Windows CPython build:

flush: OSError 5
mailbox exists: False
temporary files: ['mailbox.1788174401.yuu.26684']
Actual Behavior

After the first os.rename() raises FileExistsError, flush() removes the existing mailbox pathname. If the second os.rename() then fails, the exception propagates after the original mailbox has already been removed.

The rewritten temporary file normally remains, but the mailbox is no longer available at its original pathname.

Expected Behavior

If replacement of the rewritten mailbox fails, the original mailbox pathname should remain intact. Replacing an existing mailbox should not require explicitly removing the destination before installing the rewritten file.

CPython versions tested on

CPython main branch

Operating systems tested on

Windows

Linked PRs
  • gh-156700

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez dans le module mailbox, au niveau de _singlefileMailbox.flush(), et examinez le chemin de remplacement pour mailbox.mbox, mailbox.MMDF et mailbox.Babyl. Reproduisez les deux échecs simulés de os.rename mentionnés dans l’issue, puis ajoutez ou mettez à jour un test de régression afin que le nom de chemin de la boîte aux lettres d’origine soit conservé lorsque le remplacement échoue ; examinez la PR liée gh-156700 avant de commencer.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
backend
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Clairement spécifiée
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.