Skip to content

Commit 3ef6d11

Browse files
committed
[L0] fix waiting on non-owned native handle events
L0 adapter makes optimizations based on whether an event has any external references. The adapter keeps track of this based on a reference count. This reference count wasn't being properly set when first creating an event based on a native handle, leading to code incorrectly assuming that the event was internal and had no external references, which ultimately led to segfaults. This patch sets the refcount to 1 when first creating an event, solving the problem. It also adds a missing null-check to make user native handle events even work.
1 parent 5d58871 commit 3ef6d11

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

source/adapters/level_zero/event.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -625,13 +625,13 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventWait(
625625
///< events to wait for completion
626626
) {
627627
for (uint32_t I = 0; I < NumEvents; I++) {
628-
if (EventWaitList[I]->UrQueue->ZeEventsScope == OnDemandHostVisibleProxy) {
628+
auto e = EventWaitList[I];
629+
if (e->UrQueue && e->UrQueue->ZeEventsScope == OnDemandHostVisibleProxy) {
629630
// Make sure to add all host-visible "proxy" event signals if needed.
630631
// This ensures that all signalling commands are submitted below and
631632
// thus proxy events can be waited without a deadlock.
632633
//
633-
ur_event_handle_t_ *Event =
634-
ur_cast<ur_event_handle_t_ *>(EventWaitList[I]);
634+
ur_event_handle_t_ *Event = ur_cast<ur_event_handle_t_ *>(e);
635635
if (!Event->hasExternalRefs())
636636
die("urEventsWait must not be called for an internal event");
637637

@@ -782,6 +782,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventCreateWithNativeHandle(
782782
Context, UR_EXT_COMMAND_TYPE_USER,
783783
Properties->isNativeHandleOwned);
784784

785+
UREvent->RefCountExternal++;
786+
785787
} catch (const std::bad_alloc &) {
786788
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
787789
} catch (...) {

0 commit comments

Comments
 (0)