python / python/cpython

multiprocessing.Process cannot be closed if the process is reaped elsewhere

Đang mở
#94,661 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

topic-multiprocessing type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Here's a small reproducer where I start a multiprocessing.Process - then reap it with os.waitpid(

import os
import multiprocessing
import socket
import sys


ctx = multiprocessing.get_context("spawn")

def target(sock):
    with sock:
        sock.send(b"\x00")
        sock.recv(1)


def main():
    a, b = socket.socketpair()
    with a, b:
        with b:
            proc = ctx.Process(target=target, args=(b, ))
            proc.start()
        assert a.recv(1) == b"\x00"
        a.send(b"\x00")
        os.waitpid(proc.pid, 0)
        if sys.version_info >= (3, 7):
            proc.close()
        elif proc.is_alive():
            raise RuntimeError("proc={proc} is still running".format(proc=proc))


if __name__ == "__main__":
    sys.exit(main())

which results in:

 Traceback (most recent call last):
  File "/home/graingert/projects/distributed/demo.py", line 31, in <module>
    sys.exit(main())
  File "/home/graingert/projects/distributed/demo.py", line 25, in main
    proc.close()
  File "/home/graingert/miniconda3/lib/python3.9/multiprocessing/process.py", line 181, in close
    raise ValueError("Cannot close a process while it is still running. "
ValueError: Cannot close a process while it is still running. You should first call join() or terminate().

One issue is the call to self._popen.poll() suppresses the OSError of a reaped process:
https://github.com/python/cpython/blob/b6558d768f19584ad724be23030603280f9e6361/Lib/multiprocessing/popen_fork.py#L26-L31

I think this can be fixed by checking the state of the multiprocessing.Process.sentinel instead of using os.waitpid

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với reproducer trong issue và kiểm tra Lib/multiprocessing/popen_fork.py, đặc biệt là cách xử lý _popen.poll(), cùng với multiprocessing.Process.close(). Điều tra cách một tiến trình được reaped từ bên ngoài được biểu diễn bởi sentinel của nó và so sánh điều đó với việc phát hiện trạng thái dựa trên waitpid. Công việc được xem là hoàn tất khi reproducer có thể reaped tiến trình con từ bên ngoài rồi đóng Process mà không gặp ValueError không chính xác cho rằng tiến trình vẫn đang chạy.

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

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.