Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions Lib/multiprocessing/shared_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def __init__(self, name=None, create=False, size=0, *, track=True):

def __del__(self):
try:
self.close()
self._closefd()
except OSError:
pass

Expand Down Expand Up @@ -222,6 +222,11 @@ def size(self):
"Size in bytes."
return self._size

def _closefd(self):
if _USE_POSIX and self._fd >= 0:
os.close(self._fd)
self._fd = -1

def close(self):
"""Closes access to the shared memory from this instance but does
not destroy the shared memory block."""
Expand All @@ -231,9 +236,7 @@ def close(self):
if self._mmap is not None:
self._mmap.close()
self._mmap = None
if _USE_POSIX and self._fd >= 0:
os.close(self._fd)
self._fd = -1
self._closefd()

def unlink(self):
"""Requests that the underlying shared memory block be destroyed.
Expand Down
36 changes: 36 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4582,6 +4582,42 @@ def test_shared_memory_pickle_unpickle_dead_object(self):
with self.assertRaises(FileNotFoundError):
pickle.loads(pickled_sms)

def test_shared_memory_del_with_live_buffer(self):
# gh-155003: Garbage collecting a SharedMemory must not release a
# buffer that is still in use, and must still close the file
# descriptor.
cases = {
'buf': lambda sms: sms.buf,
'cast': lambda sms: sms.buf.cast('i'),
}
if c_int is not None:
# Holds a buffer on the memoryview object itself (as a NumPy
# array would), so releasing the memoryview fails, not just
# closing the mmap.
cases['ctypes'] = lambda sms: (c_int * 4).from_buffer(sms.buf)
for case, make_view in cases.items():
with self.subTest(case=case):
sms = shared_memory.SharedMemory(create=True, size=16)
name = sms.name
fd = sms._fd
sms.buf[0] = 42
kept = make_view(sms)
with support.catch_unraisable_exception() as cm:
del sms
support.gc_collect()
self.assertIsNone(cm.unraisable)
with memoryview(kept) as mv, mv.cast('B') as view:
self.assertEqual(view[0], 42)
view[1] = 43
if shared_memory._USE_POSIX:
with self.assertRaises(OSError):
os.fstat(fd)
other = shared_memory.SharedMemory(name)
self.assertEqual(bytes(other.buf[:2]), b'\x2a\x2b')
other.unlink()
other.close()
del kept

def test_shared_memory_across_processes(self):
# bpo-40135: don't define shared memory block's name in case of
# the failure when we run multiprocessing tests in parallel.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix :class:`multiprocessing.shared_memory.SharedMemory` leaking its file
descriptor and printing an ignored :exc:`BufferError` when garbage collected
while its :attr:`~multiprocessing.shared_memory.SharedMemory.buf` was still in
use, for example by a NumPy array. :attr:`!buf` now also stays usable after
the :class:`!SharedMemory` object is garbage collected.
Loading