multiprocessing.Process cannot be closed if the process is reaped elsewhere
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- 平均合併
- 1 天 9 小時
- 30 天內合併 PR
- 558
描述
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
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 issue 中的 reproducer 開始,檢查 Lib/multiprocessing/popen_fork.py,尤其是 _popen.poll() 的處理,以及 multiprocessing.Process.close()。調查一個被外部 reaped 的程序如何由其 sentinel 表示,並將其與基於 waitpid 的狀態偵測進行比較。當 reproducer 可以從外部 reaped 子程序,然後關閉 Process,而不會錯誤地觸發表示程序仍在執行中的 ValueError 時,即視為完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- operating-systems
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100