From f1821a83e858b7ab7a31feeb4be4066b636a1ece Mon Sep 17 00:00:00 2001 From: Agarwal Date: Mon, 31 Aug 2026 19:51:56 -0700 Subject: [PATCH 1/2] [SYCL] Do not release threadpool resources on Windows during shutdown --- sycl/source/detail/global_handler.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index 9e2b30727e23f..16829dbe3a795 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -323,7 +323,16 @@ void shutdown_early(bool CanJoinThreads = true) { // upon its release GlobalHandler::RTGlobalObjHandler->prepareSchedulerToRelease(true); - if (GlobalHandler::RTGlobalObjHandler->MHostTaskThreadPool.Inst) { + // Do not cleanup thread pool on windows during application shutdown. + // Let OS do the cleanup. + bool doThreadPoolCleanup = + GlobalHandler::RTGlobalObjHandler->MHostTaskThreadPool.Inst.get() != + nullptr; +#ifndef _WIN32 + doThreadPoolCleanup &= !CanJoinThreads; +#endif + + if (doThreadPoolCleanup) { GlobalHandler::RTGlobalObjHandler->MHostTaskThreadPool.Inst->finishAndWait( CanJoinThreads); GlobalHandler::RTGlobalObjHandler->MHostTaskThreadPool.Inst.reset(nullptr); @@ -373,8 +382,13 @@ void shutdown_late() { GlobalHandler::RTGlobalObjHandler->MXPTIRegistry.Inst.reset(nullptr); - // Release the rest of global resources. +#ifndef _WIN32 + // Release the rest of global resources. Do not release GlobalHandler + // on Windows and let OS reclaim leaked memory. Releasing GlobalHandler + // on Windows can seg fault if application uses host tasks, as there + // can be a race between host tasks and shutdown. delete GlobalHandler::RTGlobalObjHandler; +#endif GlobalHandler::RTGlobalObjHandler = nullptr; } From bbd95c458dc839139329903876e4e266b2b62000 Mon Sep 17 00:00:00 2001 From: Udit Kumar Agarwal Date: Mon, 31 Aug 2026 20:17:38 -0700 Subject: [PATCH 2/2] Update global_handler.cpp --- sycl/source/detail/global_handler.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index 16829dbe3a795..979ffc020861b 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -326,9 +326,9 @@ void shutdown_early(bool CanJoinThreads = true) { // Do not cleanup thread pool on windows during application shutdown. // Let OS do the cleanup. bool doThreadPoolCleanup = - GlobalHandler::RTGlobalObjHandler->MHostTaskThreadPool.Inst.get() != - nullptr; -#ifndef _WIN32 + GlobalHandler::RTGlobalObjHandler->MHostTaskThreadPool.Inst.get() != + nullptr; +#ifdef _WIN32 doThreadPoolCleanup &= !CanJoinThreads; #endif