stackless-dev / stackless

The Stackless Python programming language
http://www.stackless.com/
Other
1.03k stars 60 forks source link

Issues with PEP-578 audit hooks sys.setprofile and sys.settrace #300

Closed akruis closed 3 years ago

akruis commented 3 years ago

Every now and then and always before I release a Stackless version I compile Stackless with SLP_WITH_FRAME_REF_DEBUG defined and run the test suite. (The definition is in stackless.h.)

This time an assertion failed:

#13 0x00007efdeb6be102 in __GI___assert_fail (assertion=0x77bef3 "SLP_CURRENT_FRAME_IS_VALID(_PyThreadState_GET())", 
    file=0x822f2d "Objects/abstract.c", line=2609, function=0x823948 "PyObject *PyIter_Next(PyObject *)") at assert.c:101
#14 0x00000000006ed5c0 in PyIter_Next (iter=<list_iterator at remote 0x7efdea4847d0>) at Objects/abstract.c:2609
#15 0x000000000061c9c1 in PySys_Audit (event=0x7ba8af "sys.settrace", argFormat=0x0) at ./Python/sysmodule.c:207
#16 0x0000000000583668 in PyEval_SetTrace (func=0x61d3a0 <trace_trampoline>, arg=<method at remote 0x7efdea400890>) at Python/ceval.c:5274
#17 0x0000000000594490 in SLP_UPDATE_TSTATE_ON_SWITCH (tstate=0xdf1340, prev=0x7efdeb4d8940, next=0x7efdea58f600)
    at Stackless/module/scheduling.c:645
#18 0x0000000000591ea7 in slp_schedule_task_prepared (ts=0xdf1340, result=0x7ffed344fa40, prev=0x7efdeb4d8940, next=0x7efdea58f600, 
    stackless=0, did_switch=0x0) at Stackless/module/scheduling.c:1120

The cause is obvious: a recursive call into the interpreter from within SLP_UPDATE_TSTATE_ON_SWITCH. Within this function the thread state is in an inconsistent state and therefore the interpreter must not be called. In previous versions of Python the C-functions PyEval_SetTrace() and PyEval_SetProfile() were save, but upstream commit b82e17e6 added PySys_Audit() calls to PyEval_SetTrace() and PyEval_SetProfile().

Probably the best solution would be to add appropriate PySys_Audit() calls to tasklet_set_trace_function(), tasklet_set_profile_function() and tasklet_setstate() and do not call the audit hook on tasklet switching.

akruis commented 3 years ago

Fixed.