multiprocessing SharedMemory.__del__ can fail, leak file descriptor
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug report
Bug description:
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:
>>> from multiprocessing.shared_memory import SharedMemory
>>> import os
>>> def shview(size=4096):
... shmem = SharedMemory(create=True, size=size)
... print("fd is:", shmem._fd)
... return shmem.buf.cast('i')
...
>>> mv = shview()
fd is: 5
Exception ignored while calling deallocator <function SharedMemory.__del__ at 0x7f686264d640>:
Traceback (most recent call last):
File "/usr/lib64/python3.14/multiprocessing/shared_memory.py", line 189, in __del__
self.close()
File "/usr/lib64/python3.14/multiprocessing/shared_memory.py", line 232, in close
self._mmap.close()
BufferError: cannot close exported pointers exist
>>> os.fstat(5).st_size # fd still open
4096
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:
>>> class SharedMemoryFix(SharedMemory):
... def __del__(self):
... if self._fd >= 0:
... os.close(self._fd)
... self._fd = -1
...
>>> def shview(size=4096):
... shmem = SharedMemoryFix(create=True, size=size)
... print("fd is:", shmem._fd)
... return shmem.buf.cast('i')
...
>>> mv = shview()
fd is: 7
>>> os.fstat(7)
Traceback (most recent call last):
File "<python-input-8>", line 1, in <module>
os.fstat(7)
~~~~~~~~^^^
OSError: [Errno 9] Bad file descriptor
(I'm thinking about the POSIX side here. IDK if the behaviour on Windows needs to be different, though I guess it can rely on the mmap's dealloc too)
CPython versions tested on:
3.14
Operating systems tested on:
Linux
Linked PRs
- gh-155007
- gh-155070
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia in Lib/multiprocessing/shared_memory.py, in SharedMemory.del e close(), usando la riproduzione Linux del problema per osservare il fallimento dell’esportazione del buffer e il file descriptor rimasto aperto. Controlla le PR collegate gh-155007 e gh-155070 prima di iniziare; il lavoro è completato quando il distruttore non lascia più aperto il descriptor, preservando al contempo il comportamento rilevante di mmap e del buffer.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- operating-systems
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100