SharedMemory constructor raises "cannot mmap an empty file" exception
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
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- python
- Lĩnh vực
- operating-systems
Hướng nghiên cứu
Bắt đầu trong Lib/multiprocessing/shared_memory.py, tại hàm khởi tạo SharedMemory, và xem xét luồng xử lý ngoại lệ quanh mmap.mmap. Chạy bản tái hiện được nêu trong issue trên nền tảng bị ảnh hưởng, sau đó kiểm tra PR được liên kết gh-133227. Công việc được xem là hoàn tất khi một đối tượng bộ nhớ dùng chung có kích thước bằng không có thể được dọn dẹp thông qua luồng của thư viện chuẩn mà không để ValueError không được xử lý.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug report
I was trying to clean up a shared memory object left behind by an earlier process using the following code:
shm = SharedMemory("some_name")
shm.close()
shm.unlink()
However, the first line resulted in an exception being raised:
File "/home/USER/.pyenv/versions/3.9.10/lib/python3.9/multiprocessing/shared_memory.py", line 114, in __init__
self._mmap = mmap.mmap(self._fd, size)
ValueError: cannot mmap an empty file
The exception handler around that line unlinks the object in case of an OSError, but not in case of this ValueError raised by mmap.mmap:
This makes it effectively impossible to clean up this particular shared memory object through the standard library.
I'm not sure how the shared memory object was corrupted in the first place, but it looks like the exception is triggered because os.fstat states that it has size 0:
>>> f = _posixshmem.shm_open('some_name', os.O_RDWR, mode=0o600)
>>> os.fstat(f)
os.stat_result(st_mode=33152, st_ino=80, st_dev=27, st_nlink=1, st_uid=1000, st_gid=1000, st_size=0, st_atime=1651836036, st_mtime=1651836036, st_ctime=1651836036)
After which the code tries to mmap with size 0 and fails with the exception mentioned earlier.
I was only able to resolve this by calling _posixshmem.shm_unlink('some_name') manually. I think the exception handler should be extended to also unlink the file if it was truncated to 0 like this.
This scenario can be reproduced with the following code:
import _posixshmem
import os
from multiprocessing.shared_memory import SharedMemory
f = _posixshmem.shm_open('test', os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o600)
os.close(f)
try:
mem = SharedMemory("test")
finally:
_posixshmem.shm_unlink("test")
Your environment
- CPython versions tested on: 3.9.10
- Operating system and architecture: Ubuntu 20.04.3 LTS
Linked PRs
- gh-133227
- 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ự
-
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 74/100
bancolombia/sentinel#23 ·
-
test md Đang mởCI
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100