SharedMemory constructor raises "cannot mmap an empty file" exception
まだ誰も着手していません。
評価
- 難易度
- 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時間
- マージ済み PR(30日)
- 558
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
-
🐛 Bug 🔔 Pending processing
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
jumpserver/jumpserver#17584 ·