Skip to content

initialize_globals()在边缘场景下的获取堆栈卡住问题 #34

Description

@yangrudan

问题

python进程调用so库抢占GIL锁时, 获取堆栈发送命令后会卡住

Image

Activity

  1. yangrudan commented on Jan 7, 2026

    @yangrudan
    ContributorAuthor

    过程

    PROBING=1 python main.py, 非主线程看起来依赖gil锁的获取

    Image

    这种情况通过另一个终端probing -t <pid> bt卡住, 请求无法返回

    Image
  2. yangrudan commented on Jan 7, 2026

    @yangrudan
    ContributorAuthor
  3. reiase commented on Jan 8, 2026

    @reiase
    Contributor

    能提供下测试的python脚本吗

  4. yangrudan commented on Jan 8, 2026

    @yangrudan
    ContributorAuthor

    能提供下测试的python脚本吗

    add.cpp

    #include <pybind11/pybind11.h>
    #include <thread> // 包含线程相关的头文件
    #include <chrono> // 
    #include <stdio.h>                
    int add(int a, int b) {
        while(1) {
            std::this_thread::sleep_for(std::chrono::seconds(2)); // 每2秒暂停一次
        }
        //printf("aaaaaa\n");
        //std::this_thread::sleep_for(std::chrono::seconds(2)); // 每秒暂停一次
            // 可以在这里加入一些逻辑,比如打印日志等
            // std::cout << "Stuck in an infinite loop..." << std::endl;
        return a + b;
    }
    
    int my_add(int a, int b){
        return add(a, b);
    }
    
    int my_add_function(int a, int b){
        return my_add(a, b);
    }
    
    PYBIND11_MODULE(add, m) {
        m.def("add_aa", &my_add_function, "A function that adds two numbers");
    }

    main.py

    import add
    import time
    import threading
    
    
    # result = add.add_aa(3, 5)
    # print(f"The result is: {result}")
    def add_numbers_2():
        add_numbers_1()
    
    
    def add_numbers_1():
        add_numbers()
    
    def add_numbers():
        while True:
            time.sleep(10)
            result = add.add_aa(3, 5)
        #print(f"The result is: {result}")
    
    if __name__ == "__main__":
        add_numbers_2()

    setup.py

    from setuptools import setup, Extension
    from pybind11.setup_helpers import Pybind11Extension, build_ext
    
    ext_modules = [
        Pybind11Extension(
            "add",
            ["add.cpp"],
            extra_compile_args=['-g', '-O0'],  # 添加-O0禁用优化,增强调试效果
            define_macros=[('DEBUG', '1')],  # 可选:定义DEBUG宏
        ),
    ]
    
    setup(
        name="add",
        ext_modules=ext_modules,
        cmdclass={"build_ext": build_ext},
    )

    运行命令

    PROBING=1 PROBING_PORT=9922 python main.py
  5. changed the title [-]获取堆栈卡住[/-] [+]initialize_globals()在边缘场景下的获取堆栈卡住问题[/+] on Jan 9, 2026
  6. yangrudan commented on Jan 9, 2026

    @yangrudan
    ContributorAuthor

    _core模块调用initialize_globals()引入了GIL锁的依赖 @reiase

    Image Image Image Image
  7. added a commit that references this issue on Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions