Skip to content

Commit

Permalink
Merge pull request #1241 from braingram/bug/close_memmap_with_context
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamJamieson authored and braingram committed Nov 23, 2022
1 parent aa52111 commit 16e9b72
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2.14.1 (unreleased)
-------------------

The ASDF Standard is at v1.6.0

- Fix issue #1239, close memmap with asdf file context [#1241]

2.14.0 (2022-11-22)
-------------------

Expand Down
6 changes: 2 additions & 4 deletions asdf/generic_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,11 +779,9 @@ def read_into_array(self, size):
return np.fromfile(self._fd, dtype=np.uint8, count=size)

def close(self):
self.flush_memmap()
super().close()
if self._close:
if hasattr(self, "_mmap") and not self._mmap.closed:
self._mmap.flush()
self._mmap.close()
self.close_memmap()


class MemoryIO(RandomAccessFile):
Expand Down
31 changes: 31 additions & 0 deletions asdf/tags/core/tests/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,37 @@ def test_masked_array_stay_open_bug(tmpdir):
assert len(p.open_files()) <= len(orig_open)


def test_memmap_stay_open_bug(tmpdir):
"""
Regression test for issue #1239
memmapped arrays only closed at garbage collection when asdf.open given an open file
When asdf.open is called with an already opened file
it did not close any memmaps that it created (as the file
pointer was still valid). These lingered until garbage collection
and caused CI failures in astropy:
https://github.com/astropy/astropy/pull/14035#issuecomment-1325236928
"""
psutil = pytest.importorskip("psutil")

tmppath = os.path.join(str(tmpdir), "arr.asdf")

tree = {"test": np.array([1, 2, 3])}

f = asdf.AsdfFile(tree)
f.write_to(tmppath)

p = psutil.Process()
orig_open = p.open_files()

for i in range(3):
with open(tmppath, mode="rb") as fp:
with asdf.open(fp) as f2:
np.sum(f2.tree["test"])

assert len(p.open_files()) <= len(orig_open)


def test_masked_array_repr(tmpdir):
tmppath = os.path.join(str(tmpdir), "masked.asdf")

Expand Down

0 comments on commit 16e9b72

Please sign in to comment.