Allow ignoring SHM close failures

Useful if we export the mmap beyond the lifetime of the shm object
This commit is contained in:
Kovid Goyal 2022-06-06 20:31:13 +05:30
parent 79f7954048
commit dec62b1929
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -49,9 +49,11 @@ class SharedMemory:
def __init__(
self, name: str = '', size: int = 0, readonly: bool = False,
mode: int = stat.S_IREAD | stat.S_IWRITE,
prefix: str = 'kitty-', unlink_on_exit: bool = False
prefix: str = 'kitty-',
unlink_on_exit: bool = False, ignore_close_failure: bool = False
):
self.unlink_on_exit = unlink_on_exit
self.ignore_close_failure = ignore_close_failure
if size < 0:
raise TypeError("'size' must be a non-negative integer")
if size and name:
@ -157,7 +159,11 @@ class SharedMemory:
"""Closes access to the shared memory from this instance but does
not destroy the shared memory block."""
if self._mmap is not None:
try:
self._mmap.close()
except BufferError:
if not self.ignore_close_failure:
raise
self._mmap = None
if self._fd >= 0:
os.close(self._fd)