Skip to content

Commit 7acc5e0

Browse files
pierreluctgdrebbe-intrepid
authored andcommitted
Fixing GIL issue in message_reflash_callback
Existing code was casing Python memory allocator called without holding the GIL issue.
1 parent 50ccf9c commit 7acc5e0

1 file changed

Lines changed: 7 additions & 16 deletions

File tree

src/methods.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,14 +1146,11 @@ PyObject* meth_get_error_messages(PyObject* self, PyObject* args)
11461146
return set_ics_exception(exception_runtime_error(), "This is a bug!");
11471147
}
11481148

1149-
PyThreadState* _callback_save = NULL;
11501149
PyObject* msg_callback = NULL;
11511150
static void message_callback(const char* message, bool success)
11521151
{
11531152
// We need to relock the GIL here otherwise we crash
1154-
if (_callback_save) {
1155-
PyEval_RestoreThread(_callback_save);
1156-
}
1153+
PyGILState_STATE state = PyGILState_Ensure();
11571154
if (!msg_callback) {
11581155
PySys_WriteStdout("%s\n", message);
11591156
} else if (PyObject_HasAttrString(msg_callback, "message_callback")) {
@@ -1162,9 +1159,7 @@ static void message_callback(const char* message, bool success)
11621159
PyObject_CallFunction(msg_callback, "s,b", message, success);
11631160
}
11641161
// Unlock the GIL here again...
1165-
if (_callback_save) {
1166-
_callback_save = PyEval_SaveThread();
1167-
}
1162+
PyGILState_Release(state);
11681163
}
11691164

11701165
#ifdef _USE_INTERNAL_HEADER_
@@ -1218,12 +1213,12 @@ PyObject* meth_flash_devices(PyObject* self, PyObject* args)
12181213
return set_ics_exception(exception_runtime_error(), dll_get_error(buffer));
12191214
}
12201215
ice::Function<int __stdcall (unsigned long, NeoDevice*, const SReflashChip_t*, unsigned long, unsigned long, unsigned long, unsigned long, void*)> FlashDevice2(lib, "FlashDevice2");
1221-
_callback_save = PyEval_SaveThread();
1216+
Py_BEGIN_ALLOW_THREADS
12221217
if (!FlashDevice2(0x3835C256, &(neo_device->dev), rc, reflash_count, 0, 0, 0, &message_callback)) {
1223-
PyEval_RestoreThread(_callback_save);
1218+
Py_BLOCK_THREADS
12241219
return set_ics_exception(exception_runtime_error(), "FlashDevice2() Failed");
12251220
}
1226-
PyEval_RestoreThread(_callback_save);
1221+
Py_END_ALLOW_THREADS
12271222
Py_RETURN_NONE;
12281223
}
12291224
catch (ice::Exception& ex)
@@ -1238,9 +1233,7 @@ PyObject* msg_reflash_callback = NULL;
12381233
static void message_reflash_callback(const wchar_t* message, unsigned long progress)
12391234
{
12401235
// We need to relock the GIL here otherwise we crash
1241-
if (_callback_save) {
1242-
PyEval_RestoreThread(_callback_save);
1243-
}
1236+
PyGILState_STATE state = PyGILState_Ensure();
12441237
if (!msg_reflash_callback) {
12451238
PySys_WriteStdout("%s -%d\n", message, progress);
12461239
} else if (PyObject_HasAttrString(msg_reflash_callback, "reflash_callback")) {
@@ -1249,9 +1242,7 @@ static void message_reflash_callback(const wchar_t* message, unsigned long progr
12491242
PyObject_CallFunction(msg_reflash_callback, "u,i", message, progress);
12501243
}
12511244
// Unlock the GIL here again...
1252-
if (_callback_save) {
1253-
_callback_save = PyEval_SaveThread();
1254-
}
1245+
PyGILState_Release(state);
12551246
}
12561247

12571248
// void _stdcall icsneoSetReflashCallback( void(*OnReflashUpdate)(const wchar_t*,unsigned long) )

0 commit comments

Comments
 (0)