[Bug] get_ha_status waits for ha_lock while holding it once an interrupted recovery has a moved guest
Describe the bug
In an automatic group, when the recovery journal holds an interrupted run with a moved guest, PegaProxManager.get_ha_status() never returns. The thread waits for self.ha_lock, which it took itself a few lines earlier. Everything else that needs ha_lock on that cluster (the HA monitor, a recovery) waits behind it.
The chain, in pegaprox/core/manager.py:
get_ha_statusbuilds the status insidewith self.ha_lock:.ha_lockis athreading.Lock(), not reentrant.- Still inside that block:
if ha.lease_in_force(): status['interrupted_recoveries'] = self.ha_interrupted_recoveries(). ha_interrupted_recoveriescallsself._ha_hold_if_back(rec, looks)for a run with moved guests._ha_hold_if_backcallsself._ha_seen_since(node, since)._ha_seen_sincedoeswith self.ha_lock:to readha_node_status.
One more thing happens under the lock on the way: ha_interrupted_recoveries calls self.get_vm_resources(), a request to the cluster, although the comment at the top of get_ha_status says the request for the node count is made outside the lock to avoid deadlocks.
get_ha_status is what the HA routes of pegaprox/api/clusters.py answer with (status, enable, disable, config).
Steps to Reproduce
I do not have an automatic group with an interrupted recovery to show this on. So the reproducer runs the real methods on a stand-in manager. It replaces only the journal read and the lease question:
- A
MagicMockmanager with a realthreading.Lock()asha_lock, and the realget_ha_status,ha_interrupted_recoveries,_ha_hold_if_backand_ha_seen_sincebound to it. ha.lease_in_forceanswers True, andha.recovery_leftoversreturns one run withmoved: [101]and amoved_at.- Call
get_ha_status()in a thread and wait 5 seconds.
Result on Testing 88b94e45:
tests/zz_proof_ha_status_lock.py .F
E AssertionError: get_ha_status did not return in 5 s; ha_lock is held: True
1 failed, 1 passed in 5.77s
The test that passes is the same call with an empty journal. The status comes back with interrupted_recoveries == [], so the stand-in manager is enough for the function to run to its end.
The reproducer is 60 lines. I can paste it here or put it in a PR.
Expected behavior
get_ha_status returns. The interrupted recoveries are read before or after the block that holds ha_lock, not inside it.
Environment
- PegaProx Version: 1.4.1 (Testing 88b94e45). The same call is on
main, so the 1.4.1 release has it too. - Installation Method: Docker (python:3.12 container running the test suite)
- OS: Debian 12 (container)
- Browser: not involved
- Behind Reverse Proxy? No
- UI? not involved
Logs
E AssertionError: get_ha_status did not return in 5 s; ha_lock is held: True
Screenshots
None.
If moving the call out of the lock is the fix you want, I can send that with the reproducer turned into a test. Making ha_lock an RLock would also stop the wait, but it would leave the cluster request under the lock.
Checklist
- [x] I have searched existing issues to make sure this is not a duplicate
- [x] I am using the latest version of PegaProx
- [x] I've put enough in here that someone can start on it without asking me three questions first
- [x] I understand PegaProx is maintained by volunteers in their free time. Nobody is on call, and writing URGENT doesn't get anyone out of bed 🪄
Good catch, and the stand-in reproducer makes the chain easy to follow. It matches the code on Testing: get_ha_status asks ha_interrupted_recoveries() while it holds ha_lock, a plain Lock, and _ha_seen_since takes that same lock again, with get_vm_resources() on the way. Reading the interrupted recoveries after the locked block looks like the natural fix to me, but this is the HA group code, so I've noted it for the maintainers to confirm the direction before a PR.
Cheers, Jan-Philipp
Thanks Jan-Philipp. I wrote it the way you describe, so it is ready if the maintainers confirm the direction: https://github.com/gputier/project-pegaprox/tree/fix/ha-status-lock (one commit on Testing 99df4577, manager.py and one test file).
The interrupted recoveries are now read after the with self.ha_lock block. Nothing else moves. The test calls get_ha_status in a thread with a two second limit, with a lease in force and a half-done recovery on record:
E AssertionError: get_ha_status still waits for ha_lock after 2 s
1 failed, 3 passed in 2.47s
The run above is on Testing. With the change, 4 passed. The HA tests next to it (tests/test_ha_exits.py, tests/test_ha_guard_rounds.py, tests/test_ha_interrupted_recoveries.py): 195 passed. Full suite: 16034 passed, and the same 10 failures as on Testing without the change in my container (16030 passed there).
One side effect: the recovery list is read a few lines after the node states, outside the lock. So the two may be one monitor round apart.
I will not open the PR until one of you says so. If you would rather make the lock reentrant or fix it another way, drop the branch.
Agreed, reading the interrupted recoveries after the locked block is the direction we're taking as well. It goes in on Testing with today's follow-ups.
Cheers, Marcus
This is in 1.5.0, released today: the HA status no longer waits on its own lock after an interrupted recovery. Closing.