Repository navigation
Initialize JSON schemas lazily to avoid static initialization order issue - #3
Merged
Merged
Conversation
The schema map was built during static initialization, which parses JSON via cpprestsdk. cpprestsdk's scoped_c_thread_locale::c_locale() is not safe to call before cpprestsdk's own globals are initialized, e.g. when the library is dlopen'ed while the process locale is not "C". Replace the static map with a function-local static so the schemas are built on first call to load_json_schema().
LufeBisect
approved these changes
Oct 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reasoning
nmos::details::schemaswas a namespace-scope static built during static initialization bymake_schemas(), which parses the bundled JSON schemas with cpprestsdk.cpprestsdk's JSON parser uses
scoped_c_thread_locale, andscoped_c_thread_locale::c_locale()depends on cpprestsdk's own static globals. C++ doesn't guarantee initialization order across translation units or shared libraries, so the schemas can be parsed before cpprestsdk is ready. This shows up when nmos-cpp is loaded withdlopenwhile the process locale is not"C": the locale path in cpprestsdk is taken, and it uses state that hasn't been initialized yet.Explanation
schemasmap with aschemas()function that holds a function-local static. The map is now built on the first call, after static initialization has finished. Since C++11, function-local statics are initialized in a thread-safe way.load_json_schema()to callschemas(). Its behaviour is unchanged: it still throwsjson_exceptionwhen a schema ID isn't found.🤖 Generated with Claude Code
@LufeBisect