Skip to content

Initialize JSON schemas lazily on first use - #541

Merged
lo-simon merged 1 commit into
sony:masterfrom
garethsb:bugfix/lazy-json-schemas
Nov 17, 2026
Merged

lo-simon merged 1 commit into
sony:masterfrom
garethsb:bugfix/lazy-json-schemas

Conversation

@garethsb

Copy link
Copy Markdown
Contributor

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's scoped_c_thread_locale::c_locale(). In cpprestsdk 2.10.19, the version in conanfile.txt and in vcpkg, c_locale() isn't safe to call before cpprestsdk's own globals are initialized, for example when nmos-cpp is dlopen'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

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)

@lo-simon lo-simon left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lo-simon
lo-simon merged commit f504a70 into sony:master Nov 17, 2026
11 checks passed
@garethsb
garethsb deleted the bugfix/lazy-json-schemas branch November 18, 2026 19:45
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.

3 participants