Windows single-file mailbox rewrite can remove the mailbox when the fallback rename fails
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Bắt đầu trong mô-đun mailbox tại _singlefileMailbox.flush() và xem xét đường dẫn thay thế cho mailbox.mbox, mailbox.MMDF và mailbox.Babyl. Tái hiện hai lỗi os.rename được mô phỏng từ issue, sau đó thêm hoặc cập nhật một bài kiểm thử hồi quy để pathname của mailbox ban đầu vẫn còn khi việc thay thế thất bại; xem lại PR gh-156700 được liên kết trước khi bắt đầu.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 36k
- Merge trung bình
- 1 ngày 9 giờ
- Pull request đã merge (30 ngày)
- 558
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của python/cpython
-
docs pending
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
stdlib type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
stdlib type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
build type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
stdlib topic-email type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Tất cả issue của python/cpython
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
zostera/django-bootstrap4#894 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
use-agent-os/agent-os#3276 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
NousResearch/hermes-agent#117848 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
zilliztech/memsearch#759 ·