diff options
author | Sam Gross <colesbury@gmail.com> | 2024-10-24 18:09:59 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-24 18:09:59 -0400 |
commit | 332356b880576a1a00b5dc34f03d7d3995dd4512 (patch) | |
tree | 1f29a5ca46bb083c99e9d623f2da14a0f523014c /Python/bltinmodule.c | |
parent | 1306f33c84b2745aa8af5e3e8f680aa80b836c0e (diff) | |
download | cpython-332356b880576a1a00b5dc34f03d7d3995dd4512.tar.gz cpython-332356b880576a1a00b5dc34f03d7d3995dd4512.zip |
gh-125900: Clean-up logic around immortalization in free-threading (#125901)
* Remove `@suppress_immortalization` decorator
* Make suppression flag per-thread instead of per-interpreter
* Suppress immortalization in `eval()` to avoid refleaks in three tests
(test_datetime.test_roundtrip, test_logging.test_config8_ok, and
test_random.test_after_fork).
* frozenset() is constant, but not a singleton. When run multiple times,
the test could fail due to constant interning.
Diffstat (limited to 'Python/bltinmodule.c')
-rw-r--r-- | Python/bltinmodule.c | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index f87f942cc76..12f065d4b4f 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -867,18 +867,17 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename, goto error; #ifdef Py_GIL_DISABLED - // gh-118527: Disable immortalization of code constants for explicit + // Disable immortalization of code constants for explicit // compile() calls to get consistent frozen outputs between the default // and free-threaded builds. - // Subtract two to suppress immortalization (so that 1 -> -1) - PyInterpreterState *interp = _PyInterpreterState_GET(); - _Py_atomic_add_int(&interp->gc.immortalize, -2); + _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); + tstate->suppress_co_const_immortalization++; #endif result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize); #ifdef Py_GIL_DISABLED - _Py_atomic_add_int(&interp->gc.immortalize, 2); + tstate->suppress_co_const_immortalization--; #endif Py_XDECREF(source_copy); @@ -1024,7 +1023,16 @@ builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals, str++; (void)PyEval_MergeCompilerFlags(&cf); +#ifdef Py_GIL_DISABLED + // Don't immortalize code constants for explicit eval() calls + // to avoid memory leaks. + _PyThreadStateImpl *tstate = (_PyThreadStateImpl *)_PyThreadState_GET(); + tstate->suppress_co_const_immortalization++; +#endif result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf); +#ifdef Py_GIL_DISABLED + tstate->suppress_co_const_immortalization--; +#endif Py_XDECREF(source_copy); } |