Repository navigation
Initialize JSON schemas lazily on first use - #541
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(). (cherry picked from commit e9de49f in bisect-pt/nmos-cpp)
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.
This cherry-picks bisect-pt/nmos-cpp@e9de49f, by @massis08, onto
master.nmos-cpp built its map of JSON schemas during static initialization, so every process that loads the library parsed every IS-04 to IS-14 schema before
main. That parsing uses cpprestsdk'sscoped_c_thread_locale::c_locale(). In cpprestsdk 2.10.19, the version inconanfile.txtand in vcpkg,c_locale()isn't safe to call before cpprestsdk's own globals are initialized, for example when nmos-cpp isdlopen'ed while the process locale isn't "C".cpprest-lts/cpprestsdk#66 fixes
c_locale()and other cpprestsdk globals so that JSON parsing works during static initialization. That fix isn't in cpprestsdk 2.10.19, and Microsoft's repository is archived, so nmos-cpp builds against that version still need this change.The map is now a function-local static, so it is built on the first call to
load_json_schema(). Even with a fixed cpprestsdk, this avoids the start-up cost in processes that never validate a message. If building the map throws an exception, the exception now reaches the caller instead of terminating the process.Function-local statics are only thread-safe when the compiler implements C++11 thread-safe initialization. Dependencies.md already lists that as a requirement, so every supported compiler builds this correctly.
Compared with the original commit, this version has a shorter comment that doesn't depend on the cpprestsdk version, the commit message is rewrapped, and a trailing space is removed.
Co-authored-by: Gareth's 🤖 buddy