From 2f050b3307e291f94422de6c1cda4338d5018b68 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 13 Nov 2026 03:35:27 +0000 Subject: [PATCH] gh-85839: Keep heap allocations alive for exported memoryviews (cherry picked from commit 92c80e4063530e79bcd398225415f4ee870e7dc8) --- Lib/multiprocessing/heap.py | 8 +++++- Lib/test/_test_multiprocessing.py | 25 +++++++++++++++++++ ...6-11-12-19-33-00.gh-issue-85839.gpt6k4.rst | 2 ++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-12-19-33-00.gh-issue-85839.gpt6k4.rst diff --git a/Lib/multiprocessing/heap.py b/Lib/multiprocessing/heap.py index 5c835648395f79..4fbf475a4958a0 100644 --- a/Lib/multiprocessing/heap.py +++ b/Lib/multiprocessing/heap.py @@ -8,6 +8,7 @@ # import bisect +import ctypes from collections import defaultdict import mmap import os @@ -330,4 +331,9 @@ def __init__(self, size): def create_memoryview(self): (arena, start, stop), size = self._state - return memoryview(arena.buffer)[start:start+size] + # A view of arena.buffer alone cannot keep this allocation alive: + # the heap may recycle the block while the view is still in use. + # Keep the wrapper reachable through the buffer exporter. + exporter = (ctypes.c_ubyte * size).from_buffer(arena.buffer, start) + exporter._wrapper = self + return memoryview(exporter).cast('B') diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index 540189147cb4d7..23602de67ee265 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -4671,6 +4671,31 @@ def test_heap(self): self.assertEqual(len(heap._allocated_blocks), 0, heap._allocated_blocks) self.assertEqual(len(heap._len_to_seq), 0) + def test_memoryview_keeps_allocation_alive(self): + heap = multiprocessing.heap.BufferWrapper._heap + wrapper = multiprocessing.heap.BufferWrapper(8) + view = wrapper.create_memoryview() + view[:] = b"abcdefgh" + wrapper_ref = weakref.ref(wrapper) + + # A derived view must keep the allocation alive even after the + # original view and wrapper have both gone away. + subview = view[2:6] + del view, wrapper + gc.collect() + self.assertIsNotNone(wrapper_ref()) + + # A second allocation must not overwrite the live view's data. + other = multiprocessing.heap.BufferWrapper(8) + other_view = other.create_memoryview() + other_view[:] = b"12345678" + self.assertEqual(bytes(subview), b"cdef") + + del subview, other_view, other + gc.collect() + self.assertIsNone(wrapper_ref()) + self.assertEqual(heap._n_frees, heap._n_mallocs) + def test_free_from_gc(self): # Check that freeing of blocks by the garbage collector doesn't deadlock # (issue #12352). diff --git a/Misc/NEWS.d/next/Library/2026-11-12-19-33-00.gh-issue-85839.gpt6k4.rst b/Misc/NEWS.d/next/Library/2026-11-12-19-33-00.gh-issue-85839.gpt6k4.rst new file mode 100644 index 00000000000000..217b59cd10c080 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-12-19-33-00.gh-issue-85839.gpt6k4.rst @@ -0,0 +1,2 @@ +Memoryviews returned by ``multiprocessing.heap.BufferWrapper.create_memoryview()`` +now keep their shared-memory heap allocation alive until the view is released.