Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Segfault in 3.13 when callingPyEval_SetTrace from a thread with no Python frames #121814

Closed
Labels
type-bugAn unexpected behavior, bug, or error
@godlygeek

Description

@godlygeek

Bug report

Bug description:

Given asetup.py with:

fromsetuptoolsimportExtensionfromsetuptoolsimportsetupsetup(name="testext",version="0.0",ext_modules=[Extension("testext",language="c++",sources=["testext.cpp"]),    ],zip_safe=False,)

and atestext.cpp with:

#definePY_SSIZE_T_CLEAN#include<Python.h>#include<assert.h>#include<pthread.h>#include<unistd.h>inttracefunc(PyObject *, PyFrameObject *,int, PyObject *){return0;}void*thread_body(void*){    PyGILState_STATE gilstate =PyGILState_Ensure();PyEval_SetTrace(&tracefunc, Py_None);PyGILState_Release(gilstate);returnNULL;}PyObject*trace_in_thread(PyObject*, PyObject*){pthread_t thread;int ret =pthread_create(&thread,NULL, &thread_body,NULL);assert(0 == ret);    Py_BEGIN_ALLOW_THREADS    ret =pthread_join(thread,NULL);assert(0 == ret);    Py_END_ALLOW_THREADS    Py_RETURN_NONE;}static PyMethodDef methods[] = {        {"trace_in_thread", trace_in_thread, METH_NOARGS,"Call PyEval_SetTrace in a thread"},        {NULL,NULL,0,NULL},};staticstructPyModuleDef moduledef = {PyModuleDef_HEAD_INIT,"testext","", -1, methods};PyMODINIT_FUNCPyInit_testext(void){returnPyModule_Create(&moduledef);}

doing:

python3.13 -m pip install.python3.13 -c'import testext; testext.trace_in_thread()'

gives a segmentation fault, because of this code in_PyEval_SetTrace:

PyFrameObject*frame=PyEval_GetFrame();
if (frame->f_trace_opcodes) {

This reproducer enters_PyEval_SetTrace with no Python frames on the stack, and soPyEval_GetFrame returns a null pointer andframe->f_trace_opcodes dereferences it. It seems that this needs to be guarded:

PyFrameObject*frame=PyEval_GetFrame();if (frame&&frame->f_trace_opcodes) {

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp