@@ -337,6 +337,20 @@ ur_result_t ur_context_handle_t_::finalize() {
337337 }
338338 ZePoolCache.clear ();
339339 }
340+ for (auto &ZePool : ZePoolCleanupList) {
341+ if (NumEventsAvailableInEventPool[ZePool] == 0 &&
342+ checkL0LoaderTeardown ()) {
343+ auto ZeResult = ZE_CALL_NOCHECK (zeEventPoolDestroy, (ZePool));
344+ // Gracefully handle the case that L0 was already unloaded.
345+ if (ZeResult && (ZeResult != ZE_RESULT_ERROR_UNINITIALIZED &&
346+ ZeResult != ZE_RESULT_ERROR_UNKNOWN))
347+ return ze2urResult (ZeResult);
348+ if (ZeResult == ZE_RESULT_ERROR_UNKNOWN) {
349+ ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
350+ }
351+ }
352+ }
353+ ZePoolCleanupList.clear ();
340354 }
341355
342356 if (checkL0LoaderTeardown ()) {
@@ -420,13 +434,9 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
420434 if (!ZePoolCache->empty ()) {
421435 if (NumEventsAvailableInEventPool[ZePoolCache->front ()] == 0 ) {
422436 if (DisableEventsCaching) {
423- // Remove full pool from the cache if events caching is disabled.
437+ ZePoolCleanupList. push_back (*ZePoolCache-> begin ());
424438 ZePoolCache->erase (ZePoolCache->begin ());
425439 } else {
426- // If event caching is enabled then we don't destroy events so there is
427- // no need to remove pool from the cache and add it back when it has
428- // available slots. Just keep it in the tail of the cache so that all
429- // pools can be destroyed during context destruction.
430440 ZePoolCache->push_front (nullptr );
431441 }
432442 }
0 commit comments