Skip to content

Commit cf565e9

Browse files
Revert "Fix ETW Sink Initialize unproperly locking" (microsoft#21360)
Reverts microsoft#21226 Causes any onnxruntime app to hang on Windows ARM64. Our pipelines do not have the same ETW environment, so we couldn't catch it. ![image](https://github.com/user-attachments/assets/80edbf7d-be50-4cb0-a016-f390b81dc798) The call to TraceLoggingRegisterEx() recursively calls back into LazyInitialize(): LazyInitialize() -> TraceLoggingRegisterEx() -> ORT_TL_EtwEnableCallback() -> Instance() -> LazyInitialize() The original code got out of the recursive loop by checking the `initialized_` flag.
1 parent 50170c6 commit cf565e9

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

onnxruntime/core/platform/windows/logging/etw_sink.cc

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ ULONGLONG EtwRegistrationManager::Keyword() const {
9898
return keyword_;
9999
}
100100

101+
HRESULT EtwRegistrationManager::Status() const {
102+
return etw_status_;
103+
}
104+
101105
void EtwRegistrationManager::RegisterInternalCallback(const EtwInternalCallback& callback) {
102106
std::lock_guard<OrtMutex> lock(callbacks_mutex_);
103107
callbacks_.push_back(&callback);
@@ -140,9 +144,15 @@ EtwRegistrationManager::EtwRegistrationManager() {
140144
}
141145

142146
void EtwRegistrationManager::LazyInitialize() {
143-
static HRESULT etw_status = ::TraceLoggingRegisterEx(etw_provider_handle, ORT_TL_EtwEnableCallback, nullptr);
144-
if (FAILED(etw_status)) {
145-
ORT_THROW("ETW registration failed. Logging will be broken: " + std::to_string(etw_status));
147+
if (!initialized_) {
148+
std::lock_guard<OrtMutex> lock(init_mutex_);
149+
if (!initialized_) { // Double-check locking pattern
150+
initialized_ = true;
151+
etw_status_ = ::TraceLoggingRegisterEx(etw_provider_handle, ORT_TL_EtwEnableCallback, nullptr);
152+
if (FAILED(etw_status_)) {
153+
ORT_THROW("ETW registration failed. Logging will be broken: " + std::to_string(etw_status_));
154+
}
155+
}
146156
}
147157
}
148158

@@ -161,6 +171,12 @@ void EtwSink::SendImpl(const Timestamp& timestamp, const std::string& logger_id,
161171
// register on first usage
162172
static EtwRegistrationManager& etw_manager = EtwRegistrationManager::Instance();
163173

174+
// do something (not that meaningful) with etw_manager so it doesn't get optimized out
175+
// as we want an instance around to do the unregister
176+
if (FAILED(etw_manager.Status())) {
177+
return;
178+
}
179+
164180
// TODO: Validate if this filtering makes sense.
165181
if (message.DataType() == DataType::USER) {
166182
return;

onnxruntime/core/platform/windows/logging/etw_sink.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ class EtwRegistrationManager {
6666
// Get the current keyword
6767
uint64_t Keyword() const;
6868

69+
// Get the ETW registration status
70+
HRESULT Status() const;
71+
6972
void RegisterInternalCallback(const EtwInternalCallback& callback);
7073

7174
void UnregisterInternalCallback(const EtwInternalCallback& callback);
@@ -97,6 +100,7 @@ class EtwRegistrationManager {
97100
bool is_enabled_;
98101
UCHAR level_;
99102
ULONGLONG keyword_;
103+
HRESULT etw_status_;
100104
};
101105

102106
} // namespace logging

0 commit comments

Comments
 (0)