python / python/cpython

multiprocessing.Process() with method fork() works not the same with os.fork() on unloading of .so files

オープン
#127,213 コメント 7 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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

説明

Bug report

Bug description:

I have a c/cpp file and compile it with pybind to get a so named xyz_test.cpython-311-x86_64-linux-gnu.so so that I can import it(xyz_test) and call a method named xyz_test.call() in Python code .

I use it with multiprocessing.Process() or os.fork(), but the unload process of this so is different:

with multiprocessing.setset_start_method('fork') and multiprocessing.Process(), this so is only unloaded while the parent process is terminated.
with os.fork(), this so is unloaded both while the child process and the parent process are terminated.

The behavior with os.fork() is the same with what I test on C code with C fork(), so is the behavior a bug with multiprocessing.Process() ?

Thank you!

# main_os_fork.py
import xyz_test
import os
import time

def func():
    pid = os.getpid()
    print(f'=== pid = {pid}, this is python func')
    xyz_test.callTLS()


if __name__ == '__main__':
    pid = os.getpid()
    print(f'=== pid = {pid}, this is python main')
    xyz_test.callTLS()
    fork_pid = os.fork()
    if fork_pid == 0:
        func()
    else:
        time.sleep(1)
# main_mp_fork.py
import xyz_test
import multiprocessing
import os
import time

def func():
    pid = os.getpid()
    print(f'=== pid = {pid}, this is python func')
    xyz_test.callTLS()
    time.sleep(10*60)


if __name__ == '__main__':
    pid = os.getpid()
    print(f'=== pid = {pid}, this is python main')
    xyz_test.callTLS()
    multiprocessing.set_start_method('fork')
    child = multiprocessing.Process(target=func)
    
    child.start()
    child.join()
# xyz_test.h
#ifndef SHARED_OBJ_H_
#define SHARED_OBJ_H_


#define PID_LOG(STR) \
auto pid = getpid(); \
printf("=== pid is %d, %s\n", pid, STR);

#define PRINT_ADDR() \
printf("\t now addr is %p\n", this);

void callTLS();

class A {
public:
    A();
    ~A();
    void call();
private:
    int a = 0;
};

#endif

# xyz_test.cc
#include "shared_obj.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <thread>
#include <iostream>

static thread_local A test;

void __attribute__((constructor)) init() {
    PID_LOG("so is loaded.");
}

void __attribute__((destructor)) fini() {
    PID_LOG("so is unloaded.");
}

A::A() {
    PID_LOG("this is A::A().");
    PRINT_ADDR();
}

A::~A() {
    PID_LOG("this is A::~A().");
    PRINT_ADDR();
}

void A::call() {
    PID_LOG("this is A::call().")
    PRINT_ADDR();
}

void callTLS() {
    test.call();
}
CPython versions tested on:

3.11

Operating systems tested on:

Linux

Linked PRs
  • gh-127246

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

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

はじめの一歩

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

調査の方向性

main_os_fork.py と main_mp_fork.py から始め、次に xyz_test.cc 拡張機能を確認し、Linux 上で Python 3.11 を使った場合のアンロード出力を比較します。リンクされている PR gh-127246 を確認し、異なる .so の teardown 動作が想定どおりなのか、それとも回帰テストと修正が必要なのかを判断してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
cpp, linux, python
領域
operating-systems
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

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

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