Python: Fix GIL usage in guestfs_int_py_event_callback_wrapper (RHBZ#1773520)

All Py_* functions should be protected by the GIL.
Otherwise internal python data structures can get corrupted.

Move PyGILState_Ensure to the beginning of the block and
PyGILState_Release to the bottom.

Signed-off-by: Sam Eiderman <sameid@google.com>
This commit is contained in:
Sam Eiderman
2019-11-18 14:38:40 +02:00
committed by Richard W.M. Jones
parent 62c36fa297
commit 7757eb8d0c

View File

@@ -112,13 +112,17 @@ guestfs_int_py_event_callback_wrapper (guestfs_h *g,
const char *buf, size_t buf_len,
const uint64_t *array, size_t array_len)
{
PyGILState_STATE py_save = PyGILState_UNLOCKED;
PyGILState_STATE py_save;
PyObject *py_callback = callback;
PyObject *py_array;
PyObject *args;
PyObject *a;
size_t i;
PyObject *py_r;
int threads_initialized = PyEval_ThreadsInitialized ();
if (threads_initialized)
py_save = PyGILState_Ensure ();
py_array = PyList_New (array_len);
for (i = 0; i < array_len; ++i) {
@@ -132,14 +136,8 @@ guestfs_int_py_event_callback_wrapper (guestfs_h *g,
buf, buf_len, py_array);
Py_INCREF (args);
if (PyEval_ThreadsInitialized ())
py_save = PyGILState_Ensure ();
py_r = PyObject_CallObject (py_callback, args);
if (PyEval_ThreadsInitialized ())
PyGILState_Release (py_save);
Py_DECREF (args);
if (py_r != NULL)
@@ -147,6 +145,9 @@ guestfs_int_py_event_callback_wrapper (guestfs_h *g,
else
/* Callback threw an exception: print it. */
PyErr_PrintEx (0);
if (threads_initialized)
PyGILState_Release (py_save);
}
PyObject *