From 443f553782680ed7efff6f779eacaba404e1b3fd Mon Sep 17 00:00:00 2001 From: Scott McKay Date: Fri, 9 Jun 2023 18:40:51 +1000 Subject: [PATCH] Fix native onnxruntime library not loading in Azure App Service (#16286) ### Description SetThreadDescription isn't available in an Azure App Service sandbox. #15219 removed a check that it was available, making it a hard dependency. When it's not available the dll load fails with a 'procedure not found' error. Add back the check. ### Motivation and Context #15375 - although note this has nothing to do with the original issue. This is just for https://github.com/microsoft/onnxruntime/issues/15375#issuecomment-1579464889 --- onnxruntime/core/platform/windows/env.cc | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/onnxruntime/core/platform/windows/env.cc b/onnxruntime/core/platform/windows/env.cc index b9617accd6..022ecc30f3 100644 --- a/onnxruntime/core/platform/windows/env.cc +++ b/onnxruntime/core/platform/windows/env.cc @@ -137,12 +137,20 @@ class WindowsThread : public EnvThread { static unsigned __stdcall ThreadMain(void* param) { std::unique_ptr p(static_cast(param)); - const ORTCHAR_T* name_prefix = - (p->name_prefix == nullptr || wcslen(p->name_prefix) == 0) ? L"onnxruntime" : p->name_prefix; - std::wostringstream oss; - oss << name_prefix << "-" << p->index; - // Ignore the error - (void)SetThreadDescription(GetCurrentThread(), oss.str().c_str()); + // Not all machines have kernel32.dll and/or SetThreadDescription (e.g. Azure App Service sandbox) + // so we need to ensure it's available before calling. + HMODULE kernelModule = GetModuleHandle(TEXT("kernel32.dll")); + if (kernelModule != nullptr) { + auto setThreadDescriptionFn = (SetThreadDescriptionFunc)GetProcAddress(kernelModule, "SetThreadDescription"); + if (setThreadDescriptionFn != nullptr) { + const ORTCHAR_T* name_prefix = (p->name_prefix == nullptr || wcslen(p->name_prefix) == 0) ? L"onnxruntime" + : p->name_prefix; + std::wostringstream oss; + oss << name_prefix << "-" << p->index; + // Ignore any errors + (void)(setThreadDescriptionFn)(GetCurrentThread(), oss.str().c_str()); + } + } unsigned ret = 0; ORT_TRY {