File tree 2 files changed +23
-7
lines changed
2 files changed +23
-7
lines changed Original file line number Diff line number Diff line change @@ -188,7 +188,12 @@ class ARROW_PYTHON_EXPORT OwnedRef {
188
188
return *this ;
189
189
}
190
190
191
- ~OwnedRef () { reset (); }
191
+ ~OwnedRef () {
192
+ // GH-38626: destructor may be called after the Python interpreter is finalized.
193
+ if (Py_IsInitialized ()) {
194
+ reset ();
195
+ }
196
+ }
192
197
193
198
void reset (PyObject* obj) {
194
199
Py_XDECREF (obj_);
@@ -225,13 +230,11 @@ class ARROW_PYTHON_EXPORT OwnedRefNoGIL : public OwnedRef {
225
230
explicit OwnedRefNoGIL (PyObject* obj) : OwnedRef(obj) {}
226
231
227
232
~OwnedRefNoGIL () {
228
- // This destructor may be called after the Python interpreter is finalized.
229
- // At least avoid spurious attempts to take the GIL when not necessary.
230
- if ( obj () == NULLPTR) {
231
- return ;
233
+ // GH-38626: destructor may be called after the Python interpreter is finalized.
234
+ if ( Py_IsInitialized () && obj () != NULLPTR) {
235
+ PyAcquireGIL lock;
236
+ reset () ;
232
237
}
233
- PyAcquireGIL lock;
234
- reset ();
235
238
}
236
239
};
237
240
Original file line number Diff line number Diff line change @@ -117,6 +117,19 @@ def test_runtime_info():
117
117
subprocess .check_call ([sys .executable , "-c" , code ], env = env )
118
118
119
119
120
+ def test_import_at_shutdown ():
121
+ # GH-38626: importing PyArrow at interpreter shutdown would crash
122
+ code = """if 1:
123
+ import atexit
124
+
125
+ def import_arrow():
126
+ import pyarrow
127
+
128
+ atexit.register(import_arrow)
129
+ """
130
+ subprocess .check_call ([sys .executable , "-c" , code ])
131
+
132
+
120
133
@pytest .mark .skipif (sys .platform == "win32" ,
121
134
reason = "Path to timezone database is not configurable "
122
135
"on non-Windows platforms" )
You can’t perform that action at this time.
0 commit comments