From aa1137fbe27d1162aa31e68a761982637048fd4b Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Fri, 21 Nov 2025 14:04:42 -0500 Subject: [PATCH] gh-136692: Provide extra debug info when monitoring assertion fails We are seeing occasional failures in the free threading buildbots, but they are difficult to reproduce locally. Add some extra information to the failure messages. --- Python/instrumentation.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/Python/instrumentation.c b/Python/instrumentation.c index 72b7433022fdea..593a1ceafb7d78 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -1081,10 +1081,35 @@ instrumentation_cross_checks(PyInterpreterState *interp, PyCodeObject *code) static int debug_check_sanity(PyInterpreterState *interp, PyCodeObject *code) { - int res; + int res = 1; LOCK_CODE(code); - res = is_version_up_to_date(code, interp) && - instrumentation_cross_checks(interp, code); + // gh-136692: These assertions are triggering on buildbots, so provide + // some more information when crashing. + uint32_t glob_version = global_version(interp); + uintptr_t code_version = code->_co_instrumentation_version; + if (glob_version != code_version) { + _Py_FatalErrorFormat( + __func__, + "Instrumentation version mismatch: global=%lu code=%lu", + (unsigned long)glob_version, (unsigned long)code_version); + } + + _Py_LocalMonitors expected = local_union( + interp->monitors, + code->_co_monitoring->local_monitors); + _Py_LocalMonitors active = code->_co_monitoring->active_monitors; + if (!monitors_equals(active, expected)) { + char buf1[64]; + char buf2[64]; + for (int event = 0; event < _PY_MONITORING_LOCAL_EVENTS; event++) { + snprintf(&buf1[event*3], 4, "%02x ", expected.tools[event]); + snprintf(&buf2[event*3], 4, "%02x ", active.tools[event]); + } + _Py_FatalErrorFormat( + __func__, + "Instrumentation monitors mismatch: expected=%s active=%s", + buf1, buf2); + } UNLOCK_CODE(); return res; }