Repository navigation
fix(build): repair singleton double-checked locking race and harden aarch64 portability - #203
Merged
Merged
Conversation
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.
Purpose
Following up on the aarch64 port (#181), an ARM portability audit (7-category static sweep plus on-hardware validation on a 128-core Kunpeng-920, ARMv8.2) found one real concurrency bug currently hidden by x86 TSO and three latent UB/divergence risks:
Singleton<T>double-checked locking is broken on weak memory models. The instance pointer was published with a plain store guarded only by a compiler-onlyMEMORY_BARRIER, while the fast path used a plain non-atomic load. A reader can therefore observe a non-null pointer before construction is visible. The fix usesstd::atomic<T*>with an acquire fast-path load and release publication, while preservingLazyInstantiation::Create(T*&)'s public signature.IOHook::Impl::mode_has a data race.Reset()writes the plain enum while IO threads read it inTry(). The fix makes itstd::atomic<Mode>and stores it before the existing sequentially consistentpos_andio_count_stores.SerializationUtils::DeserializeBinaryRowviolates strict aliasing. It read an arity from a byte-filled buffer throughreinterpret_cast<int32_t*>. The fix usesmemcpy, matching the serialize side without changing the wire format.Extreme cache/block sizes can trigger undefined
double -> intconversions.CacheManagerandSstFileWritercould produce architecture-dependent results for pathological configurations. A common-layerSaturatingDoubleToInteger<T>helper now implements Java-style saturation and is used at both sites.FieldSumAggINT8 sum/negation was also audited and intentionally left unchanged: modulo-256 addition and negation produce the same stored bits regardless of plain-charsignedness. Unlike the min/max comparisons fixed in #181, signedness cannot change these results.Out of scope (documented follow-ups): tightening option validation for
btree-index.block-size/cache-page-size, and the unchecked int64-to-int32 narrowing inCoreOptions::GetCachePageSize.Tests
The concurrency tests were verified red before the fix and TSan-clean after it on the Kunpeng-920:
SingletonTest.TestConcurrentFirstPublicationSingleton<T>on a translation-unit-local type, so no link order,--gtest_shuffle, or--gtest_filtercan pre-publish the instance and silently degrade the gate into the fast path; each thread also verifies full construction visibilityIOHookTest.TestConcurrentResetAndTryReset()/Clear()andTry()access; a shared start barrier releases all threads together and the reset loop runs until every worker finishes, so overlap is structural rather than timing-dependentSerializationUtilsTest.TestDeserializeBinaryRowFromStreamCacheManagerTestSaturatingCastTestThe
FactoryCreatorfirst-publication storm from the initial revision was removed because staticREGISTER_PAIMON_FACTORYconstructors initialize it beforemain(), so it cannot test first publication reliably. For the same reason the storm no longer borrowsSingleton<IOHook>:GetInstance()is now defined in the header, so the test instantiatesSingleton<T>for its own TU-local type and controls first publication by construction.FactoryCreatorandIOHookkeep extern-template declarations with explicit instantiations insingleton.cpp, because the-Bsymbolicplugin shared libraries must share exactly one definition (implicit per-library copies split the singleton and factory registrations are lost).Validation on the latest head (Kunpeng-920, aarch64):
paimon-common-factories-test: 9/9 passed (Debug), including 20×--gtest_shuffleruns and a--gtest_repeat=5 --gtest_shufflerun proving order independencepaimon-common-test: 1436/1436 passed andpaimon-common-sst-file-format-test: 32/32 passed (Debug)--gtest_shufflewith zero reportspre-commit run --files <changed>: all checks passedgit diff --check: cleanAPI and Format
No public API or storage-format change.
LazyInstantiation::Create(T*&)remains unchanged, and the big-endian arity prefix is preserved and pinned by tests.Documentation
No documentation changes needed; code comments explain the ordering and saturation rationale.
Generative AI tooling
Generated-by: Claude Code (claude-opus-4-8)