Skip to content

Commit 7bdfb08

Browse files
[SYCL] Prevent deadlock during shutdown by not holding GraphUpdate mutex (#20715)
Deadlock can happen if L0 calls `exit()` while we are in the middle of enqueuing a command. --------- Co-authored-by: Copilot <[email protected]>
1 parent d76350b commit 7bdfb08

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

sycl/source/detail/sycl_mem_obj_t.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,25 @@ void SYCLMemObjT::updateHostMemory(void *const Ptr) {
141141
}
142142

143143
void SYCLMemObjT::updateHostMemory() {
144-
if ((MUploadDataFunctor != nullptr) && MNeedWriteBack)
144+
// Don't try updating host memory when shutting down.
145+
if ((MUploadDataFunctor != nullptr) && MNeedWriteBack &&
146+
GlobalHandler::instance().isOkToDefer())
145147
MUploadDataFunctor();
146148

147149
// If we're attached to a memory record, process the deletion of the memory
148150
// record. We may get detached before we do this.
149151
if (MRecord) {
150-
bool Result = Scheduler::getInstance().removeMemoryObject(this);
152+
// Don't strictly try holding the lock in removeMemoryObject during shutdown
153+
// to prevent deadlocks.
154+
bool Result = Scheduler::getInstance().removeMemoryObject(
155+
this, GlobalHandler::instance().isOkToDefer());
151156
std::ignore = Result; // for no assert build
157+
158+
// removeMemoryObject might fail during shutdown because of not being
159+
// able to hold write lock. This can happen if shutdown happens due to
160+
// exception/termination while holding lock.
152161
assert(
153-
Result &&
162+
(Result || !GlobalHandler::instance().isOkToDefer()) &&
154163
"removeMemoryObject should not return false in mem object destructor");
155164
}
156165
releaseHostMem(MShadowCopy);

0 commit comments

Comments
 (0)