Skip to content

[lldb] Eliminate (_)Py_IsFinalizing (NFC) #152226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2025

Conversation

JDevlieghere
Copy link
Member

Looking at the implementation of pylifecycle.c in cpython, finalizing and initialized are set at the same time. Therefore we can eliminate the call to Py_IsFinalizing and only check Py_IsInitialized, which is part of the stable API.

I converted the check to an assert and confirmed that during my test suite runs, we never got into the if block. Because we check before taking the lock, there is an opportunity for a race, but that exact same race exists with the original code.

Looking at the implementation of pylifecycle.c in cpython, finalizing
and initialized are set at the same time. Therefore we can eliminate the
call to Py_IsFinalizing and only check Py_IsInitialized, which is part
of the stable API.

I converted the check to an assert and confirmed that during my test
suite runs, we never got into the if block. Because we check before
taking the lock, there is an opportunity for a race, but that exact same
race exists with the original code.
@JDevlieghere JDevlieghere requested a review from labath August 6, 2025 00:06
@JDevlieghere
Copy link
Member Author

@llvmbot llvmbot added the lldb label Aug 6, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 6, 2025

@llvm/pr-subscribers-lldb

Author: Jonas Devlieghere (JDevlieghere)

Changes

Looking at the implementation of pylifecycle.c in cpython, finalizing and initialized are set at the same time. Therefore we can eliminate the call to Py_IsFinalizing and only check Py_IsInitialized, which is part of the stable API.

I converted the check to an assert and confirmed that during my test suite runs, we never got into the if block. Because we check before taking the lock, there is an opportunity for a race, but that exact same race exists with the original code.


Full diff: https://github.com/llvm/llvm-project/pull/152226.diff

1 Files Affected:

  • (modified) lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp (+3-16)
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
index 82aa022bbae0b..27ac54322165e 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
@@ -71,24 +71,11 @@ Expected<std::string> python::As<std::string>(Expected<PythonObject> &&obj) {
   return std::string(utf8.get());
 }
 
-static bool python_is_finalizing() {
-#if PY_VERSION_HEX >= 0x030d0000
-  return Py_IsFinalizing();
-#else
-  return _Py_IsFinalizing();
-#endif
-}
-
 void PythonObject::Reset() {
   if (m_py_obj && Py_IsInitialized()) {
-    if (python_is_finalizing()) {
-      // Leak m_py_obj rather than crashing the process.
-      // https://docs.python.org/3/c-api/init.html#c.PyGILState_Ensure
-    } else {
-      PyGILState_STATE state = PyGILState_Ensure();
-      Py_DECREF(m_py_obj);
-      PyGILState_Release(state);
-    }
+    PyGILState_STATE state = PyGILState_Ensure();
+    Py_DECREF(m_py_obj);
+    PyGILState_Release(state);
   }
   m_py_obj = nullptr;
 }

Copy link
Collaborator

@labath labath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I gave this a spin on our internal build, and it seems to work fine.

@JDevlieghere JDevlieghere merged commit 3686e5b into llvm:main Aug 6, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants