]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-134557: Suppress immortalization in _PyCode_GetScriptXIData under free-threading...
authorneonene <53406459+neonene@users.noreply.github.com>
Mon, 26 May 2025 17:46:49 +0000 (02:46 +0900)
committerGitHub <noreply@github.com>
Mon, 26 May 2025 17:46:49 +0000 (11:46 -0600)
Disable immortalization around Py_CompileString*().

The same approach as 332356b that fixed the refleaks in compile() and eval().

E: 09e72cf can pass test_capi, test_sys and test__interpchannels with this patch for me.

Python/crossinterp.c

index 26eecdddf4bdd0ee5608778e6fb9dcdd47a285a0..13d91c508c41fa7d41cf62eb450e3335d27d37ec 100644 (file)
@@ -908,8 +908,15 @@ get_script_xidata(PyThreadState *tstate, PyObject *obj, int pure,
             }
             goto error;
         }
+#ifdef Py_GIL_DISABLED
+        // Don't immortalize code constants to avoid memory leaks.
+        ((_PyThreadStateImpl *)tstate)->suppress_co_const_immortalization++;
+#endif
         code = Py_CompileStringExFlags(
                     script, filename, Py_file_input, &cf, optimize);
+#ifdef Py_GIL_DISABLED
+        ((_PyThreadStateImpl *)tstate)->suppress_co_const_immortalization--;
+#endif
         Py_XDECREF(ref);
         if (code == NULL) {
             goto error;