This is an automated email from the ASF dual-hosted git repository.
raulcd pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/main by this push:
new d4e0e26ac0 GH-49942: [Python] Protect PyBuffer and NumPyBuffer
destructors against interpreter finalization (#49943)
d4e0e26ac0 is described below
commit d4e0e26ac074cb0c59478ada8f83f2e509a0d3f2
Author: Antoine Pitrou <[email protected]>
AuthorDate: Thu May 7 09:35:05 2026 +0200
GH-49942: [Python] Protect PyBuffer and NumPyBuffer destructors against
interpreter finalization (#49943)
### Rationale for this change
In https://github.com/apache/arrow/pull/38637 we protected the `OwnedRef`
and `OwnedRefNoGIL` destructors against being called when the Python
interpreter is being finalized, but we didn't address other classes with custom
destructors, such as `PyBuffer` and `NumPyBuffer`.
### What changes are included in this PR?
Avoid executing Python C API code if the interpreter is finalized while a
C++ destructor runs.
### Are these changes tested?
Only manually, using the initial reproducer in
https://github.com/apache/arrow/issues/45214.
### Are there any user-facing changes?
No.
* GitHub Issue: #49942
Authored-by: Antoine Pitrou <[email protected]>
Signed-off-by: Raúl Cumplido <[email protected]>
---
python/pyarrow/src/arrow/python/common.cc | 3 ++-
python/pyarrow/src/arrow/python/numpy_convert.cc | 7 +++++--
2 files changed, 7 insertions(+), 3 deletions(-)
diff --git a/python/pyarrow/src/arrow/python/common.cc
b/python/pyarrow/src/arrow/python/common.cc
index a6d963b9b4..2009f09259 100644
--- a/python/pyarrow/src/arrow/python/common.cc
+++ b/python/pyarrow/src/arrow/python/common.cc
@@ -236,7 +236,8 @@ Result<std::shared_ptr<Buffer>>
PyBuffer::FromPyObject(PyObject* obj) {
}
PyBuffer::~PyBuffer() {
- if (data_ != nullptr) {
+ // GH-38626: destructor may be called after the Python interpreter is
finalized.
+ if (Py_IsInitialized() && data_ != nullptr) {
PyAcquireGIL lock;
PyBuffer_Release(&py_buf_);
}
diff --git a/python/pyarrow/src/arrow/python/numpy_convert.cc
b/python/pyarrow/src/arrow/python/numpy_convert.cc
index 4113cc67d2..fbbfccc871 100644
--- a/python/pyarrow/src/arrow/python/numpy_convert.cc
+++ b/python/pyarrow/src/arrow/python/numpy_convert.cc
@@ -53,8 +53,11 @@ NumPyBuffer::NumPyBuffer(PyObject* ao) : Buffer(nullptr, 0) {
}
NumPyBuffer::~NumPyBuffer() {
- PyAcquireGIL lock;
- Py_XDECREF(arr_);
+ // GH-38626: destructor may be called after the Python interpreter is
finalized.
+ if (Py_IsInitialized()) {
+ PyAcquireGIL lock;
+ Py_XDECREF(arr_);
+ }
}
#define TO_ARROW_TYPE_CASE(NPY_NAME, FACTORY) \