fix: Remove race condition in memory allocator initialization - #376
fix: Remove race condition in memory allocator initialization#376yurekami wants to merge 1 commit into
Conversation
Fixes deepseek-ai#315 The gAllocatorInited boolean flag had a race condition where: - Multiple threads could see gAllocatorInited=false simultaneously - Memory ordering issues could cause threads to see gAllocatorInited=true before gAllocator was fully initialized The fix removes the redundant flag and relies solely on std::call_once, which already provides thread-safe initialization with proper memory ordering guarantees and an efficient fast-path after first execution. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
But wait, it doesn´t really remove a race condition.. It already calls The So the boolean flag is actually a perf optimization! If you run into the "race condition" it will just fall back to the |
|
@Spacefish The flag is intended as a fast path, agreed, but it is not a benign one. The harmless direction is the one you describe: a thread seeing a stale That stale-null read is not a slow path, it is a correctness bug: On the perf side the flag buys less than it appears to: the |
Summary
gAllocatorInitedflag that caused a race conditionstd::call_oncewhich provides proper thread-safety guaranteesProblem
The original code had a race condition in
GlobalMemoryAllocator.cc:Issues:
gAllocatorInitedis a plainbool, not atomic - no synchronizationgAllocatorInited = truecan execute beforestd::call_oncefully completesgAllocatorInited = truewhilegAllocatoris still uninitializedSolution
Remove the redundant flag and rely solely on
std::call_once:Why this works:
std::call_onceprovides full happens-before guarantees (C++11 standard)call_oncebecomes a fast single atomic load (efficient fast-path)Test plan
🤖 Generated with Claude Code