From 27b6d0a4336480eb6325a5596aa85f478696abff Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Wed, 9 Feb 2022 06:26:46 -0600 Subject: [PATCH] Execute body of host_task if main interpreter is still initialized This PR applies the solution worked out in a toy POC Host tasks are executed in a separate thread from the main thread which submitted the kernel and incremented reference count on objects we want to keep alive. When kernel submission occurs near the end of the script, the CPython interpreter may have begun finalization process by the time the kernel completes the execution. `PyThread_Ensure` would crash in that case. So execute the host task only if `Py_IsInitialized()`. The only testing of this scenario is to submit the kernel at the end and not call queue synchronization. --- dpctl/_host_task_util.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dpctl/_host_task_util.hpp b/dpctl/_host_task_util.hpp index 5208e11d2c..7871cdebfc 100644 --- a/dpctl/_host_task_util.hpp +++ b/dpctl/_host_task_util.hpp @@ -55,7 +55,8 @@ int async_dec_ref(DPCTLSyclQueueRef QRef, *(reinterpret_cast(ERefs[ev_id]))); } cgh.host_task([obj_array_size, obj_vec]() { - { + // if the main thread has not finilized the interpreter yet + if (Py_IsInitialized()) { PyGILState_STATE gstate; gstate = PyGILState_Ensure(); for (size_t i = 0; i < obj_array_size; ++i) {