Skip to content

Commit 27b6d0a

Browse files
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.
1 parent e5789bf commit 27b6d0a

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

dpctl/_host_task_util.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ int async_dec_ref(DPCTLSyclQueueRef QRef,
5555
*(reinterpret_cast<sycl::event *>(ERefs[ev_id])));
5656
}
5757
cgh.host_task([obj_array_size, obj_vec]() {
58-
{
58+
// if the main thread has not finilized the interpreter yet
59+
if (Py_IsInitialized()) {
5960
PyGILState_STATE gstate;
6061
gstate = PyGILState_Ensure();
6162
for (size_t i = 0; i < obj_array_size; ++i) {

0 commit comments

Comments
 (0)