SharedMemory constructor raises "cannot mmap an empty file" exception #92408
Labels
stdlib
Python modules in the Lib dir
topic-multiprocessing
type-bug
An unexpected behavior, bug, or error
Bug report
I was trying to clean up a shared memory object left behind by an earlier process using the following code:
However, the first line resulted in an exception being raised:
The exception handler around that line unlinks the object in case of an
OSError
, but not in case of thisValueError
raised bymmap.mmap
:cpython/Lib/multiprocessing/shared_memory.py
Lines 109 to 117 in 973a520
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: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:
Your environment
Linked PRs
The text was updated successfully, but these errors were encountered: