From 31ad0d90125ad8422aaf8e346b4371437042c0bb Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Wed, 23 Jul 2025 00:17:26 +0800 Subject: [PATCH 1/5] Track executor before any possible deallocations --- Python/optimizer.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Python/optimizer.c b/Python/optimizer.c index 8d01d605ef4a2a..84f29ac369aa12 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -1222,6 +1222,10 @@ make_executor_from_uops(_PyUOpInstruction *buffer, int length, const _PyBloomFil } sanity_check(executor); #endif + // Note: this MUST be here before any Py_DECREF(executor). + // Otherwise, the GC tries to untrack a still untracked object + // during dealloc. + _PyObject_GC_TRACK(executor); #ifdef _Py_JIT executor->jit_code = NULL; executor->jit_side_entry = NULL; @@ -1234,7 +1238,6 @@ make_executor_from_uops(_PyUOpInstruction *buffer, int length, const _PyBloomFil return NULL; } #endif - _PyObject_GC_TRACK(executor); return executor; } From c16da4ff010186ddced1c7acf7fc3b529e3e179b Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 22 Jul 2025 16:20:11 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2025-07-22-16-20-06.gh-issue-137007.1oPvvK.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-07-22-16-20-06.gh-issue-137007.1oPvvK.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-22-16-20-06.gh-issue-137007.1oPvvK.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-22-16-20-06.gh-issue-137007.1oPvvK.rst new file mode 100644 index 00000000000000..cb25fd10c0bd2c --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-22-16-20-06.gh-issue-137007.1oPvvK.rst @@ -0,0 +1 @@ +Fix a bug during JIT compilation failure which caused garbage collection debug assertions to fail. From 4f9f6ded0f65fa53ee03ab1ba2cef2537923bbe4 Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Wed, 23 Jul 2025 00:55:18 +0800 Subject: [PATCH 3/5] move further up --- Python/optimizer.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Python/optimizer.c b/Python/optimizer.c index 84f29ac369aa12..2e8a17757df76f 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -1205,6 +1205,10 @@ make_executor_from_uops(_PyUOpInstruction *buffer, int length, const _PyBloomFil assert(next_exit == -1); assert(dest == executor->trace); assert(dest->opcode == _START_EXECUTOR); + // Note: we MUST track it here before any Py_DECREF(executor) or + // linking of executor. Otherwise, the GC tries to untrack a + // still untracked object during dealloc. + _PyObject_GC_TRACK(executor); _Py_ExecutorInit(executor, dependencies); #ifdef Py_DEBUG char *python_lltrace = Py_GETENV("PYTHON_LLTRACE"); @@ -1222,10 +1226,6 @@ make_executor_from_uops(_PyUOpInstruction *buffer, int length, const _PyBloomFil } sanity_check(executor); #endif - // Note: this MUST be here before any Py_DECREF(executor). - // Otherwise, the GC tries to untrack a still untracked object - // during dealloc. - _PyObject_GC_TRACK(executor); #ifdef _Py_JIT executor->jit_code = NULL; executor->jit_side_entry = NULL; From 4ae3405d12503cccdf92c7dd6e021cba44d80086 Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Fri, 7 Nov 2025 23:40:10 +0000 Subject: [PATCH 4/5] track before immortalizing --- Python/optimizer.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Python/optimizer.c b/Python/optimizer.c index 6e98bda0624d73..b97dbd5ec266ed 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -1520,6 +1520,7 @@ _PyExecutor_GetColdExecutor(void) Py_FatalError("Cannot allocate core JIT code"); } ((_PyUOpInstruction *)cold->trace)->opcode = _COLD_EXIT; + _PyObject_GC_TRACK(cold); #ifdef _Py_JIT cold->jit_code = NULL; cold->jit_size = 0; From bc2da123aa7ab583c610c2875f06862fd1cddf50 Mon Sep 17 00:00:00 2001 From: Ken Jin Date: Tue, 9 Dec 2025 23:58:58 +0000 Subject: [PATCH 5/5] remove useless track --- Python/optimizer.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Python/optimizer.c b/Python/optimizer.c index b97dbd5ec266ed..6e98bda0624d73 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -1520,7 +1520,6 @@ _PyExecutor_GetColdExecutor(void) Py_FatalError("Cannot allocate core JIT code"); } ((_PyUOpInstruction *)cold->trace)->opcode = _COLD_EXIT; - _PyObject_GC_TRACK(cold); #ifdef _Py_JIT cold->jit_code = NULL; cold->jit_size = 0;