From befac588366a41eb216236d318f743ba3902ccce Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Thu, 13 Aug 2026 09:12:39 +0900 Subject: [PATCH] [3.14] gh-129752: Don't update adaptive counters when TLBC=0 in free-threading. (gh-155497) (cherry picked from commit ee4fe00a8f72ea84a421bb626d7c78335c5c9ea1) Co-authored-by: Donghee Na --- Lib/test/test_thread_local_bytecode.py | 1 - ...-08-11-01-45-24.gh-issue-129752.Zo3OUx.rst | 2 ++ Python/ceval_macros.h | 20 +++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-08-11-01-45-24.gh-issue-129752.Zo3OUx.rst diff --git a/Lib/test/test_thread_local_bytecode.py b/Lib/test/test_thread_local_bytecode.py index d5c56db8d5da582..6ea0e777e026051 100644 --- a/Lib/test/test_thread_local_bytecode.py +++ b/Lib/test/test_thread_local_bytecode.py @@ -108,7 +108,6 @@ def f(a, b, q=None): """) assert_python_ok("-X", "tlbc=1", "-c", code) - @support.skip_if_sanitizer("gh-129752: data race on adaptive counter", thread=True) def test_no_copies_if_tlbc_disabled(self): code = textwrap.dedent(""" import queue diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-11-01-45-24.gh-issue-129752.Zo3OUx.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-11-01-45-24.gh-issue-129752.Zo3OUx.rst new file mode 100644 index 000000000000000..aad13dc0adef34f --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-11-01-45-24.gh-issue-129752.Zo3OUx.rst @@ -0,0 +1,2 @@ +Don't update adaptive counters in the free-threaded build when thread-local +bytecode is disabled (``-X tlbc=0``). Patch by Donghee Na. diff --git a/Python/ceval_macros.h b/Python/ceval_macros.h index 95a8e719d4d0d1c..c75c0b2410c6771 100644 --- a/Python/ceval_macros.h +++ b/Python/ceval_macros.h @@ -285,6 +285,25 @@ GETITEM(PyObject *v, Py_ssize_t i) { #define ADAPTIVE_COUNTER_TRIGGERS(COUNTER) \ backoff_counter_triggers(forge_backoff_counter((COUNTER))) +#ifdef Py_GIL_DISABLED +/* Counters are unreachable when thread-local bytecode is disabled, + * so there is no need to update them. */ +#define ADVANCE_ADAPTIVE_COUNTER(COUNTER) \ + do { \ + _Py_BackoffCounter cnt = (COUNTER); \ + if (!is_unreachable_backoff_counter(cnt)) { \ + (COUNTER) = advance_backoff_counter(cnt); \ + } \ + } while (0); + +#define PAUSE_ADAPTIVE_COUNTER(COUNTER) \ + do { \ + _Py_BackoffCounter cnt = (COUNTER); \ + if (!is_unreachable_backoff_counter(cnt)) { \ + (COUNTER) = pause_backoff_counter(cnt); \ + } \ + } while (0); +#else #define ADVANCE_ADAPTIVE_COUNTER(COUNTER) \ do { \ (COUNTER) = advance_backoff_counter((COUNTER)); \ @@ -294,6 +313,7 @@ GETITEM(PyObject *v, Py_ssize_t i) { do { \ (COUNTER) = pause_backoff_counter((COUNTER)); \ } while (0); +#endif #ifdef ENABLE_SPECIALIZATION_FT /* Multiple threads may execute these concurrently if thread-local bytecode is