fix(healing): handle post-heal verify exceptions without crashing loop

This commit is contained in:
patrick committed 2026-03-01 21:14:59 -05:00
1 parent 9edce6b31d
commit 9b53a4c491
2 files changed
+51 -13

No files matched your search

+27 -12
View File
@@ -105,19 +105,34 @@ def execute_heal_and_verify(auditor, reporter, count, drift, heal_verify_delay):
f"(pre_fingerprint={drift.get('fingerprint', '')})." f"(pre_fingerprint={drift.get('fingerprint', '')})."
) )
time.sleep(heal_verify_delay) time.sleep(heal_verify_delay)
post_is_synced, post_count, post_drift = auditor.reconcile() try:
heal_result = "success" if post_is_synced else "failed" post_is_synced, post_count, post_drift = auditor.reconcile()
verify_error = None if post_is_synced else "Post-heal verification failed" heal_result = "success" if post_is_synced else "failed"
reporter.update_status(post_is_synced, post_count, last_error=verify_error) verify_error = None if post_is_synced else "Post-heal verification failed"
reporter.update_status(post_is_synced, post_count, last_error=verify_error)
logging.warning( logging.warning(
"Post-heal verification result " "Post-heal verification result "
f"result={heal_result} pre={drift.get('fingerprint', '')} " f"result={heal_result} pre={drift.get('fingerprint', '')} "
f"post={post_drift.get('fingerprint', '')} " f"post={post_drift.get('fingerprint', '')} "
f"missing={post_drift.get('missing_count', 0)} " f"missing={post_drift.get('missing_count', 0)} "
f"extra={post_drift.get('extra_count', 0)} " f"extra={post_drift.get('extra_count', 0)} "
f"metadata={post_drift.get('metadata_drift_count', 0)}" f"metadata={post_drift.get('metadata_drift_count', 0)}"
) )
except Exception as e:
heal_result = "verify_error"
reporter.update_status(False, count, last_error=f"Post-heal verify error: {e}")
post_drift = {
"reason": "verify_error",
"missing_count": 0,
"extra_count": 0,
"metadata_drift_count": 0,
"fingerprint": "",
}
logging.error(
"Post-heal verification crashed "
f"(pre_fingerprint={drift.get('fingerprint', '')}): {e}"
)
else: else:
reporter.update_status(False, count, last_error="Bouncer restart failed") reporter.update_status(False, count, last_error="Bouncer restart failed")
logging.error( logging.error(
+24 -1
View File
@@ -15,9 +15,10 @@ class FakeReporter:
class FakeAuditor: class FakeAuditor:
def __init__(self, heal_ok=True, reconcile_result=None): def __init__(self, heal_ok=True, reconcile_result=None, reconcile_exception=None):
self.heal_ok = heal_ok self.heal_ok = heal_ok
self.reconcile_result = reconcile_result self.reconcile_result = reconcile_result
self.reconcile_exception = reconcile_exception
self.last_heal_drift = None self.last_heal_drift = None
def heal(self): def heal(self):
@@ -28,6 +29,8 @@ class FakeAuditor:
return self.heal_ok return self.heal_ok
def reconcile(self): def reconcile(self):
if self.reconcile_exception:
raise self.reconcile_exception
return self.reconcile_result return self.reconcile_result
@@ -108,6 +111,26 @@ class PostHealVerificationTests(unittest.TestCase):
self.assertEqual("not_verified", returned_post_drift["reason"]) self.assertEqual("not_verified", returned_post_drift["reason"])
self.assertEqual([(False, 7, "Bouncer restart failed")], reporter.update_calls) self.assertEqual([(False, 7, "Bouncer restart failed")], reporter.update_calls)
@patch("healing.time.sleep", return_value=None)
def test_execute_heal_and_verify_verify_exception(self, _sleep):
reporter = FakeReporter()
auditor = FakeAuditor(heal_ok=True, reconcile_exception=RuntimeError("boom"))
heal_result, returned_post_drift = execute_heal_and_verify(
auditor,
reporter,
count=7,
drift={"fingerprint": "pre123"},
heal_verify_delay=0,
)
self.assertEqual("verify_error", heal_result)
self.assertEqual("verify_error", returned_post_drift["reason"])
self.assertEqual(1, len(reporter.update_calls))
self.assertFalse(reporter.update_calls[0][0])
self.assertEqual(7, reporter.update_calls[0][1])
self.assertIn("Post-heal verify error: boom", reporter.update_calls[0][2])
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()