Skip to content

Commit 29a103c

Browse files
committed
Partially revert gh-6010: remove py_is_finalizing() workarounds
Now that the root cause (free of string literals in def_property_static, gh-5976) is fixed in the previous commit, the py_is_finalizing() guards introduced in gh-6010 are no longer needed: - tp_dealloc_impl: remove early return during finalization (was leaking all function records instead of properly destroying them) - destruct(): remove guard around arg.value.dec_ref() - common.h: remove py_is_finalizing() helper (no remaining callers) The genuine fix from gh-6010 (PyObject_Free + Py_DECREF ordering in tp_dealloc_impl) is retained. Made-with: Cursor
1 parent 528c0c2 commit 29a103c

2 files changed

Lines changed: 2 additions & 20 deletions

File tree

include/pybind11/detail/common.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -601,15 +601,6 @@ enum class return_value_policy : uint8_t {
601601

602602
PYBIND11_NAMESPACE_BEGIN(detail)
603603

604-
// Py_IsFinalizing() is a public API since 3.13; before that use _Py_IsFinalizing().
605-
inline bool py_is_finalizing() {
606-
#if PY_VERSION_HEX >= 0x030D0000
607-
return Py_IsFinalizing() != 0;
608-
#else
609-
return _Py_IsFinalizing() != 0;
610-
#endif
611-
}
612-
613604
static constexpr int log2(size_t n, int k = 0) { return (n <= 1) ? k : log2(n >> 1, k + 1); }
614605

615606
// Returns the size as a multiple of sizeof(void *), rounded up.

include/pybind11/pybind11.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -936,11 +936,8 @@ class cpp_function : public function {
936936
std::free(const_cast<char *>(arg.descr));
937937
}
938938
}
939-
// During finalization, default arg values may already be freed by GC.
940-
if (!detail::py_is_finalizing()) {
941-
for (auto &arg : rec->args) {
942-
arg.value.dec_ref();
943-
}
939+
for (auto &arg : rec->args) {
940+
arg.value.dec_ref();
944941
}
945942
if (rec->def) {
946943
std::free(const_cast<char *>(rec->def->ml_doc));
@@ -1435,12 +1432,6 @@ PYBIND11_NAMESPACE_BEGIN(function_record_PyTypeObject_methods)
14351432

14361433
// This implementation needs the definition of `class cpp_function`.
14371434
inline void tp_dealloc_impl(PyObject *self) {
1438-
// Skip dealloc during finalization — GC may have already freed objects
1439-
// reachable from the function record (e.g. default arg values), causing
1440-
// use-after-free in destruct().
1441-
if (detail::py_is_finalizing()) {
1442-
return;
1443-
}
14441435
// Save type before PyObject_Free invalidates self.
14451436
auto *type = Py_TYPE(self);
14461437
auto *py_func_rec = reinterpret_cast<function_record_PyObject *>(self);

0 commit comments

Comments
 (0)