python / python/cpython

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

Ouverte
#127,213 7 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

stdlib topic-multiprocessing type-bug
Langage dominant
Python
Étoiles
77.2k
Forks
35.9k
Métriques de merge des PR
Métriques de PR en attente

Description

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par main_os_fork.py et main_mp_fork.py, puis examinez l’extension xyz_test.cc et comparez leur sortie de déchargement sous Linux avec Python 3.11. Consultez la PR liée gh-127246 et déterminez si le comportement différent de teardown des .so est attendu ou nécessite un test de régression et une correction.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
cpp, linux, python
Domaine
operating-systems
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.