From 749385155ad987e537d3ef581b716b19f87cff93 Mon Sep 17 00:00:00 2001 From: taosang2 Date: Mon, 24 Jun 2024 18:29:52 -0400 Subject: [PATCH] SWDEV-467540 - Get lastCommand safely We must be in protected way to get last command when calling awaitCompletion() where lastCommand will be released and possibly destroyed. This can solve scope lock(notify_lock_) crash in Event::notifyCmdQueue() with AMD_DIRECT_DISPATCH = true. Change-Id: I4297166f912a71112f4a8945d993160ba9afdc34 --- rocclr/platform/commandqueue.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/rocclr/platform/commandqueue.cpp b/rocclr/platform/commandqueue.cpp index c8edb13b5a..b9e3056ff7 100644 --- a/rocclr/platform/commandqueue.cpp +++ b/rocclr/platform/commandqueue.cpp @@ -60,10 +60,19 @@ bool HostQueue::terminate() { if (AMD_DIRECT_DISPATCH) { if (vdev() != nullptr) { // If the queue still has the last command, then wait and release it - if (lastEnqueueCommand_ != nullptr) { - lastEnqueueCommand_->awaitCompletion(); - lastEnqueueCommand_->release(); - lastEnqueueCommand_ = nullptr; + // We must be in protected way to get last command when calling + // awaitCompletion() where lastCommand will be released and possibly + // destroyed. + Command* lastCommand = getLastQueuedCommand(true); + if (lastCommand != nullptr) { + lastCommand->awaitCompletion(); + // Note that if lastCommand isn't a marker, it may not be lastEnqueueCommand_ now + // after lastCommand->awaitCompletion() is called. + if (lastEnqueueCommand_ != nullptr) { + lastEnqueueCommand_ ->release(); // lastEnqueueCommand_ should be a marker + lastEnqueueCommand_ = nullptr; + } + lastCommand->release(); } } thread_.Release();