Skip to content

Fix static initialization order fiasco in AdaptiveMaxConcurrency#3315

Merged
chenBright merged 1 commit into
apache:masterfrom
Felix-Gong:fix-static-init-order
May 29, 2026
Merged

Fix static initialization order fiasco in AdaptiveMaxConcurrency#3315
chenBright merged 1 commit into
apache:masterfrom
Felix-Gong:fix-static-init-order

Conversation

@Felix-Gong
Copy link
Copy Markdown
Contributor

@Felix-Gong Felix-Gong commented May 29, 2026

Fix

Fixes #3271

Replace class-static std::string members (UNLIMITED and CONSTANT) 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.

// Before:
static const std::string UNLIMITED;

// After:
static const std::string& UNLIMITED() {
    static const std::string s_unlimited = "unlimited";
    return s_unlimited;
}

Changes

  • src/brpc/adaptive_max_concurrency.h — Static members → static member functions
  • src/brpc/adaptive_max_concurrency.cpp — Remove definitions, add () to calls
  • src/brpc/server.cpp / server.h — Update references
  • src/brpc/policy/constant_concurrency_limiter.cpp — Update references
  • test/brpc_adaptive_class_unittest.cpp — Update test references

Full library build passes. This fix benefits all architectures including x86_64, ARM64, and RISC-V.

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>
@yanglimingcn
Copy link
Copy Markdown
Contributor

LGTM

Copy link
Copy Markdown
Contributor

@chenBright chenBright left a comment

Choose a reason for hiding this comment

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

LGTM

@chenBright chenBright merged commit e9d4b19 into apache:master May 29, 2026
17 checks passed
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.

Static object initialization order leads to std::string crash

3 participants