Fix refcounting and lifetime management around async python callbacks

* We need to keep a PythonContext (and its globals Dict) around while
  we still have some pending callbacks happening. So now the external
  code creates a PythonContext and then releases it when it's done, but
  the context will hang around until the global redirector object is
  destructed, which is responsible for deleting the context.
* The global redirector is deleted when a refcounting cycle is detected
  and the dict is unreachable, which only happens after the context is
  released.
* Any time a callback is passed to something and converted to a
  std::function we add a reference on the global redirector to keep it
  alive. When the callback has finished executing we remove the ref.
* This way, any pending callbacks that have been called but not finished
  or converted (queued) and not called yet asynchronously will keep the
  context object alive to be able to output, handle exceptions, etc.
* Additionally we need to detect when we're being called asynchronously
  and handle exceptions separately instead of trying to propagate up the
  call chain, because there might not be any more python code up the
  chain (e.g. the render manager calling a python callback).
This commit is contained in:
baldurk
2017-03-28 17:15:00 +01:00
parent c49670cfad
commit 6969b5b677
5 changed files with 238 additions and 74 deletions
+107 -63
View File
@@ -130,6 +130,64 @@ static PyMethodDef OutputRedirector_methods[] = {
PyObject *PythonContext::main_dict = NULL;
void FetchException(QString &typeStr, QString &valueStr, QList<QString> frames)
{
PyObject *exObj = NULL, *valueObj = NULL, *tracebackObj = NULL;
PyErr_Fetch(&exObj, &valueObj, &tracebackObj);
PyErr_NormalizeException(&exObj, &valueObj, &tracebackObj);
if(exObj && PyType_Check(exObj))
{
PyTypeObject *type = (PyTypeObject *)exObj;
typeStr = QString::fromUtf8(type->tp_name);
}
else
{
typeStr = "";
}
if(valueObj)
valueStr = ToQStr(valueObj);
if(tracebackObj)
{
PyObject *tracebackModule = PyImport_ImportModule("traceback");
if(tracebackModule)
{
PyObject *func = PyObject_GetAttrString(tracebackModule, "format_tb");
if(func && PyCallable_Check(func))
{
PyObject *args = Py_BuildValue("(N)", tracebackObj);
PyObject *formattedTB = PyObject_CallObject(func, args);
if(formattedTB)
{
Py_ssize_t size = PyList_Size(formattedTB);
for(Py_ssize_t i = 0; i < size; i++)
{
PyObject *el = PyList_GetItem(formattedTB, i);
frames << ToQStr(el);
}
Py_DecRef(formattedTB);
}
Py_DecRef(args);
}
}
}
Py_DecRef(exObj);
Py_DecRef(valueObj);
Py_DecRef(tracebackObj);
}
void PythonContext::GlobalInit()
{
// must happen on the UI thread
@@ -167,10 +225,11 @@ void PythonContext::GlobalInit()
OutputRedirectorType.tp_name = "renderdoc_output_redirector";
OutputRedirectorType.tp_basicsize = sizeof(OutputRedirector);
OutputRedirectorType.tp_flags = Py_TPFLAGS_DEFAULT;
OutputRedirectorType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE;
OutputRedirectorType.tp_doc =
"Output redirector, to be able to catch output to stdout and stderr";
OutputRedirectorType.tp_new = PyType_GenericNew;
OutputRedirectorType.tp_dealloc = &PythonContext::outstream_del;
OutputRedirectorType.tp_methods = OutputRedirector_methods;
OutputRedirector_methods[0].ml_meth = &PythonContext::outstream_write;
@@ -280,25 +339,27 @@ PythonContext::PythonContext(QObject *parent) : QObject(parent)
QString valueStr = "";
QList<QString> frames;
// set global output that point to this
// set global output that point to this context. It is responsible for deleting the context when
// it goes out of scope
PyObject *redirector = PyObject_CallFunction((PyObject *)&OutputRedirectorType, "");
if(redirector)
{
PyDict_SetItemString(context_namespace, "renderdoc_output_redirector_context_pointer",
redirector);
PyDict_SetItemString(context_namespace, "_renderdoc_internal", redirector);
OutputRedirector *output = (OutputRedirector *)redirector;
output->context = this;
Py_DECREF(redirector);
}
// release the GIL again
PyGILState_Release(gil);
}
PythonContext::~PythonContext()
void PythonContext::Finish()
{
PyGILState_STATE gil = PyGILState_Ensure();
// release our external handle to globals. It'll now only be ref'd from inside
Py_XDECREF(context_namespace);
PyGILState_Release(gil);
@@ -352,8 +413,12 @@ void PythonContext::executeString(const QString &filename, const QString &source
PyEval_SetTrace(&PythonContext::traceEvent, traceContext);
m_State = PyGILState_GetThisThreadState();
ret = PyEval_EvalCode(compiled, context_namespace, context_namespace);
m_State = NULL;
Py_XDECREF(thisobj);
Py_XDECREF(traceContext);
}
@@ -366,62 +431,7 @@ void PythonContext::executeString(const QString &filename, const QString &source
bool caughtException = (ret == NULL);
if(caughtException)
{
PyObject *exObj = NULL, *valueObj = NULL, *tracebackObj = NULL;
PyErr_Fetch(&exObj, &valueObj, &tracebackObj);
PyErr_NormalizeException(&exObj, &valueObj, &tracebackObj);
if(exObj && PyType_Check(exObj))
{
PyTypeObject *type = (PyTypeObject *)exObj;
typeStr = QString::fromUtf8(type->tp_name);
}
else
{
typeStr = tr("Unknown Exception");
}
if(valueObj)
valueStr = ToQStr(valueObj);
if(tracebackObj)
{
PyObject *tracebackModule = PyImport_ImportModule("traceback");
if(tracebackModule)
{
PyObject *func = PyObject_GetAttrString(tracebackModule, "format_tb");
if(func && PyCallable_Check(func))
{
PyObject *args = Py_BuildValue("(N)", tracebackObj);
PyObject *formattedTB = PyObject_CallObject(func, args);
if(formattedTB)
{
Py_ssize_t size = PyList_Size(formattedTB);
for(Py_ssize_t i = 0; i < size; i++)
{
PyObject *el = PyList_GetItem(formattedTB, i);
frames << ToQStr(el);
}
Py_DecRef(formattedTB);
}
Py_DecRef(args);
}
}
}
Py_DecRef(exObj);
Py_DecRef(valueObj);
Py_DecRef(tracebackObj);
}
FetchException(typeStr, valueStr, frames);
Py_XDECREF(ret);
@@ -569,6 +579,18 @@ void PythonContext::setPyGlobal(const char *varName, PyObject *obj)
emit exception("RuntimeError", QString("Failed to set variable '%1'").arg(varName), {});
}
void PythonContext::outstream_del(PyObject *self)
{
OutputRedirector *redirector = (OutputRedirector *)self;
if(redirector)
{
PythonContext *context = redirector->context;
delete context;
}
}
PyObject *PythonContext::outstream_write(PyObject *self, PyObject *args)
{
const char *text = NULL;
@@ -588,8 +610,8 @@ PyObject *PythonContext::outstream_write(PyObject *self, PyObject *args)
PyObject *globals = PyEval_GetGlobals();
if(globals)
{
OutputRedirector *global = (OutputRedirector *)PyDict_GetItemString(
globals, "renderdoc_output_redirector_context_pointer");
OutputRedirector *global =
(OutputRedirector *)PyDict_GetItemString(globals, "_renderdoc_internal");
if(global)
context = global->context;
}
@@ -625,3 +647,25 @@ int PythonContext::traceEvent(PyObject *obj, PyFrameObject *frame, int what, PyO
return 0;
}
extern "C" PyThreadState *GetExecutingThreadState(PyObject *global_handle)
{
OutputRedirector *redirector = (OutputRedirector *)global_handle;
if(redirector->context)
return redirector->context->GetExecutingThreadState();
return NULL;
}
extern "C" void HandleException(PyObject *global_handle)
{
QString typeStr;
QString valueStr = "";
QList<QString> frames;
FetchException(typeStr, valueStr, frames);
OutputRedirector *redirector = (OutputRedirector *)global_handle;
if(redirector->context)
emit redirector->context->exception(typeStr, valueStr, frames);
}