Skip to content

gh-85839: Keep multiprocessing heap allocations alive for memoryviews - #162082

Closed
Jacob-Met wants to merge 1 commit into
python:mainfrom
Jacob-Met:fix/gh-85839-bufferwrapper-memoryview-lifetime
Closed

Jacob-Met wants to merge 1 commit into
python:mainfrom
Jacob-Met:fix/gh-85839-bufferwrapper-memoryview-lifetime

Conversation

@Jacob-Met

Copy link
Copy Markdown

Problem

BufferWrapper.create_memoryview() returns a memoryview of the arena mmap without retaining the BufferWrapper that owns the specific allocated block. Once the wrapper is collected, its finalizer can return the block to the heap while a slice of the returned memoryview still points into it. Later allocations can silently overwrite data still exposed to callers.

Fix

Create a ctypes.c_ubyte buffer exporter over the allocated block and keep its owning BufferWrapper reachable from that exporter. Return a cast byte memoryview of the exporter. A derived memoryview continues retaining the exporter/wrapper until the last view is released, preventing premature heap reuse while preserving the byte-view API.

Test

A regression holds a subview after both the original view and wrapper are deleted, checks that a second allocation does not overwrite the subview, then checks the wrapper is reclaimed and the heap's allocations/free counts balance once all views are released. The prior isolated development test run reported 555 multiprocessing checks passing on the exact pushed branch fix/gh-85839-bufferwrapper-memoryview-lifetime (commit 2f050b3). This presenting agent reviewed the code and regression; a fresh full test suite has not yet been run in this session.

AI disclosure

Implemented and tested with autonomous GPT-6 assistance; reviewed by the presenting assistant. No human code review is claimed.

Fixes #85839.

@Jacob-Met
Jacob-Met requested a review from gpshead as a code owner November 16, 2026 05:19
@python-cla-bot

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@ZeroIntensity ZeroIntensity added the AI-guidelines-not-met The contribution does not meet our guidelines for using AI tools and is closed automatically. label Nov 16, 2026
@bedevere-app

bedevere-app Bot commented Nov 16, 2026

Copy link
Copy Markdown

Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors.

We believe this contribution does not meet our guidelines for using AI tools, so we're closing it. If you think we've misunderstood your contribution, please let us know. You're welcome to explain your approach or ask questions about the guidelines.

Please read our guidelines for using AI tools.

@bedevere-app bedevere-app Bot closed this Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-guidelines-not-met The contribution does not meet our guidelines for using AI tools and is closed automatically. awaiting review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Result of multiprocessing.heap.BufferWrapper.create_memoryview can point to freed memory

3 participants