python / python/cpython

Exceptions raised while tracing certain statements cannot be caught

Open
#148,278 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.13 3.14 3.15 interpreter-core type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

So I found a really odd bug today. except blocks don't catch exceptions raised when tracing certain statements that themselves "cannot raise". For example:

def f():
    breakpoint()
    try:
        pass  # If you raise while tracing this line, the exception isn't caught.
    except:
        ...
    try:
        42  # Ditto.
    except:
        ...
    try:
        return  # Ditto.
    except:
        ...

A couple of observations:

  • Exceptions raised while tracing other "normal" statements are handled the normal way.
  • The code for the exception handling is still present in these examples, even though it's statically unreachable.
  • If two or more of these "non-raising" statements occur together in the same block (like several pass statements in a row), only the last one will have the bug:
def f():
    breakpoint()
    try:
        pass  # No bug
        pass  # No bug
        pass  # Bug
    except:
        ...

I haven't invested any time into figuring out what the root cause is, I've just been poking at it with code examples. It's definitely a bug in the bytecode compiler though, not the interpreter (dis shows missing exception table entries). Probably something to do with how we emit exception tables for NOP or anything that unwinds the block stack, if I had to guess?

(This also affects 3.12, and maybe even earlier versions, but only tagging for 3.13 and newer since I don't think this is a "security" issue.)

Linked PRs
  • gh-148333

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the examples in the issue and inspect their dis output, focusing on missing exception-table entries for pass, 42, and return. Compare these with tracing of normal statements and examine the compiler's handling of NOP and block-stack unwinding. Done means the except blocks catch exceptions raised while tracing these statements, with regression coverage for the cases described.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.