https://github.com/python/cpython/commit/a5e78cbb9d3efb7b601eca82336d1e97e993dd48 commit: a5e78cbb9d3efb7b601eca82336d1e97e993dd48 branch: 3.14 author: Gregory P. Smith <[email protected]> committer: gpshead <[email protected]> date: 2026-11-11T07:35:08Z summary:
[3.14] gh-155003: Don't try to close mmap in SharedMemory.__del__ (GH-155070) (#161798) * Don't try to close mmap in SharedMemory.__del__ * Add a test for SharedMemory finalization with a live buffer, expand NEWS --------- (cherry picked from commit 4b79d90e935398442d65c59243f8015f062da0f8) Co-authored-by: Thomas Kluyver <[email protected]> files: A Misc/NEWS.d/next/Library/2026-08-02-08-43-20.gh-issue-155003.VVy9Z_.rst M Lib/multiprocessing/shared_memory.py M Lib/test/_test_multiprocessing.py diff --git a/Lib/multiprocessing/shared_memory.py b/Lib/multiprocessing/shared_memory.py index b5396451d0e64a5..de74a0cbbba8533 100644 --- a/Lib/multiprocessing/shared_memory.py +++ b/Lib/multiprocessing/shared_memory.py @@ -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 @@ -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.""" @@ -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. diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index 926219b6a28c6c1..74d562dca9d8663 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -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. diff --git a/Misc/NEWS.d/next/Library/2026-08-02-08-43-20.gh-issue-155003.VVy9Z_.rst b/Misc/NEWS.d/next/Library/2026-08-02-08-43-20.gh-issue-155003.VVy9Z_.rst new file mode 100644 index 000000000000000..c41ce1879488b78 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-02-08-43-20.gh-issue-155003.VVy9Z_.rst @@ -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. _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
