Skip to content

Fix static initialization fiasco problem with the logger and code calling into it from other TUs - #358

Merged
Philip Adams (PhilipBAdams) merged 6 commits into
microsoft:mainfrom
asklar:user/asklar/deferredLogger
Feb 22, 2023
Merged

Fix static initialization fiasco problem with the logger and code calling into it from other TUs#358
Philip Adams (PhilipBAdams) merged 6 commits into
microsoft:mainfrom
asklar:user/asklar/deferredLogger

Conversation

@asklar

@asklar Alexander Sklar (asklar) commented Feb 20, 2023

Copy link
Copy Markdown
Member

There is currently a dependency between the order of dynamic initializers for the logger and things that call into it, like CpuREP (see #356 for a related issue). This can result in crashes because the order of dynamic initializers across translation units is not deterministic in C++.

The fix is to access the logger via a function call that allows us to defer the logger initialization until it is needed (in a thread-safe manner). Using "magic statics" to do that.

An app will now be able to customize the log level (to suppress logging from dynamic initializers) by exporting a function with a certain name, which we will look up during GetLogger():

App code:

extern "C" __declspec(dllexport) LogLevel SPTAG_GetLoggerLevel() 
{ 
    return LogLevel::Empty;
}

Fixes #356

@asklar Alexander Sklar (asklar) changed the title Addresses static initialization fiasco problem with the logger and code calling into it from other translation units Fix static initialization fiasco problem with the logger and code calling into it from other TUs Feb 20, 2023
@PhilipBAdams
Philip Adams (PhilipBAdams) merged commit a5bd48e into microsoft:main Feb 22, 2023
@asklar
Alexander Sklar (asklar) deleted the user/asklar/deferredLogger branch August 29, 2023 15:06
L1ray (L1-ray) pushed a commit to L1-ray/SPTAG that referenced this pull request May 9, 2026
…ling into it from other TUs (microsoft#358)

* Addresses static initialization fiasco problem with the logger and code calling into it from other translation units

* fix namespace

* make logger init multithreading safe

* actually we can use magic statics to simplify

* Fix microsoft#356

* gate GMH/GPA on Windows only
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logger is not configurable until after it's been used

2 participants