multiprocessing.shared_memory: failed attach unlinks another process's block
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug report
Bug description
SharedMemory.__init__ calls self.unlink() from its OSError cleanup handler:
try:
if create and size:
os.ftruncate(self._fd, size)
stats = os.fstat(self._fd)
size = stats.st_size
self._mmap = mmap.mmap(self._fd, size)
except OSError:
self.unlink()
raise
This runs unconditionally, including on the attach path (create=False).
unlink() calls shm_unlink(), which permanently destroys the named block.
So if mmap() fails while attaching to a block created by another process
(e.g. ENOMEM under memory pressure), the failing attach destroys another
owner's live shared memory block.
Reproduction (simulating an mmap failure during attach):
from unittest import mock
from multiprocessing import shared_memory
owner = shared_memory.SharedMemory(create=True, size=1024)
try:
with mock.patch("multiprocessing.shared_memory.mmap.mmap",
side_effect=OSError("ENOMEM")):
shared_memory.SharedMemory(owner.name) # attach
except OSError:
pass
shared_memory.SharedMemory(owner.name) # FileNotFoundError: block destroyed
Secondary issue: resource_tracker.register() runs after this try/except, so
on the create=True path the block is not yet registered. unlink() still
calls resource_tracker.unregister(), so the resource_tracker does
cache[rtype].remove(name) on an unknown name → KeyError printed as a
traceback, with the tracker's exit code set to 3.
Expected behavior
A failed attach must not unlink a block it does not own. On error the fd should
just be closed; the block should be unlinked only when it was created in this
call, and without a spurious unregister for a never-registered block.
Your environment
- CPython main (3.16.0a0); also affects earlier versions
- Linux / POSIX shared memory (
_USE_POSIX)
Linked PRs
- gh-153280
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.
Hướng nghiên cứu
Bắt đầu tại multiprocessing.shared_memory.SharedMemory.init và tái hiện lỗi attach bằng mock được nêu trong issue. Kiểm tra hành vi cleanup và resource-tracker cho cả các đường đi attach và create. Được xem là hoàn tất khi một attach thất bại vẫn để block hiện có có thể sử dụng và không tạo ra lỗi tracker không đáng có; issue có PR được liên kết gh-153280.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- operating-systems
- Loại issue
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 25/100