From cc865e6d21a508754646bd79341edd1d0784caa3 Mon Sep 17 00:00:00 2001 From: rdb Date: Fri, 10 Dec 2021 13:54:58 +0100 Subject: [PATCH] pipeline: Don't exit process when SystemExit happens on external thread PyErr_Print() causes the process to be forcibly exited when a SystemExit exception occurred, this is a big issue on Android. --- panda/src/pipeline/pythonThread.cxx | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/panda/src/pipeline/pythonThread.cxx b/panda/src/pipeline/pythonThread.cxx index 506f949289..07c86ba413 100644 --- a/panda/src/pipeline/pythonThread.cxx +++ b/panda/src/pipeline/pythonThread.cxx @@ -245,16 +245,22 @@ call_python_func(PyObject *function, PyObject *args) { PyObject *exc, *val, *tb; PyErr_Fetch(&exc, &val, &tb); - thread_cat.error() - << "Exception occurred within " << *current_thread << "\n"; - // Temporarily restore the exception state so we can print a callback - // on-the-spot. - Py_XINCREF(exc); - Py_XINCREF(val); - Py_XINCREF(tb); - PyErr_Restore(exc, val, tb); - PyErr_Print(); + // on-the-spot, except if it's a SystemExit, which would cause PyErr_Print + // to exit the process immediately. + if (exc != PyExc_SystemExit) { + thread_cat.error() + << "Exception occurred within " << *current_thread << "\n"; + + Py_XINCREF(exc); + Py_XINCREF(val); + Py_XINCREF(tb); + PyErr_Restore(exc, val, tb); + PyErr_Print(); + } else { + thread_cat.info() + << "SystemExit occurred within " << *current_thread << "\n"; + } PyGILState_Release(gstate);