[Python-Dev] Encoding of PyFrameObject members

Victor Stinner victor.stinner at gmail.com
Fri Feb 6 11:07:42 CET 2015


Hi,

2015-02-06 0:27 GMT+01:00 Francis Giraldeau <francis.giraldeau at gmail.com>:
> I need to access frame members from within a signal handler for tracing
> purpose.

IMO you have a big technical or design issue here. Accessing Python
internals in a signal handler is not reliable. A signal can occur
anytime, between two instructions.

> However, the function PyUnicode_AsUTF8String() calls PyObject_Malloc(),
> which is not reentrant. If the signal handler nest over PyObject_Malloc(),
> it causes a segfault, and it could also deadlock.

Yes, the list of async signal-safe function is very very short :-)
It's something like: read(), write(), and use the stack (but not too
much stack or you will get a stack overflow).

I spent many weeks to implement the faulthandler module (try to write
a safe and portable implementation). To write the traceback, I only
use write(). But to read the traceback, I inspect Python internals
which is completly unsafe. faulthandler is written to only be called
when something really bad happen (a "crash"), so it's not so important
if it does crash too :-)

See also the tracemalloc module which also inspects the traceback, but
it does *not* use signals (which would be unsafe). It uses hooks on
the memory allocator.

Python has sys.settrace() and sys.setprofile(). Why not using these functions?

Victor


More information about the Python-Dev mailing list