File descriptor leak using multiprocessing.Queue
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
"Too many open files" can occur with multiprocessing.Queue, even if the protocol used to work with the queue is correct.
The file descriptor leak can be demonstrated with two small programs:
File "ko.py"
import os
import multiprocessing
pid = os.getpid()
cmd = f'lsof -p {pid} | grep -i pipe | wc -l'
cmdfull = f'lsof -p {pid} | grep -i pipe'
queues = {}
for i in range(100):
queues[i] = multiprocessing.Queue()
os.system(cmd)
queues[i].close()
queues[i].join_thread()
os.system(cmd)
os.system(cmdfull)
File "ok.py"
import os
import multiprocessing
pid = os.getpid()
cmd = f'lsof -p {pid} | grep -i pipe | wc -l'
cmdfull = f'lsof -p {pid} | grep -i pipe'
queues = {}
for i in range(100):
queues[i] = multiprocessing.Queue()
queues[i].put('Hello')
os.system(cmd)
queues[i].close()
queues[i].join_thread()
os.system(cmd)
os.system(cmdfull)
An empty queue that remains empty is not abnormal (e.g., an error queue when no errors occur).
The workaround is to create a class extending multiprocessing.Queue, adding a close method that inserts an extra message into the queue before performing the actual close.
This issue can be observed on Linux and macOS. However, it is not specific to a particular operating system.
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the ko.py and ok.py reproductions on a supported Linux or macOS system and compare their file-descriptor counts. Then inspect the multiprocessing.Queue close and join_thread paths; done means repeatedly closing empty queues no longer leaves the descriptors shown by lsof while preserving the working behavior demonstrated by ok.py.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- operating-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100