multiprocessing.Process() with method fork() works not the same with os.fork() on unloading of .so files
未关闭
还没有人认领这个 Issue。
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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 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