fix(orm): replace data race in ENTITY_METADATA with std::call_once#558
Merged
Conversation
The ENTITY_METADATA() macro used a plain static bool for one-time initialization, creating a data race when multiple threads call get_metadata() concurrently for the first time. Replace with std::call_once + std::once_flag for guaranteed thread-safe initialization. Add #include <mutex> for std::once_flag and std::call_once. Fix pre-existing broken markdown anchors in docs/README.kr.md (emoji-prefixed headings generating mismatched TOC anchors, cross-file links to non-indexed guides/ directory). Closes #556
Contributor
Benchmark ResultsNo comparison reports available. Baseline may not be established yet. |
kcenon
added a commit
that referenced
this pull request
Apr 13, 2026
) The ENTITY_METADATA() macro used a plain static bool for one-time initialization, creating a data race when multiple threads call get_metadata() concurrently for the first time. Replace with std::call_once + std::once_flag for guaranteed thread-safe initialization. Add #include <mutex> for std::once_flag and std::call_once. Fix pre-existing broken markdown anchors in docs/README.kr.md (emoji-prefixed headings generating mismatched TOC anchors, cross-file links to non-indexed guides/ directory). Closes #556
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.
What
Summary
Replaces a data race in the
ENTITY_METADATA()macro by swapping the unsafestatic bool initializedcheck-then-act pattern withstd::call_once+std::once_flag, guaranteeing thread-safe one-time initialization of entity metadata.Change Type
Affected Components
database/orm/entity.h—ENTITY_METADATA()macro definitionWhy
Problem Solved
The
ENTITY_METADATA()macro used a plainstatic bool initializedflag with a manual check-then-act pattern that is not thread-safe under the C++11 memory model. When multiple threads callget_metadata()for the first time concurrently,initialize_metadata()could be called multiple times, leading to undefined behavior (data race) and potential duplicate field entries in the metadata.Related Issues
Alternative Approaches Considered
std::atomic<bool>with double-checked locking — More complex, same overhead ascall_onceWhere
Files Changed
database/orm/entity.hstatic boolwithstd::call_once, add#include <mutex>docs/README.kr.mdHow
Implementation Details
This aligns with the existing threading discipline in
backend_base::initialized_which correctly usesstd::atomic<bool>.Breaking Changes
None —
get_metadata()return type and behavior are unchanged.