Skip to content

Commit

Permalink
Avoid calling PyEval_RestoreThread when python is finalizing
Browse files Browse the repository at this point in the history
  • Loading branch information
Junchao-Mellanox committed May 15, 2024
1 parent 0044540 commit 7725a5b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pyext/swsscommon.i
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
#include "zmqclient.h"
#include "zmqconsumerstatetable.h"
#include "zmqproducerstatetable.h"
#ifdef SWIGPYTHON
#include <Python.h>
#endif
%}

%include <std_string.i>
Expand Down Expand Up @@ -96,7 +99,28 @@
}
~PyThreadStateGuard()
{
#if PY_MAJOR_VERSION >= 3
#if PY_MINOR_VERSION < 13
// https://docs.python.org/3/c-api/init.html mentions that
// Note Calling this function from a thread when the runtime is finalizing will
// terminate the thread, even if the thread was not created by Python. You can use
// _Py_IsFinalizing() or sys.is_finalizing() to check if the interpreter is in process
// of being finalized before calling this function to avoid unwanted termination.
if (!_Py_IsFinalizing())
{
PyEval_RestoreThread(m_save);
}
#else
// cpython removes _Py_IsFinalizing and replace it by Py_IsFinalizing in 3.13 via commit
// https://github.com/python/cpython/commit/3ff5ef2ad3d89c3ccf4e07ac8fdd798267ae6c61
if (!Py_IsFinalizing())
{
PyEval_RestoreThread(m_save);
}
#endif
# else
PyEval_RestoreThread(m_save);
#endif
}
} thread_state_guard;

Expand Down

0 comments on commit 7725a5b

Please sign in to comment.