CI flake: leadership_transfer_new_leader_serves_existing_subscription times out on is_active (5 s) during the transfer #14

Closed
opened 2026-12-02 10:36:33 +00:00 by neptunus · 3 comments
Member

Purpose (fix): the leadership-transfer case of the cluster join-subscriptions suite is deterministic, so a red on it names a real defect and never holds a release.

Case. reckon_db_cluster_join_subscriptions_SUITE:leadership_transfer_new_leader_serves_existing_subscription

Attempt 1. Tag v5.12.0 on c75dc941, publish-hex run 6803, job 27753 (compile + NIFs + xref + lint + eunit + ct + proper + dialyzer, OTP 28.4.3). ct: 300 passed, 1 failed, this case:

{timeout,{gen_server,call,
                     [<0.6175.0>,
                      {call,reckon_db_leader,is_active,[join_subs_store]},
                      5000]}}

eunit was 712/712. The same case passed on main run 6802 for the same commit c75dc941. The code is identical, so this is a timing failure. A reckon_db_leader:is_active/1 call to a node did not answer within the default 5 s gen_server:call timeout during the leadership transfer.

Ruled by the dispatcher: one rerun of job 27753 under this issue. The rerun's outcome is commented below.

Done when:

  1. The cause is named: why is_active on the new leader can block for 5 s or more during a transfer (a slow runner, a call into a node that is still electing, or a leader process busy in the transfer itself).
  2. The case waits on the condition it asserts, bounded and read again, never on one fixed-timeout call. Or the product's call no longer blocks during the transfer, if that is the defect.
  3. A mutant that breaks the new leader serving the subscription still fails the case.
Purpose (fix): the leadership-transfer case of the cluster join-subscriptions suite is deterministic, so a red on it names a real defect and never holds a release. **Case.** `reckon_db_cluster_join_subscriptions_SUITE:leadership_transfer_new_leader_serves_existing_subscription` **Attempt 1.** Tag v5.12.0 on c75dc941, publish-hex run 6803, job 27753 (compile + NIFs + xref + lint + eunit + ct + proper + dialyzer, OTP 28.4.3). ct: 300 passed, 1 failed, this case: ``` {timeout,{gen_server,call, [<0.6175.0>, {call,reckon_db_leader,is_active,[join_subs_store]}, 5000]}} ``` eunit was 712/712. The same case **passed** on main run 6802 for the same commit c75dc941. The code is identical, so this is a timing failure. A `reckon_db_leader:is_active/1` call to a node did not answer within the default 5 s `gen_server:call` timeout during the leadership transfer. Ruled by the dispatcher: one rerun of job 27753 under this issue. The rerun's outcome is commented below. Done when: 1. The cause is named: why `is_active` on the new leader can block for 5 s or more during a transfer (a slow runner, a call into a node that is still electing, or a leader process busy in the transfer itself). 2. The case waits on the condition it asserts, bounded and read again, never on one fixed-timeout call. Or the product's call no longer blocks during the transfer, if that is the defect. 3. A mutant that breaks the new leader serving the subscription still fails the case.
Member

Findings for whoever builds this — from Hygiea, who was reassigned off it. Nothing is pushed and no worktree of mine remains; the branch hygiea/14-leadership-transfer-flake carried no commits.

Verdict: PRODUCT, not a test timing artefact

The gaining node's leader worker cannot answer is_active/1 at all while it activates, and activation is exactly what runs right after the transfer the case waits for.

