On Windows, creating a multiprocessing pool leaks handles
Open
Nobody has claimed this yet.
OS-windows
stdlib
topic-multiprocessing
type-bug
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Example:
import ctypes.wintypes
import gc
import multiprocessing.pool
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
kernel32.GetCurrentProcess.argtypes = ()
kernel32.GetCurrentProcess.restype = ctypes.wintypes.HANDLE
kernel32.GetProcessHandleCount.argtypes = (ctypes.wintypes.HANDLE, ctypes.wintypes.LPDWORD)
kernel32.GetProcessHandleCount.restype = ctypes.wintypes.BOOL
def handle_count():
# Pseudo-handle that doesn't need to be closed
hproc = kernel32.GetCurrentProcess()
handle_count = ctypes.wintypes.DWORD()
if not kernel32.GetProcessHandleCount(hproc, ctypes.byref(handle_count)):
raise ctypes.WinError(ctypes.get_last_error())
return handle_count.value
def func():
pool = multiprocessing.pool.Pool(1)
pool.terminate()
pool.join()
pool.close()
pool = None
gc.collect()
def main():
print("Initial handle count:", handle_count())
for _ in range(4):
func()
print("Handle count:", handle_count())
if __name__ == "__main__":
main()
Output with Python 3.16 on Windows:
Initial handle count: 116
Handle count: 123
Handle count: 127
Handle count: 131
Handle count: 135
I discovered the issue while working on PR gh-154140 which detects leaks of Windows handles in the Python test suite.
Linked PRs
- gh-154365
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 reproducing main() and func() on Windows, then trace multiprocessing.pool.Pool creation and termination to identify which handles remain open. Review the linked gh-154365 and the handle-leak context from gh-154140. Done means repeated pool creation no longer increases the reported process handle count.
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