python / python/cpython

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

オープン
#156,699 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

OS-windows stdlib topic-email type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

mailbox モジュールの _singlefileMailbox.flush() から始め、mailbox.mbox、mailbox.MMDF、mailbox.Babyl の置換パスを確認します。Issue にある os.rename のモックされた 2 つの失敗を再現し、置換に失敗した場合に元の mailbox パス名が残るよう、回帰テストを追加または更新します。開始前に、リンクされている PR gh-156700 を確認してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。