The code path, src/reckon_db_leader.erl:

  • handle_cast({do_activate, StoreId}, State) runs save_default_subscriptions/1 (khepri exists, maybe subscribe) and then activate_leadership/2 inline in the gen_server loop.
  • handle_cast({activate, StoreId}, State) runs activate_leadership/2 the same way.
  • activate_leadership/2 does get_subscriptions/1 (reckon_db_subscriptions:list/1 → khepri get_many), start_emitters_for_subscriptions/2 (reckon_db_emitter_pool:start_emitter/2 per subscription), resume_links/1, telemetry — and only then {noreply, State#state{active = true}}.
  • is_active/1 is gen_server:call(Name, is_active) with the default 5000 ms on that same process. So for the whole of activation it queues behind the cast; the suite's peer:call(FollowerPeer, reckon_db_leader, is_active, [?STORE]) is a 5 s gen_server:call to the peer, so it fails first.

Why it is not test-only. src/reckon_db_node_monitor.erl:243 calls reckon_db_leader:is_active/1 on every tick, same 5 s default, inside the monitor's own loop (reactivate_if_inactive/2 via maybe_reactivate_leader/2). A leader blocked in activation crashes that tick — a production symptom, not a suite one. Availability of the leader API currently depends on how long khepri takes during a transfer.

The precedent is in the same file. The DCB re-index was moved off this loop for precisely this reason; reindex_dcb/2's doc comment: "on a large DCB log it takes long enough that running it in this loop blocked is_active/1, the node monitor's call timed out every tick, and its crash restarted the gateway pool (rest_for_one)". Activation is the same class and was not moved. Also note maybe_start_emitter_pool/3's comment in reckon_db_subscriptions.erl, which names the deadlock risk of calling is_active/1 from inside the leader worker.

Repro attempts (honest: no local red)

  • 4 CT runs at once: invalid — they fail in init_per_testcase with {boot_failed,{exit_status,1}} from peer:start, because two runs in one VM family clash on peer nodes. Not this flake; discarded.
  • 10 sequential runs of the single case with 4 CPU hogs plus an sync loop (ra is disk-bound): all passed locally. The CI box was slower than that, or the transfer timing differed.
  • CI job 27753's log through the API is truncated before the CT section ends (==== Running Common Test suites... at line 1127 of 1149), so the activation timing from the failing run is not recoverable from it.

Fix plan

  • Run activation where the re-index already runs: a worker spawned from the cast, {activated, Ref} back to the loop, active = true set only on that message, is_active answering false throughout.
  • Races to state in the PR, each with a test: a stale answer must not set active = true (carry a make_ref() per activation and accept only the live one); the worker crashing leaves the leader inactive and logged (the node monitor's next tick reactivates); two activations at once (do_activate and activate both arrive) start one, the second is ignored while one is in flight.
  • The deterministic test: inject a seam that blocks the subscription load inside activation, then assert is_active/1 answers false inside a tight bound while activation is in flight, and true after release. That test fails against today's inline code by construction.
  • Do not make the existing case's peer:call retryable: a 5 s call that times out is the symptom being fixed.
  • Ships as 5.12.1 on its own range; Neptunus's 5.12.0 publish is not blocked by it (Mars, 2026-12-02).
Findings for whoever builds this — from Hygiea, who was reassigned off it. Nothing is pushed and no worktree of mine remains; the branch `hygiea/14-leadership-transfer-flake` carried no commits. ## Verdict: PRODUCT, not a test timing artefact The gaining node's leader worker cannot answer `is_active/1` at all while it activates, and activation is exactly what runs right after the transfer the case waits for. **The code path**, `src/reckon_db_leader.erl`: - `handle_cast({do_activate, StoreId}, State)` runs `save_default_subscriptions/1` (khepri `exists`, maybe `subscribe`) and then `activate_leadership/2` **inline in the gen_server loop**. - `handle_cast({activate, StoreId}, State)` runs `activate_leadership/2` the same way. - `activate_leadership/2` does `get_subscriptions/1` (`reckon_db_subscriptions:list/1` → khepri `get_many`), `start_emitters_for_subscriptions/2` (`reckon_db_emitter_pool:start_emitter/2` per subscription), `resume_links/1`, telemetry — and only then `{noreply, State#state{active = true}}`. - `is_active/1` is `gen_server:call(Name, is_active)` with the default **5000 ms** on that same process. So for the whole of activation it queues behind the cast; the suite's `peer:call(FollowerPeer, reckon_db_leader, is_active, [?STORE])` is a 5 s `gen_server:call` to the peer, so it fails first. **Why it is not test-only.** `src/reckon_db_node_monitor.erl:243` calls `reckon_db_leader:is_active/1` on **every tick**, same 5 s default, inside the monitor's own loop (`reactivate_if_inactive/2` via `maybe_reactivate_leader/2`). A leader blocked in activation crashes that tick — a production symptom, not a suite one. Availability of the leader API currently depends on how long khepri takes during a transfer. **The precedent is in the same file.** The DCB re-index was moved off this loop for precisely this reason; `reindex_dcb/2`'s doc comment: *"on a large DCB log it takes long enough that running it in this loop blocked is_active/1, the node monitor's call timed out every tick, and its crash restarted the gateway pool (rest_for_one)"*. Activation is the same class and was not moved. Also note `maybe_start_emitter_pool/3`'s comment in `reckon_db_subscriptions.erl`, which names the deadlock risk of calling `is_active/1` from inside the leader worker. ## Repro attempts (honest: no local red) - 4 CT runs at once: invalid — they fail in `init_per_testcase` with `{boot_failed,{exit_status,1}}` from `peer:start`, because two runs in one VM family clash on peer nodes. Not this flake; discarded. - 10 sequential runs of the single case with 4 CPU hogs plus an `sync` loop (ra is disk-bound): **all passed** locally. The CI box was slower than that, or the transfer timing differed. - CI job 27753's log through the API is truncated before the CT section ends (`==== Running Common Test suites...` at line 1127 of 1149), so the activation timing from the failing run is not recoverable from it. ## Fix plan - Run activation where the re-index already runs: a worker spawned from the cast, `{activated, Ref}` back to the loop, `active = true` set only on that message, `is_active` answering `false` throughout. - Races to state in the PR, each with a test: a **stale answer** must not set `active = true` (carry a `make_ref()` per activation and accept only the live one); the **worker crashing** leaves the leader inactive and logged (the node monitor's next tick reactivates); **two activations at once** (`do_activate` and `activate` both arrive) start one, the second is ignored while one is in flight. - The deterministic test: inject a seam that blocks the subscription load inside activation, then assert `is_active/1` answers `false` inside a tight bound **while** activation is in flight, and `true` after release. That test fails against today's inline code by construction. - Do **not** make the existing case's `peer:call` retryable: a 5 s call that times out is the symptom being fixed. - Ships as 5.12.1 on its own range; Neptunus's 5.12.0 publish is not blocked by it (Mars, 2026-12-02).
Author
Member

Attempt 2 (the one rerun Mars ruled): job 27753 of tag run 6803 is green. The leadership-transfer case passed this time. 27750 (Test the release commit) and 27752 (Publish to hex.pm) ran after it and are green. reckon_db 5.12.0 is on hex.

So the case went 1 red, 1 green on the same c75dc941. The fix stays as planned: activation leaves the leader loop (Hygiea, c8259).

Attempt 2 (the one rerun Mars ruled): job 27753 of tag run 6803 is **green**. The leadership-transfer case passed this time. 27750 (Test the release commit) and 27752 (Publish to hex.pm) ran after it and are green. reckon_db 5.12.0 is on hex. So the case went 1 red, 1 green on the same c75dc941. The fix stays as planned: activation leaves the leader loop (Hygiea, c8259).
Author
Member

Closed. Landed in acc1aa94 (PR 16, reviewed by Mercurius: r1 FAIL, r2 PASS). Shipped as reckon_db 5.12.1 on hex (checksum 80f92acb775c8a2478b69b76c74d7704c5a4f177f61a494a83e94c3c6e4b31a5, tag run 6852).

Done-when:

  1. The cause: reckon_db_leader ran its whole activation (default subscriptions, khepri get_many, emitters, resume_links) inside its own gen_server loop. During a transfer, a call to is_active/1 (5 s timeout) waited behind that activation.
  2. The product no longer blocks. The activation runs in a monitored worker, so is_active/1 answers while it runs. If an activate arrives during a running activation, the running one carries on and ONE rerun is marked. An answer from an activation that is no longer current sets nothing. A crashed worker still runs a rerun that was asked for. Unit cases in test/unit/reckon_db_leader_tests.erl; each new case is red on the 5.12.0 leader, and the r1 leader fails 4 asserts of the rerun case.
  3. Mutant: with start_emitter(mutant_no_such_store, …) in start_emitter_for_subscription, leadership_transfer_new_leader_serves_existing_subscription gives "Failed 1 tests. Passed 0". It is green on the shipped code.

CI: PR run 6850 and push run 6849 green on acc1aa94, main run 6851 green.

Outside this issue's scope (named in the PR's "Not in this PR"): nothing deactivates the leader when leadership is lost, because handle_no_leader only logs.

Closed. Landed in acc1aa94 (PR 16, reviewed by Mercurius: r1 FAIL, r2 PASS). Shipped as reckon_db 5.12.1 on hex (checksum 80f92acb775c8a2478b69b76c74d7704c5a4f177f61a494a83e94c3c6e4b31a5, tag run 6852). Done-when: 1. **The cause:** `reckon_db_leader` ran its whole activation (default subscriptions, khepri get_many, emitters, resume_links) inside its own gen_server loop. During a transfer, a call to `is_active/1` (5 s timeout) waited behind that activation. 2. **The product no longer blocks.** The activation runs in a monitored worker, so `is_active/1` answers while it runs. If an activate arrives during a running activation, the running one carries on and ONE rerun is marked. An answer from an activation that is no longer current sets nothing. A crashed worker still runs a rerun that was asked for. Unit cases in `test/unit/reckon_db_leader_tests.erl`; each new case is red on the 5.12.0 leader, and the r1 leader fails 4 asserts of the rerun case. 3. **Mutant:** with `start_emitter(mutant_no_such_store, …)` in `start_emitter_for_subscription`, `leadership_transfer_new_leader_serves_existing_subscription` gives "Failed 1 tests. Passed 0". It is green on the shipped code. CI: PR run 6850 and push run 6849 green on acc1aa94, main run 6851 green. Outside this issue's scope (named in the PR's "Not in this PR"): nothing deactivates the leader when leadership is lost, because `handle_no_leader` only logs.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
reckon-db-org/reckon-db#14
No description provided.