Skip to content

ENH: Close memmap file with a public function close() - #17940

Closed
yoonghm wants to merge 1 commit into
numpy:masterfrom
yoonghm:patch-2
Closed

yoonghm wants to merge 1 commit into
numpy:masterfrom
yoonghm:patch-2

Conversation

@yoonghm

@yoonghm yoonghm commented Dec 6, 2020

Copy link
Copy Markdown

Create a public function close() to properly close mmap

See Method to explicitly close memmap #13510, there are two ways to un-mapped a memory-mapped file:
a. del fp or
b. fp._mmap.close()

The first method may leave the file locked for a long period of time. The second method exposes internal variable _mmap.

The solution provides public function to close the memmap object.

Properly close memmap file with a public function `close()`
@BvB93 BvB93 changed the title Close memmap file with a public function close() ENH: Close memmap file with a public function close() Dec 6, 2020
Comment thread numpy/core/memmap.py
memmap

"""
if self._mmap:

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.

Can you explain what the purpose is of this if self._mmap: ... statement?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

self._mmap may be None if it is not mapped or unmapped. Hence I did a check before calling .mmap.close(). Hence calling the function close() from the same fp has no impact, consistent with the standard Python file.close().

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.

- if self._mmap:
+ if self._mmap is not None:

This should be an if not None check in that case,
especially since if mmap(...): ... raises an error if the file is already closed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

You are correct. Thanks. I have tested the code again, it seems that numpy memmap() does not crash anymore. Need further testing.

@eric-wieser

Copy link
Copy Markdown
Member

See Method to explicitly close memmap #13510, there are two ways to un-mapped a memory-mapped file:
a. del fp or
b. fp._mmap.close()

The first method may leave the file locked for a long period of time. The second method exposes internal variable _mmap.

The original issue also says

the latter crashes the python interpreter if there exists another reference to the memmap.

Have you done anything to address this?

@yoonghm

yoonghm commented Dec 6, 2020

Copy link
Copy Markdown
Author

Ya, this is not fixed. Need to let Python do a core dump and analyze the issue. It will take times to review. KIV.

@yoonghm yoonghm closed this Dec 6, 2020
@yoonghm
yoonghm deleted the patch-2 branch December 6, 2020 18:12
@yoonghm
yoonghm restored the patch-2 branch December 6, 2020 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants