Fix static initialization order fiasco in AdaptiveMaxConcurrency#3315
Merged
Conversation
AdaptiveMaxConcurrency has two class-static std::string members (UNLIMITED and CONSTANT) defined in adaptive_max_concurrency.cpp. Global AdaptiveMaxConcurrency objects in other translation units may be constructed before these static strings are initialized, causing undefined behavior. This issue was discovered during RISC-V porting and testing of BRPC. Different toolchains and linkers (GCC, Clang, cross-compilation toolchains for RISC-V, etc.) may produce different static initialization orders, making this bug manifest on some platforms but not others. Fix by replacing class-static std::string members with Meyers' Singleton pattern (function-local statics), which C++11 guarantees are initialized on first use in a thread-safe manner. This fix benefits all architectures including x86_64, ARM64, and RISC-V. Signed-off-by: Felix-Gong <gongxiaofei24@iscas.ac.cn>
Contributor
|
LGTM |
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.
Fix
Fixes #3271
Replace class-static
std::stringmembers (UNLIMITEDandCONSTANT) with Meyers' Singleton pattern (function-local statics) to avoid static initialization order fiasco.This issue was discovered during RISC-V porting and testing of BRPC. Different toolchains and linkers (GCC, Clang, cross-compilation toolchains for RISC-V, etc.) may produce different static initialization orders, making this bug manifest on some platforms but not others.
Changes
src/brpc/adaptive_max_concurrency.h— Static members → static member functionssrc/brpc/adaptive_max_concurrency.cpp— Remove definitions, add()to callssrc/brpc/server.cpp/server.h— Update referencessrc/brpc/policy/constant_concurrency_limiter.cpp— Update referencestest/brpc_adaptive_class_unittest.cpp— Update test referencesFull library build passes. This fix benefits all architectures including x86_64, ARM64, and RISC-V.