Fix crash when running ORT under low integrity process like Edge where ETW registration can fail (#22699)

### Description
Make ETW provider registration non-fatal and not throw an exception

Needs to work under build with exceptions enabled & --disable_exceptions

### Motivation and Context
ORT should not crash

Addresses #22475. Private tested by filer of that issue
This commit is contained in:
ivberg 2024-11-01 22:58:47 -07:00 committed by GitHub
parent d419df4076
commit 2e4e221da8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 4 deletions

View file

@ -158,8 +158,9 @@ void EtwRegistrationManager::LazyInitialize() {
initialization_status_ = InitializationStatus::Initializing;
etw_status_ = ::TraceLoggingRegisterEx(etw_provider_handle, ORT_TL_EtwEnableCallback, nullptr);
if (FAILED(etw_status_)) {
// Registration can fail when running under Low Integrity process, and should be non-fatal
initialization_status_ = InitializationStatus::Failed;
ORT_THROW("ETW registration failed. Logging will be broken: " + std::to_string(etw_status_));
LOGS_DEFAULT(WARNING) << "Error in ETW registration: " << std::to_string(etw_status_);
}
initialization_status_ = InitializationStatus::Initialized;
}
@ -176,7 +177,9 @@ void EtwRegistrationManager::InvokeCallbacks(LPCGUID SourceId, ULONG IsEnabled,
std::lock_guard<std::mutex> lock(callbacks_mutex_);
for (const auto& callback : callbacks_) {
(*callback)(SourceId, IsEnabled, Level, MatchAnyKeyword, MatchAllKeyword, FilterData, CallbackContext);
if (callback != nullptr) {
(*callback)(SourceId, IsEnabled, Level, MatchAnyKeyword, MatchAllKeyword, FilterData, CallbackContext);
}
}
}

View file

@ -735,8 +735,12 @@ InferenceSession::~InferenceSession() {
// Unregister the session and ETW callbacks
#ifdef _WIN32
std::lock_guard<std::mutex> lock(active_sessions_mutex_);
WindowsTelemetry::UnregisterInternalCallback(callback_ML_ORT_provider_);
logging::EtwRegistrationManager::Instance().UnregisterInternalCallback(callback_ETWSink_provider_);
if (callback_ML_ORT_provider_ != nullptr) {
WindowsTelemetry::UnregisterInternalCallback(callback_ML_ORT_provider_);
}
if (callback_ETWSink_provider_ != nullptr) {
logging::EtwRegistrationManager::Instance().UnregisterInternalCallback(callback_ETWSink_provider_);
}
#endif
active_sessions_.erase(session_id_);