SharedMemory constructor raises "cannot mmap an empty file" exception
還沒有人認領這個 Issue。
評估
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 新手友好度
- 35/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 停滯
- 技術堆疊
- python
研究方向
從 Lib/multiprocessing/shared_memory.py 中的 SharedMemory 建構函式開始,檢查 mmap.mmap 周圍的例外處理路徑。在受影響的平台上執行 issue 中所示的重現步驟,然後檢查關聯的 PR gh-133227。完成的標準是:大小為零的共享記憶體物件可以透過標準函式庫路徑進行清理,而不會讓 ValueError 未經處理。
由索引模型根據 Issue 內容生成。
描述
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
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- 平均合併
- 1 天 9 小時
- 30 天內合併 PR
- 558
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/cpython 的其他 Issue
-
docs pending
難度 2/5 1-3 小時 新手友好度 78/100
-
stdlib type-feature
難度 2/5 1-3 小時 新手友好度 78/100
-
stdlib type-feature
難度 2/5 1-3 小時 新手友好度 72/100
-
build type-bug
難度 2/5 1-3 小時 新手友好度 76/100
-
stdlib topic-email type-feature
難度 2/5 1-3 小時 新手友好度 70/100
相似的 Issue
-
area/auth bug comp/agent P3 platform/discord type/security
難度 2/5 1-3 小時 新手友好度 88/100
NousResearch/hermes-agent#117848 ·
-
難度 2/5 1-3 小時 新手友好度 74/100
bancolombia/sentinel#23 ·
-
test md 未關閉CI
難度 2/5 1-3 小時 新手友好度 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
難度 2/5 1-3 小時 新手友好度 74/100
langchain-ai/deepagents#6450 ·
-
bug client
難度 2/5 1-3 小時 新手友好度 88/100