multiprocessing.Pool gets stuck indefinitely when the child process is killed manually

未关闭
#96,062 11 条评论 6 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
python

调研方向

从 reproducer 中展示的 multiprocessing.Pool 行为开始,使用 macOS 或 Ubuntu 上报告的 Python 3.8.2 环境。重新运行示例,同时终止 worker,然后跟踪 worker 的替换以及 terminate() 的处理。完成标准是:worker 被终止后,队列中的任务仍可继续执行,并且 pool 的终止不会无限期等待。

由索引模型根据 Issue 内容生成。

描述

topic-multiprocessing type-bug

Bug report

When I use multiprocessing.Pool and let processes=1 to execute the task, if I manually kill the child process in the background, the task will not be executed, and the new child process seems to be waiting indefinitely and cannot be terminated.
Here is the example I tested:

import logging
import multiprocessing
import platform
import time
from multiprocessing import Pool

multiprocessing.log_to_stderr().setLevel(logging.DEBUG)


def print_some(i):
    print("Current process name is %s" % multiprocessing.current_process())
    print("--------------"+str(i)+"--------------")
    return "return "+str(i)


def callback_func(n):
    print (n)


if __name__ == "__main__":
    print(platform.python_version())
    multiprocessing.set_start_method('fork')
    p = Pool(1)
    i = 0
    print(p._pool[0].pid)
    while i < 6:
        p.apply_async(print_some, (i, ), callback=callback_func)
        time.sleep(3)
        i = i+1
    print("end")
    print(p._pool[0].pid)
    p.terminate()
    print("close")

and the output is(I manually kill the process 30995):

3.8.2
[DEBUG/MainProcess] created semlock with handle 6
[DEBUG/MainProcess] created semlock with handle 7
[DEBUG/MainProcess] created semlock with handle 10
[DEBUG/MainProcess] created semlock with handle 11
[DEBUG/MainProcess] created semlock with handle 14
[DEBUG/MainProcess] created semlock with handle 15
[DEBUG/MainProcess] added worker
[INFO/ForkPoolWorker-1] child process calling self.run()
30995
Current process name is <ForkProcess name='ForkPoolWorker-1' parent=30994 started daemon>
--------------0--------------
return 0
Current process name is <ForkProcess name='ForkPoolWorker-1' parent=30994 started daemon>
--------------1--------------
return 1
Current process name is <ForkProcess name='ForkPoolWorker-1' parent=30994 started daemon>
--------------2--------------
return 2
Current process name is <ForkProcess name='ForkPoolWorker-1' parent=30994 started daemon>
--------------3--------------
return 3
[DEBUG/MainProcess] cleaning up worker 0
[DEBUG/MainProcess] added worker
[INFO/ForkPoolWorker-2] child process calling self.run()
[DEBUG/MainProcess] terminating pool
[DEBUG/MainProcess] finalizing pool
[DEBUG/MainProcess] helping task handler/workers to finish
[DEBUG/MainProcess] removing tasks from inqueue until task handler finished
[DEBUG/MainProcess] worker handler exiting
[DEBUG/MainProcess] task handler got sentinel
[DEBUG/MainProcess] task handler sending sentinel to result handler
[DEBUG/MainProcess] task handler sending sentinel to workers
[DEBUG/MainProcess] task handler exiting
[DEBUG/MainProcess] result handler got sentinel
end
31000

From the output, when I kill the child process, multiprocessing.Pool does start a new process, but the task cannot continue, and terminate() seems to be stuck somewhere, because my main process is not over, been waiting.
During the running process of the service, the process of crashing is unpredictable, so I did such a test: when using multi-process, what effect will the child process crash have on the program. Finally found such a problem.

Your environment

  • CPython versions tested on: Python3.8.2
  • Operating system and architecture:MacOS10.15.7 or ubuntu16.0.4
主要语言
Python
星标
77.2k
派生
36k
平均合并
1 天 9 小时
30 天内合并 PR
558

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。