Skip to content

Commit de0a160

Browse files
committed
Don't try to close mmap in SharedMemory.__del__
1 parent 96ebb20 commit de0a160

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

Lib/multiprocessing/shared_memory.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def __init__(self, name=None, create=False, size=0, *, track=True):
186186

187187
def __del__(self):
188188
try:
189-
self.close()
189+
self._closefd()
190190
except OSError:
191191
pass
192192

@@ -222,6 +222,11 @@ def size(self):
222222
"Size in bytes."
223223
return self._size
224224

225+
def _closefd(self):
226+
if _USE_POSIX and self._fd >= 0:
227+
os.close(self._fd)
228+
self._fd = -1
229+
225230
def close(self):
226231
"""Closes access to the shared memory from this instance but does
227232
not destroy the shared memory block."""
@@ -231,9 +236,7 @@ def close(self):
231236
if self._mmap is not None:
232237
self._mmap.close()
233238
self._mmap = None
234-
if _USE_POSIX and self._fd >= 0:
235-
os.close(self._fd)
236-
self._fd = -1
239+
self._closefd()
237240

238241
def unlink(self):
239242
"""Requests that the underlying shared memory block be destroyed.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an error in the finalizer of :class:`multiprocessing.shared_memory.SharedMemory`
2+
which could leak a file descriptor.

0 commit comments

Comments
 (0)