You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
multiprocessing SharedMemory.__del__ can fail, leak file descriptor #155003
When a SharedMemory object goes out of scope, its __del__ method calls self.close() to clean it up. This can break if there is still a buffer exported from its mmap instance. It tries to close the mmap before the fd, so in this case it will leave the file descriptor open:
I think a better option for __del__ would be to close the fd and leave the mmap & buf objects alone, to be cleaned up as normal by reference counting or GC:
Actually, I think we could simplify it further, and not keep the file descriptor around at all. The man page for the mmap() C function says the mapping doesn't depend on the fd:
After the mmap() call has returned, the file descriptor, fd, can be closed immediately without invalidating the mapping.
The Python implementation also duplicates the fd by default and manages the duplicate as part of the mmap object, to allow resizing, though I think SharedMemory's use would work even without this.
Activity
takluyver commentedon Jul 31, 2026
Actually, I think we could simplify it further, and not keep the file descriptor around at all. The man page for the
mmap()C function says the mapping doesn't depend on the fd:The Python implementation also duplicates the fd by default and manages the duplicate as part of the mmap object, to allow resizing, though I think SharedMemory's use would work even without this.