Skip to content

GH-106897: Add RERAISE event to sys.monitoring. #107291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 27, 2023

Conversation

markshannon
Copy link
Member

@markshannon markshannon commented Jul 26, 2023

Fixes the imbalance between RAISE and EXCEPTION_HANDLED events.

@markshannon
Copy link
Member Author

@gaogaotiantian would you mind reviewing this?

@gaogaotiantian
Copy link
Member

If I understand the monitoring system correctly, for all the exceptions raised, we want to balance it so each RAISE or RERAISE event(except for StopIteration) should match a PY_UNWIND or an EXCEPTION_HANDLED event right?

Consider the following code:

import sys
import asyncio

E = sys.monitoring.events

sys.monitoring.use_tool_id(0, "test")
 
async def async_generator():
    for i in range(1):
        raise ZeroDivisionError
        yield i
 
async def main():
    sys.monitoring.register_callback(0, E.EXCEPTION_HANDLED, lambda *args: print(f"EXCEPTION_HANDLED {args}"))
    sys.monitoring.register_callback(0, E.RAISE, lambda *args: print(f"RAISE {args}"))
    sys.monitoring.register_callback(0, E.RERAISE, lambda *args: print(f"RERAISE {args}"))
    sys.monitoring.register_callback(0, E.PY_UNWIND, lambda *args: print(f"PY_UNWIND {args}"))
    sys.monitoring.set_events(0, E.EXCEPTION_HANDLED | E.RAISE | E.RERAISE | E.PY_UNWIND)
    try:
        async for item in async_generator():
            print(item)
    except Exception:
        pass
    sys.monitoring.set_events(0, 0)
 
asyncio.run(main())

It prints

RAISE (<code object async_generator at 0x7f54aaeb5230, file "/home/gaogaotiantian/programs/cpython/example.py", line 10>, 44, ZeroDivisionError())
EXCEPTION_HANDLED (<code object async_generator at 0x7f54aaeb5230, file "/home/gaogaotiantian/programs/cpython/example.py", line 10>, 50, ZeroDivisionError())
RERAISE (<code object async_generator at 0x7f54aaeb5230, file "/home/gaogaotiantian/programs/cpython/example.py", line 10>, 52, ZeroDivisionError())
PY_UNWIND (<code object async_generator at 0x7f54aaeb5230, file "/home/gaogaotiantian/programs/cpython/example.py", line 10>, 52)
RAISE (<code object main at 0x5600748fdbc0, file "/home/gaogaotiantian/programs/cpython/example.py", line 17>, 610, ZeroDivisionError())
EXCEPTION_HANDLED (<code object main at 0x5600748fdbc0, file "/home/gaogaotiantian/programs/cpython/example.py", line 17>, 656, ZeroDivisionError())
EXCEPTION_HANDLED (<code object main at 0x5600748fdbc0, file "/home/gaogaotiantian/programs/cpython/example.py", line 17>, 660, ZeroDivisionError())

Maybe because we should add monitor_reraise (or maybe monitor_raise?) in END_ASYNC_FOR? The exception from the coroutine seems to be raised again.

@markshannon
Copy link
Member Author

Thanks for spotting that.

@@ -720,7 +720,11 @@ dummy_func(
exc = args[0];
/* fall through */
case 0:
ERROR_IF(do_raise(tstate, exc, cause), exception_unwind);
if (do_raise(tstate, exc, cause)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused about error reporting from do_raise. If there is no exception set, it sets an exception and returns 0. What does that mean?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do_raise returns 0 if it raises, and 1 if it reraises.
Historical reasons probably 🤷

@miss-islington
Copy link
Contributor

Thanks @markshannon for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry, @markshannon, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 766d2518ae8384c6bd7f82727defeb86847ccf64 3.12

markshannon added a commit to faster-cpython/cpython that referenced this pull request Jul 27, 2023
…07291)

* Ensures that exception handling events are balanced. Each [re]raise event has a matching unwind/handled event.
@bedevere-bot
Copy link

GH-107346 is a backport of this pull request to the 3.12 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.12 only security fixes label Jul 27, 2023
@markshannon markshannon deleted the reraise-events branch July 27, 2023 12:43
markshannon added a commit that referenced this pull request Jul 28, 2023
GH-107346)

* Ensures that exception handling events are balanced. Each [re]raise event has a matching unwind/handled event.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants