Skip to content

state_exec: exec_coordinator::start() announces its election without its leader lock - #863

Merged
transfix merged 1 commit into
masterfrom
fix/coordinator-start-lock-order
Oct 28, 2026
Merged

transfix merged 1 commit into
masterfrom
fix/coordinator-start-lock-order

Conversation

@transfix

@transfix transfix commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

exec_coordinator::start() held leader_mu_ while it called start_election(), which sends the announcement through the message bus. The bus hands a message to its subscribers on the thread that admits it, with its own (recursive) lock held, and a victory delivered there reaches accept_leader(), which takes leader_mu_. So start() took leader_mu_ and then the bus lock, while a victory took the bus lock and then leader_mu_. That is a lock-order inversion that can deadlock two threads (one starting a coordinator, one admitting a victory). On a single thread it deadlocks outright when a subscriber answers the election at once.

The new TSan job in #858 found it. It reported six lock-order inversions, all with the same pair of stacks: start() at exec_coordinator.cpp:262, then the bus at state_message_bus.cpp:61; against admin_handoff() or declare_victory() admitting a victory that reaches accept_leader() at exec_coordinator.cpp:343. They came from state_exec_coordinator_test (SingleNodeBecomesLeader, Handoff, TwoCoordinatorTest.HandoffBetweenNodes) and state_exec_integration_test (MultiClusterACLTest.LeaderHandoffTransfersAuthority). The logs are in run 40628489064's sanitizers-tsan-logs artifact.

The fix: start() reads whether a leader is known under leader_mu_, releases it, and then calls start_election(). A victory that arrives between the check and the election is handled the same way as before. The election it starts gets answered and settled by the bully protocol, just as when a victory arrives just after the announcement. request_election() and on_membership_event() already call start_election() without the lock.

The new test, CoordinatorTest.StartAcceptsAVictoryAnsweredDuringItsElection, subscribes a peer that answers start()'s election with a victory for node-B from inside the bus's dispatch. Before this change, start() relocked its own leader_mu_ there. glibc deadlocks on that, and MSVC throws, which the bus swallows along with the victory. I ran it on Windows without the fix and it failed with the leader still unknown. With the fix, the coordinator accepts node-B as leader, and state_exec_coordinator_test passes (62 tests). Until this lands, #858 suppresses these reports in .github/sanitizers/tsan.supp with an entry that names this PR.

transfix added a commit that referenced this pull request Oct 28, 2026
…ressions for the baseline's two bugs

The asan-ubsan job failed to link libcvc.so: UBSan's vptr check references the
typeinfo of every polymorphic class it checks, and Jolt is built without RTTI,
as are the two libcvc files that subclass its classes, so that typeinfo exists
nowhere ("undefined reference to `typeinfo for JPH::GroupFilter'"). The job now
builds with -fno-sanitize=vptr; the rest of UBSan stays on.

The tsan job's first run found two real bugs, each fixed in its own PR, and
suppressed here until those land:
- a lock-order inversion between exec_coordinator's leader_mu_ and the message
  bus's lock, from start() announcing its election under leader_mu_ (6 reports
  in state_exec_coordinator_test and state_exec_integration_test; #863);
