On Windows, creating a multiprocessing pool leaks handles
Đang mở
Chưa có ai nhận issue này.
OS-windows
stdlib
topic-multiprocessing
type-bug
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách tái hiện main() và func() trên Windows, sau đó theo dõi việc tạo và kết thúc multiprocessing.pool.Pool để xác định những handle nào vẫn mở. Xem lại gh-154365 và bối cảnh rò rỉ handle từ gh-154140. Được xem là hoàn tất khi việc tạo pool lặp lại không còn làm tăng số lượng handle tiến trình được báo cáo.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- operating-systems
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100