From 26c1c2238880de29c7365ffc82efa45f4c8349e0 Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Sun, 8 Sep 2024 18:00:35 -0400 Subject: [PATCH 1/3] fix double finalization of static closes #1663 --- src/python.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/python.cpp b/src/python.cpp index 18c49715c..483a561dc 100644 --- a/src/python.cpp +++ b/src/python.cpp @@ -1167,7 +1167,7 @@ PyObject* pandas_arrays () { } bool is_pandas_na_like(PyObject* x) { - const static PyObjectPtr np_nan(PyObject_GetAttrString(numpy(), "nan")); + const static PyObject* np_nan = PyObject_GetAttrString(numpy(), "nan"); return is_pandas_na(x) || (x == Py_None) || (x == (PyObject*)np_nan); } @@ -1203,11 +1203,16 @@ bool py_is_callable(PyObjectRef x) { // caches np.nditer function so we don't need to obtain it everytime we want to // cast numpy string arrays into R objects. -PyObject* get_np_nditer () { - const static PyObjectPtr np_nditer(PyObject_GetAttrString(numpy(), "nditer")); - if (np_nditer.is_null()) { - throw PythonException(py_fetch_error()); - } +PyObject* get_np_nditer() { + static PyObject* np_nditer = []() -> PyObject* { + PyObject* np_nditer = PyObject_GetAttrString(numpy(), "nditer"); + if (np_nditer == NULL) { + throw PythonException(py_fetch_error()); + } + return np_nditer; + }(); + + // Return the static np_nditer return np_nditer; } From 3689fab7ce0d3832588814f4b90317732ec5dca5 Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Sun, 8 Sep 2024 18:08:39 -0400 Subject: [PATCH 2/3] allow disabling the Python finalizer --- R/package.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/package.R b/R/package.R index f8b398b06..a1e4436d4 100644 --- a/R/package.R +++ b/R/package.R @@ -224,7 +224,9 @@ initialize_python <- function(required_module = NULL, use_environment = NULL) { ) - reg.finalizer(.globals, function(e) py_finalize(), onexit = TRUE) + # allow disabling the Python finalizer + if (!tolower(Sys.getenv("RETICULATE_DISABLE_PYTHON_FINALIZER")) %in% c("true", "1", "yes")) + reg.finalizer(.globals, function(e) py_finalize(), onexit = TRUE) # set available flag indicating we have py bindings config$available <- TRUE From 8b3723c6c02bb7c2ca14d01ce25c5cffb15774e2 Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Sun, 8 Sep 2024 18:09:55 -0400 Subject: [PATCH 3/3] add NEWS --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 59e0d66d3..e3a53eaf2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # reticulate (development version) +- Fixed segfault encountered when running Python finalizer (#1663, #1664) + # reticulate 1.39.0 - Python background threads can now run in parallel with the R session (#1641).