- pycvc's evaluator.stats(), evaluator_state.stats and process.state not
  keeping their owner alive, because their %pythonappend was never generated
  (13 heap-use-after-free reports in test_pycvc_state_exec_eval.py; #864).
…its leader lock

start() held leader_mu_ while it called start_election(), which sends the
announcement through the message bus. The bus hands a message to its
subscribers on the admitting thread with its own (recursive) lock held, and a
victory delivered there reaches accept_leader(), which takes leader_mu_. So
start() took leader_mu_ then the bus lock, and a victory took the bus lock then
leader_mu_: a lock-order inversion that can deadlock two threads (one starting
a coordinator, one admitting a victory). On one thread, when a subscriber
answers the election at once, start() relocks its own leader_mu_: glibc
deadlocks, and MSVC throws, which the bus swallows along with the victory.

The new TSan job (#858) reported it six times, in
state_exec_coordinator_test (SingleNodeBecomesLeader, Handoff,
TwoCoordinatorTest.HandoffBetweenNodes) and state_exec_integration_test
(MultiClusterACLTest.LeaderHandoffTransfersAuthority): start() at
exec_coordinator.cpp:262 then the bus at state_message_bus.cpp:61, against
admin_handoff or declare_victory admitting a victory that reaches
accept_leader at exec_coordinator.cpp:343.

start() now reads whether a leader is known under leader_mu_ and calls
start_election() after releasing it. A victory that arrives between the two is
handled as before: the election it starts is answered and settled by the bully
protocol. CoordinatorTest.StartAcceptsAVictoryAnsweredDuringItsElection
subscribes a peer that answers the election with a victory from inside the
dispatch. Without the fix it fails on Windows (the leader stays unknown) and
would hang on Linux.
@transfix
transfix force-pushed the fix/coordinator-start-lock-order branch from 8106cdf to 4d5bcd2 Compare October 28, 2026 05:57
@transfix
transfix marked this pull request as ready for review October 28, 2026 17:19
@transfix
transfix merged commit 87c22fe into master Oct 28, 2026
24 of 25 checks passed
transfix added a commit that referenced this pull request Oct 28, 2026
transfix added a commit that referenced this pull request Oct 28, 2026
#863 (exec_coordinator::start() announcing its election without its leader
lock) is merged, so tsan.supp no longer hides the lock-order inversion it fixed.
tsan.supp is empty again.
transfix added a commit that referenced this pull request Oct 28, 2026
The project version and the cvcpkg recipe family move to 3.7.2. Every recipe is at cvc_revision 1
already, so none restarts.

3.7.2 carries the dynamic plants on the terrain and in a Jolt world, and the four-wheel plant
(#869): --plant dynamic and dynamic4w drive the ground, collide with the
buildings and each other, and book the collisions. It also carries dyn_4w's port-Hamiltonian form
(#850), dyn_bike's wheel-frame front slip (#852), the books goldens for GRL-SNAM's twins (#848,
#849), pycvc's drive telemetry (#857) and keep-alives (#864), the macOS SDK's openssl (#846), and
the state_exec scheduler and evaluator work (#842, #843, #844, #861, #863).
transfix added a commit that referenced this pull request Oct 28, 2026
… deep workflow (#858)

* ci: TSan and ASan+UBSan jobs over the threaded state_exec suites, and the locks deep workflow

The state_exec lock work (docs/STATE_EXEC_LOCKS.md) adds code that runs on
several threads, and no CI lane ran a race detector or an address checker. This
lands them before any lock code, so that the races and leaks already in the
tree get a baseline first.

- ci.yml `sanitizers` (tsan, asan-ubsan): gcc's -fsanitize=thread, or
  -fsanitize=address,undefined, over libcvc itself (the cvcpkg deps stay
  uninstrumented), RelWithDebInfo, building only the targets it runs. It runs
  the existing suites that use threads (state_exec_scheduler, _async,
  _integration and _coordinator tests, ariadne_runtime_test, thread_pool_test,
  async_lane_test) and pycvc's exec scripts, with the sanitizer runtime
  preloaded into the interpreter. Suppressions go in .github/sanitizers/, each
  with a comment naming what it hides. Each lock phase appends its suites.
- ci.yml's pull_request filter takes perf/** as well, so the lock phases'
  stacked drafts get the full set of jobs.
- locks-deep.yml (daily, and by hand on a branch): a Release Linux build of the
  lock test targets, running their deep sizes. A dispatchable workflow must be
  on the default branch, so it lands first; on a ref without the lock tests it
  stops after the checkout.

* ci: sanitizers / asan-ubsan without UBSan's vptr check, and TSan suppressions for the baseline's two bugs

The asan-ubsan job failed to link libcvc.so: UBSan's vptr check references the
typeinfo of every polymorphic class it checks, and Jolt is built without RTTI,
as are the two libcvc files that subclass its classes, so that typeinfo exists
nowhere ("undefined reference to `typeinfo for JPH::GroupFilter'"). The job now
builds with -fno-sanitize=vptr; the rest of UBSan stays on.

The tsan job's first run found two real bugs, each fixed in its own PR, and
suppressed here until those land:
- a lock-order inversion between exec_coordinator's leader_mu_ and the message
  bus's lock, from start() announcing its election under leader_mu_ (6 reports
  in state_exec_coordinator_test and state_exec_integration_test; #863);
- pycvc's evaluator.stats(), evaluator_state.stats and process.state not
  keeping their owner alive, because their %pythonappend was never generated
  (13 heap-use-after-free reports in test_pycvc_state_exec_eval.py; #864).

* ci: sanitizers / asan-ubsan runs pycvc with libstdc++ preloaded, and settles its first baseline

The asan-ubsan job's first full run (40639662336) found:
- Every pycvc script stopped at its first C++ throw on ASan's CHECK that it had
  found the real __cxa_throw. ASan looks it up when it starts, and the
  interpreter is C, so libstdc++ was not loaded yet. run.sh now preloads
  libstdc++ after the ASan runtime.
- Four StepDeadlineTest cases in state_exec_scheduler_test failed with "native
  calls nested too deeply (0 levels)": libcvc measured its stack from a local,
  which ASan's use-after-return check moves to a fake stack. Fixed in #866;
  until it lands the job runs with detect_stack_use_after_return=0.
- LeakSanitizer reported state_exec's reference cycles in
  state_exec_async_test and state_exec_integration_test, all indirect leaks:
  garbage only collect_cycles() frees, and these tests exit before any
  collection. lsan.supp hides them by fixture, with the reasons.

test_pycvc_state_exec_eval.py is not run under ASan until #864 lands: it would
stop at the heap-use-after-free that #864 fixes, and ASan can't suppress one.
run.sh's py_skip lists it, and the job summary shows it as skipped.

* ci: sanitizers drop the #864 workarounds now that it is on master

#864 (pycvc keep-alives for references into an evaluator, a state or a
process) is merged, so tsan.supp no longer hides its heap-use-after-free
reports and test_pycvc_state_exec_eval.py runs under ASan again. py_skip stays,
empty, for the next report ASan can't suppress.

* ci: sanitizers drop the #863 suppression now that it is on master

#863 (exec_coordinator::start() announcing its election without its leader
lock) is merged, so tsan.supp no longer hides the lock-order inversion it fixed.
tsan.supp is empty again.

* ci: sanitizers drop the #866 workaround now that it is on master

#866 (the native-stack guard measuring from the frame, not a local) is merged,
so asan-ubsan runs with ASan's default use-after-return check again, which also
checks #866 under gcc's ASan. No workaround for a fix in flight is left.
@transfix
transfix deleted the fix/coordinator-start-lock-order branch October 29, 2026 20:53
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