8000 co_instrumentation and co_monitoring Unexpectedly Set · Issue #133524 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

co_instrumentation and co_monitoring Unexpectedly Set #133524

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

Open
ericsnowcurrently opened this issue May 6, 2025 · 1 comment
Open

co_instrumentation and co_monitoring Unexpectedly Set #133524

ericsnowcurrently opened this issue May 6, 2025 · 1 comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@ericsnowcurrently
Copy link
Member
ericsnowcurrently commented May 6, 2025

Bug report

Bug description:

I ran into the following problematic case with gh-133221 (which had to be reverted):

import sys
import _testinternalcapi

def foo():
    pass

def trace_temporarily():
    sys._getframe().f_trace_opcodes = True
    sys.settrace(print)
    sys.settrace(None)
    sys._getframe().f_trace_opcodes = False
trace_temporarily()

_testinternalcapi.verify_stateless_code(foo.__code__)
foo()
_testinternalcapi.verify_stateless_code(foo.__code__)  # fails

(See #133221 (comment).)

I would expect co_instrumentation and co_monitoring to have remained NULL.

CC @markshannon

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

@ericsnowcurrently ericsnowcurrently added the type-bug An unexpected behavior, bug, or error label May 6, 2025
@ericsnowcurrently ericsnowcurrently changed the title co_instrumentation Unexpectedly Set co_instrumentation and co_monitoring Unexpectedly Set May 6, 2025
@gaogaotiantian
Copy link
Member

The root cause for this is that flipping f_trace_opcodes changes the global version of instrumentation. Therefore a new function being called allocates the instrumentation data to get up to date.

I think we can do an optimization here by checking if any event is set globally - if not, don't bother allocating the instrumentation memory. If @markshannon thinks this is something we should do, I can do it.

@picnixz picnixz added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label May 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants
0