Skip to content

Commit 57d7c3e

Browse files
gh-122247: Move instruction instrumentation sanity check after tracing check (#122251)
1 parent e006c73 commit 57d7c3e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Lib/test/test_monitoring.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,21 @@ def f(a=1, b=2):
18631863
self.assertEqual(call_data[0], (f, 1))
18641864
self.assertEqual(call_data[1], (f, sys.monitoring.MISSING))
18651865

1866+
def test_instruction_explicit_callback(self):
1867+
# gh-122247
1868+
# Calling the instruction event callback explicitly should not
1869+
# crash CPython
1870+
def callback(code, instruction_offset):
1871+
pass
1872+
1873+
sys.monitoring.use_tool_id(0, "test")
1874+
self.addCleanup(sys.monitoring.free_tool_id, 0)
1875+
sys.monitoring.register_callback(0, sys.monitoring.events.INSTRUCTION, callback)
1876+
sys.monitoring.set_events(0, sys.monitoring.events.INSTRUCTION)
1877+
callback(None, 0) # call the *same* handler while it is registered
1878+
sys.monitoring.restart_events()
1879+
sys.monitoring.set_events(0, 0)
1880+
18661881

18671882
class TestOptimizer(MonitoringTestBase, unittest.TestCase):
18681883

Python/instrumentation.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,14 +1344,14 @@ int
13441344
_Py_call_instrumentation_instruction(PyThreadState *tstate, _PyInterpreterFrame* frame, _Py_CODEUNIT *instr)
13451345
{
13461346
PyCodeObject *code = _PyFrame_GetCode(frame);
1347-
assert(debug_check_sanity(tstate->interp, code));
13481347
int offset = (int)(instr - _PyCode_CODE(code));
13491348
_PyCoMonitoringData *instrumentation_data = code->_co_monitoring;
13501349
assert(instrumentation_data->per_instruction_opcodes);
13511350
int next_opcode = instrumentation_data->per_instruction_opcodes[offset];
13521351
if (tstate->tracing) {
13531352
return next_opcode;
13541353
}
1354+
assert(debug_check_sanity(tstate->interp, code));
13551355
PyInterpreterState *interp = tstate->interp;
13561356
uint8_t tools = instrumentation_data->per_instruction_tools != NULL ?
13571357
instrumentation_data->per_instruction_tools[offset] :

0 commit comments

Comments
 (0)