Skip to content

BUG: Make mmap handling safer in frombuffer#21324

Merged
charris merged 1 commit intonumpy:mainfrom
seberg:frombuffer-fix
Apr 21, 2022
Merged

BUG: Make mmap handling safer in frombuffer#21324
charris merged 1 commit intonumpy:mainfrom
seberg:frombuffer-fix

Conversation

@seberg
Copy link
Copy Markdown
Member

@seberg seberg commented Apr 11, 2022

frombuffer always used the old buffer protocol, but this is not
safe in some cases when combined with the "new" one.
The main/only use-case that was ever found for this is that enabling
the new buffer protocol (by going through a memoryview) protects
users from accidentally closing memorymaps while they are still used.

Closes gh-9537

@seberg seberg added 00 - Bug 09 - Backport-Candidate PRs tagged should be backported labels Apr 11, 2022
def test_empty(self):
assert_array_equal(np.frombuffer(b''), np.array([]))

def test_mmap_close(self):
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattip the test just needs to be skipped on PyPy, or is there something else going on?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyPy does not track the number of exported mmap references, and so the same segfault that occurs in issue gh-9537 will still happen in PyPy. I opened an issue on PyPy but it is complicated to properly support this. https://foss.heptapod.net/pypy/pypy/-/issues/3724

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, makes sense since it adds another level of reference counting effectively.

@seberg seberg added this to the 1.22.4 release milestone Apr 11, 2022
`frombuffer` always used the old buffer protocol, but this is not
safe in some cases when combined with the "new" one.
The main/only use-case that was ever found for this is that enabling
the new buffer protocol (by going through a memoryview) protects
users from accidentally closing memorymaps while they are still used.

Closes numpygh-9537
@charris charris merged commit 79a5000 into numpy:main Apr 21, 2022
@charris
Copy link
Copy Markdown
Member

charris commented Apr 21, 2022

Thanks Sebastian.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

segfault when "viewing" closed mmap object

3 participants