-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow memmapped arrays to keep the backing mmap file open #1668
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import numpy as np | ||
|
||
import asdf | ||
|
||
|
||
def test_memmap_view_access_after_close(tmp_path): | ||
""" | ||
Accessing a view of a memmap after the asdf file | ||
is closed results in a segfault | ||
|
||
https://github.com/asdf-format/asdf/issues/1334 | ||
""" | ||
|
||
a = np.ones(10, dtype="uint8") | ||
fn = tmp_path / "test.asdf" | ||
asdf.AsdfFile({"a": a}).write_to(fn) | ||
|
||
with asdf.open(fn, copy_arrays=False) as af: | ||
v = af["a"][:5] | ||
|
||
assert np.all(v == 1) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -259,8 +259,17 @@ def _make_array(self): | |
# memory mapping. | ||
if self._array is not None: | ||
base = util.get_array_base(self._array) | ||
if isinstance(base, np.memmap) and isinstance(base.base, mmap.mmap) and base.base.closed: | ||
self._array = None | ||
if isinstance(base, np.memmap) and isinstance(base.base, mmap.mmap): | ||
# check if the underlying mmap matches the one generated by generic_io | ||
try: | ||
fd = self._data_callback(_attr="_fd")() | ||
except AttributeError: | ||
# external blocks do not have a '_fd' and don't need to be updated | ||
fd = None | ||
if fd is not None: | ||
if getattr(fd, "_mmap", None) is not base.base: | ||
self._array = None | ||
del fd | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a question for my own educational purposes, what does explicitly deleting the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. The _fd attr is a weakef so I think I added this mostly out of habit. The explicit del of fd isn't needed here as fd isn't used or stored (so the reference will be released at the end of the function). |
||
|
||
if self._array is None: | ||
if isinstance(self._source, str): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we got the better end of that trade.