Skip to content

sys.monitoring: local events still sent after free_tool_id #111963

Closed
@nedbat

Description

@nedbat

Bug report

Bug description:

I use set_local_events with a tool id. Later I free the tool id, but then still get those events. Shouldn't freeing the tool id stop all events?

import sys
print(sys.version)

sysmon = sys.monitoring
events = sysmon.events
myid = 1

def simple_function():
    print("Inside simple function")

def sysmon_py_start(code, instruction_offset):
    print(f"sysmon_py_start({code=}, {instruction_offset=})")
    sysmon.set_local_events(myid, code, events.LINE)

def sysmon_line(code, line_number):
    print(f"sysmon_line({code=}, {line_number=})")

print("Registering")
sysmon.use_tool_id(myid, "try669.py")
sysmon.set_events(myid, events.PY_START)

sysmon.register_callback(myid, events.PY_START, sysmon_py_start)
sysmon.register_callback(myid, events.LINE, sysmon_line)

simple_function()

print("Freeing")
sysmon.set_events(myid, 0)
sysmon.free_tool_id(myid)

simple_function()

When I run this, I see:

3.12.0 (main, Oct  2 2023, 13:16:02) [Clang 14.0.3 (clang-1403.0.22.14.1)]
Registering
sysmon_py_start(code=<code object simple_function at 0x10ead1530, file "/private/tmp/try669.py", line 8>, instruction_offset=0)
sysmon_line(code=<code object simple_function at 0x10ead1530, file "/private/tmp/try669.py", line 8>, line_number=9)
Inside simple function
Freeing
sysmon_line(code=<code object simple_function at 0x10ead1530, file "/private/tmp/try669.py", line 8>, line_number=9)
Inside simple function

Shouldn't freeing the tool id stop sysmon_line from being called again?

The same behavior happens with 3.13.0a1.

cc @markshannon

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Labels

3.12only security fixes3.13bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions