Skip to content

Raising during line tracing events causes assertion failures when opcode tracing is enabled #96864

Closed

Description

@chgnrdv

Example:

import sys

def tracefunc(frame, event, arg):
    frame.f_trace_opcodes = True
    if event == 'line':
        1 / 0
    return tracefunc

def main():
    pass

sys.settrace(tracefunc)
main()

3.12.0a0 (main:e47b96c44f) crashes with the following message:

$ ../cpython/python -Xfaulthandler crash.py 
python: Python/ceval.c:1107: _PyEval_EvalFrameDefault: Assertion `!_PyErr_Occurred(tstate)' failed.
Fatal Python error: Aborted

Current thread 0x00007f06c1a47740 (most recent call first):
  File "/home/.../crash.py", line 13 in <module>
Aborted (core dumped)

Stack trace:

#0  0x00007ffff7d3f34c in __pthread_kill_implementation () from /usr/lib/libc.so.6
#1  0x00007ffff7cf24b8 in raise () from /usr/lib/libc.so.6
#2  0x00007ffff7cdc534 in abort () from /usr/lib/libc.so.6
#3  0x00007ffff7cdc45c in __assert_fail_base.cold () from /usr/lib/libc.so.6
#4  0x00007ffff7ceb116 in __assert_fail () from /usr/lib/libc.so.6
#5  0x00005555557b7497 in _PyEval_EvalFrameDefault (tstate=0x555555b46aa8 <_PyRuntime+180616>, frame=0x7ffff7fbc020, throwflag=0)
    at Python/ceval.c:1107
#6  0x00005555557c9d3b in _PyEval_EvalFrame (tstate=tstate@entry=0x555555b46aa8 <_PyRuntime+180616>, frame=frame@entry=0x7ffff7fbc020, 
    throwflag=throwflag@entry=0) at ./Include/internal/pycore_ceval.h:95
#7  0x00005555557c9e53 in _PyEval_Vector (tstate=tstate@entry=0x555555b46aa8 <_PyRuntime+180616>, func=func@entry=0x7ffff77e5e90, 
    locals=locals@entry=0x7ffff77895b0, args=args@entry=0x0, argcount=argcount@entry=0, kwnames=kwnames@entry=0x0) at Python/ceval.c:5789
#8  0x00005555557c9f69 in PyEval_EvalCode (co=co@entry=0x7ffff79a1fe0, globals=globals@entry=0x7ffff77895b0, locals=locals@entry=0x7ffff77895b0)
    at Python/ceval.c:591
#9  0x00005555558246b3 in run_eval_code_obj (tstate=tstate@entry=0x555555b46aa8 <_PyRuntime+180616>, co=co@entry=0x7ffff79a1fe0, 
    globals=globals@entry=0x7ffff77895b0, locals=locals@entry=0x7ffff77895b0) at Python/pythonrun.c:1713
#10 0x0000555555824778 in run_mod (mod=mod@entry=0x555555c3fb10, filename=filename@entry=0x7ffff77f1310, globals=globals@entry=0x7ffff77895b0, 
    locals=locals@entry=0x7ffff77895b0, flags=flags@entry=0x7fffffffe548, arena=arena@entry=0x7ffff77ec040) at Python/pythonrun.c:1734
#11 0x0000555555824840 in pyrun_file (fp=fp@entry=0x555555b8f530, filename=filename@entry=0x7ffff77f1310, start=start@entry=257, 
    globals=globals@entry=0x7ffff77895b0, locals=locals@entry=0x7ffff77895b0, closeit=closeit@entry=1, flags=0x7fffffffe548)
    at Python/pythonrun.c:1629
#12 0x0000555555827664 in _PyRun_SimpleFileObject (fp=fp@entry=0x555555b8f530, filename=filename@entry=0x7ffff77f1310, closeit=closeit@entry=1, 
    flags=flags@entry=0x7fffffffe548) at Python/pythonrun.c:439
#13 0x0000555555827823 in _PyRun_AnyFileObject (fp=fp@entry=0x555555b8f530, filename=filename@entry=0x7ffff77f1310, closeit=closeit@entry=1, 
    flags=flags@entry=0x7fffffffe548) at Python/pythonrun.c:78
#14 0x0000555555845d79 in pymain_run_file_obj (program_name=program_name@entry=0x7ffff77f1380, filename=filename@entry=0x7ffff77f1310, 
    skip_source_first_line=0) at Modules/main.c:360
#15 0x0000555555845ea1 in pymain_run_file (config=config@entry=0x555555b2b818 <_PyRuntime+69368>) at Modules/main.c:379
#16 0x0000555555846637 in pymain_run_python (exitcode=exitcode@entry=0x7fffffffe6c4) at Modules/main.c:610
#17 0x00005555558468e3 in Py_RunMain () at Modules/main.c:689
#18 0x000055555584695a in pymain_main (args=args@entry=0x7fffffffe720) at Modules/main.c:719
#19 0x0000555555846a20 in Py_BytesMain (argc=<optimized out>, argv=<optimized out>) at Modules/main.c:743
#20 0x000055555564c742 in main (argc=<optimized out>, argv=<optimized out>) at ./Programs/python.c:15

3.10.7 prints this traceback:

$ python -Xfaulthandler crash.py 
Traceback (most recent call last):
  File "/home/.../crash.py", line 10, in main
    pass
  File "/home/.../crash.py", line 6, in tracefunc
    1 / 0
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/.../crash.py", line 13, in <module>
    main()
SystemError: _PyEval_EvalFrameDefault returned a result with an exception set

OS version: Linux 5.16.9-arch1-1 x86_64.

Activity

  1. added
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Sep 16, 2022
  2. self-assigned this
    on Sep 16, 2022
  3. brandtbucher commented on Sep 16, 2022

    @brandtbucher
    Member

    Oof, thanks for the report. I'm looking at it now.

    @pablogsal, marking as a 3.11 release blocker until I can find out if the assertion failures in debug mode are new or not.

  4. brandtbucher commented on Sep 16, 2022

    @brandtbucher
    Member

    Minimal reproducer:

    import sys
    
    def trace(frame, event, arg):
        if event == "line":
            raise Exception('BOOM!')
        frame.f_trace_opcodes = True
        return trace
    
    def main():
        pass
    
    sys.settrace(trace)
    main()

    Verified that 3.10, 3.11, and main all abort when compiled in debug mode. In release mode, 3.10 raises SystemError, while 3.11 and main both behave as expected. So this isn't new in 3.11 (if anything, the behavior in release mode improved), and the debug mode fix can probably wait for 3.11.1. Your call, @pablogsal.

    Either way, I'll start working on fixes for all three.

  5. changed the title [-]Python/ceval.c:1107: _PyEval_EvalFrameDefault: Assertion `!_PyErr_Occurred(tstate)' failed[/-] [+]Raising during `opcode`/`line` tracing events causes assertion failures[/+] on Sep 16, 2022
  6. changed the title [-]Raising during `opcode`/`line` tracing events causes assertion failures[/-] [+]Raising during `line` tracing events causes assertion failures when opcode tracing is enabled[/+] on Sep 16, 2022
  7. changed the title [-]Raising during `line` tracing events causes assertion failures when opcode tracing is enabled[/-] [+]Raising during line tracing events causes assertion failures when opcode tracing is enabled[/+] on Sep 16, 2022
  8. added a commit that references this issue on Sep 19, 2022
  9. added 2 commits that reference this issue on Sep 20, 2022
  10. added 2 commits that reference this issue on Sep 20, 2022
  11. brandtbucher commented on Sep 20, 2022

    @brandtbucher
    Member

    Thanks again for the report!

  12. Repository owner moved this from Todo to Done in Release and Deferred blockers 🚫on Sep 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.10 (EOL)end of life3.11only security fixes3.12only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)release-blockertype-crashA hard crash of the interpreter, possibly with a core dump

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions