Skip to content

tests, lib: unit tests build and pass on GCC 13; LockFreeChanger reads its shared_ptr atomically - #77

Open
EnzinoBB wants to merge 4 commits into
CREDITSCOM:masterfrom
CreditsCommunity:official/tests-gcc13
Open

EnzinoBB wants to merge 4 commits into
CREDITSCOM:masterfrom
CreditsCommunity:official/tests-gcc13

Conversation

@EnzinoBB

@EnzinoBB EnzinoBB commented Nov 1, 2026

Copy link
Copy Markdown

Why this PR

With ENABLE_TESTS=ON the nodetests target no longer compiles on a current toolchain (GCC 13), and two tests of the system suite can hang. One of the hangs exposed a real defect: cs::LockFreeChanger replaces its value with std::atomic_compare_exchange on a std::shared_ptr, but reads the same shared_ptr without atomic access. A reader copying it while a writer swaps it is a data race on the control block and can free the value under the reader. The production user is ConfigHolder, read by the whole node while a config reload can replace it.

What changes

  • lib/include/lib/system/lockfreechanger.hpp: every read of data_ uses std::atomic_load_explicit (the same free-function family the class already uses for the compare-exchange); operator* keeps the loaded shared_ptr alive while copying the value; #include <memory> added. Interface and behaviour unchanged.
  • tests/csnode/conveyer_tests.cpp: PoolMetaInfo gained a reward field after timestamp; the aggregate initializer is shifted accordingly (empty reward, expectations unchanged).
  • tests/csnode/poolsynchronizer_tests.cpp: PoolSynchronizer::neighbours() was removed in 3ed6b98; the helper and its assertion are dropped from the already disabled test.
  • tests/lib_system/lib_system_tests.cpp (LockFreeChanger.BaseUsageSimple): the writer's "different from previous" loop had an inverted condition and never ended when the writer thread started first; the reader asserts only once a value was written.
  • tests/lib_system/process_tests.cpp (Process.HighLoadUsage): two busy-waits could miss their event under load (a lost wake-up in the test); the test now waits on the finish count with a deadline. lib/system/process.hpp is untouched.

Effect on the network

None. Only the test suite and the atomicity of LockFreeChanger reads change; nothing on the wire or in consensus.

Tests

Built and run on 4a95aaa with GCC 13 and Boost 1.83 (static): nodetests, lmdbxxtests and lib_system all pass. Before the fix, LockFreeChanger.BaseUsage crashed in 9 of 30 local runs, and ThreadSanitizer reported 8 data races on a small program with 2 writers and 2 readers (0 after). The build also needs -include cstdint -include cstddef on GCC 13 for two third-party/legacy headers; that is a build-flag matter and is not part of this PR.

Same change in the community fork: CreditsCommunity#1, #2 and #3, running on mainnet community nodes.

EnzinoBB and others added 4 commits November 1, 2026 12:07
The nodetests target no longer compiled, so none of the csnode unit tests could run.
Test-only change: production code is untouched.

- conveyer_tests: PoolMetaInfo gained a `reward` field after `timestamp`, so the
  aggregate initializer was shifted by one. Pass an empty reward; applyCharacteristic
  adds the reward user field only when it is non-empty, so expectations are unchanged.
- poolsynchronizer_tests: PoolSynchronizer::neighbours() was removed in 3ed6b98
  (new synchronization algorithm). Neighbours are now a private unordered_map, so the
  "sorted by sequence" invariant no longer exists. Drop the helper and its assertion
  from the already DISABLED test, with a comment explaining why.

Result with ENABLE_TESTS=ON (GCC 13, Boost 1.83 static): nodetests 110/110,
lmdbxxtests 13/13 and lib_system 49/49 pass.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This made the `system` suite hang on the GitHub runner (ctest timeout at 300 s
in run 37647459022). Test-only change: production code is untouched.

- The writer generated a value "different from previous" with
  `do { ... } while (randomValue != previous.data())`. The condition is
  inverted: on the first pass previous is 0 and values are drawn from
  [1, INT_MAX], so the loop never ends. Locally the writer thread usually
  started after `isRunning` was already false, which hid the bug; on the runner
  it started first and spun forever. Use `==`.
- With the loop fixed, a second race became visible: the reader's
  wait_for(250 ms) can time out before the writer stored anything, and then
  `desired != previous` compares 0 with 0. Only assert once a value has been
  written.

Locally: 3000/3000 repetitions pass, and 1000/1000 with every core busy.
Before the fix it hung within 4 repetitions.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
cs::LockFreeChanger replaces its value with std::atomic_compare_exchange on a
std::shared_ptr, but read the same shared_ptr with plain, non-atomic accesses:
- exchange() itself: `current = data_;`
- operator-> and operator* (and so data()).

A reader copying the shared_ptr while a writer swaps it is a data race on the
control block, which can free the value under the reader (use-after-free).
Production user: ConfigHolder (csnode/configholder.hpp), which serves
ConfigHolder::instance().config() to the whole node while
config::Observer::configChanged can exchange it on a config reload.

Fix, keeping interface and behaviour unchanged:
- use std::atomic_load_explicit (same free-function family the class already
  uses for compare_exchange) for every read of data_;
- operator* keeps the loaded shared_ptr alive while copying the value;
- add the missing #include <memory>.

Evidence:
- ThreadSanitizer, minimal program with 2 writers and 2 readers for 300 ms:
  8 data-race reports before, 0 after (3 runs).
- tests: LockFreeChanger.BaseUsage crashed (SIGSEGV/abort) in 9 of 30 runs
  locally before the fix.

Not changed (pre-existing, needs an API change): ConfigHolder::config() returns
a raw `const Config*`. After a reload, the previous Config can be freed while a
caller still holds that pointer.

Cost: with libstdc++, atomic_load on a shared_ptr takes an uncontended
lock from a small lock pool. config() is called per round/event, not in tight
loops.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The hang is in the test, not in cs::Process. Diagnosis on the GitHub runner
under stress-ng (run 37689274802) hung at iteration 1. The log showed
"Launch count 250 / Finish count 250", so every `finished` signal was
delivered. gdb showed a single thread left, spinning in
Process_HighLoadUsage_Test::TestBody().

Two busy-waits could miss their event:
- After the last launch the test set `lastExecution = true` and spun until
  `finished` reset it to false. Under load the last child (it runs ~10 ms)
  can finish and emit `finished` before the flag is set to true, and then
  nothing ever clears it. Wait on finishCount instead, with a 10 s deadline,
  so a really lost signal fails the assertions instead of hanging.
- `while (!process.isRunning());` right after the first launch spins forever
  if the child is already gone. Also stop when it has already finished.

Test-only change: production code (lib/system/process.hpp) is untouched.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant