python / python/cpython

`multiprocessing.Process.is_alive()` can incorrectly return True after `join()`

オープン
#130,895 コメント 1 件 リアクション 6 件 担当者 1 名 GitHub で見る

@gpshead がすでに取り組んでいます。

2026年1月11日 から。

stdlib topic-multiprocessing type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:

This came up in https://github.com/python/cpython/pull/130849#discussion_r1980137089

The problem is that popen_fork.Popen (and popen_spawn.Popen and popen_forkserver.Popen) are not thread-safe:

https://github.com/python/cpython/blob/02de9cb9a8fa5b0ae3947231b8c0677834aaee45/Lib/multiprocessing/popen_fork.py#L25-L35

The first successful call to os.waitpid() may reap the pid so that subsequent calls raise an OSError. I've only seen this on macOS (not Linux). We may not yet however have set self.returncode -- that happens a few statements later, so poll() can return None if:

  1. The process has finished
  2. Another thread called poll(), but hasn't yet set self.returncode

And then is_alive() can return True:

https://github.com/python/cpython/blob/02de9cb9a8fa5b0ae3947231b8c0677834aaee45/Lib/multiprocessing/process.py#L153-L170

Note that some classes like concurrent.futures.ProcessPoolExecutor use threads internally, so the user may not even know that threads are involved.

Repro:

repro.py
import os
import multiprocessing as mp
import threading
import time
import sys

original_excepthook = threading.excepthook

def on_except(args):
    original_excepthook(args)
    os._exit(1)

threading.excepthook = on_except

def p1():
    pass

def thread1(p):
    while p.is_alive():
        time.sleep(0.00001)
        pass

def test():
    for i in range(1000):
        print(i)
        p = mp.Process(target=p1)
        p.start()

        t = threading.Thread(target=thread1, args=(p,))
        t.start()

        p.join()
        assert not p.is_alive()

        t.join()

def main():
    threads = [threading.Thread(target=test) for _ in range(10)]
    for t in threads:
        t.start()
    for t in threads:
        t.join()

if __name__ == "__main__":
    main()

NOTE:

  • This is unrelated to free threading
  • popen_fork.Popen (and subclasses) are distinct from subprocess.Popen
CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-131440

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。