omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty proposal] fix(backend): robust exception chaining traversal and resilience guards in firestore transaction retry ($50 proposed)

Open ajin558 opened this issue 2 months ago • 5 comments

Summary of Problem & Architectural Gap

backend/database/firestore_transaction_retry.py provides bounded retry orchestration (run_with_transaction_contention_retry) for Firestore transactions when contention occurs, used across 19 core backend services (such as folders.py, chat.py, notifications.py, action_items.py, and conversation_finalization_jobs.py).

However, several architectural edge cases and defensive gaps currently leave transaction retry vulnerable to premature failures:

  1. Incomplete Exception Chaining Traversal:

    • _is_transaction_contention previously traversed only __cause__.
    • In Python exception handling (PEP 3134), when an exception is raised within an except block without explicit from err (implicit chaining), Python sets __context__ while __cause__ remains None. Any service layer or middleware wrapping a FirestoreAborted error without explicit chaining caused _is_transaction_contention to return False, failing immediately rather than retrying.
    • Modern exception groups (PEP 654) or cyclic reference chains were also unhandled or could loop infinitely without visited-id cycle tracking.
    • Status codes indicating contention (code == 409, status_code == 409, grpc_status_code == 10 / ABORTED) were not recognized if wrapped in non-google.api_core error shapes.
  2. Missing Parameter Sanitization & Defensive Boundaries:

    • max_attempts lacked type and boolean validation (isinstance(True, int) is True in Python; passing booleans or non-integers led to undefined iteration).
    • operation_name was un-sanitized, leading to formatting anomalies if passed empty or non-string values.
    • Callables (transaction_factory, operation, sleep, random_value) lacked callable validation.
  3. Jitter Calculation Resilience & NaN/Inf Protection:

    • delay calculation lacked bounds checking against corrupted random generators (e.g. NaN, Inf, or out-of-range floats), which would propagate to sleep(delay) and raise unhandled runtime exceptions.
    • Exponent calculation was uncapped against very large attempt counts.
  4. Observability Hook:

    • Added an optional on_retry callback parameter to run_with_transaction_contention_retry allowing audit/metrics collection on retry attempts, guarded with fault isolation so observer exceptions never crash the transaction retry pipeline.
  5. Hermetic Test Coverage:

    • There was zero dedicated unit test coverage for firestore_transaction_retry.py. We provide a comprehensive 29-test hermetic test suite covering all retry branches, recovery, exhaustion, cause/context unwrapping, jitter bounds, and error isolation.

Proposed Solution & Scope

  • Public Predicate Export: Export is_transaction_contention(error: BaseException) -> bool with cycle-safe BFS traversal over __cause__, __context__, and nested exception groups.
  • Input Validation: Validate max_attempts (strict positive int, rejecting bools/floats), sanitize operation_name, validate callables.
  • Delay Calculation: Guard against NaN/Inf and negative values, clamp exponent to _MAX_EXPONENT = 10.
  • Observer Hook: Support on_retry callback with exception isolation.
  • Hermetic Tests: 29 tests in backend/tests/unit/test_firestore_transaction_retry.py with 100% pass rate in <0.2s.

Bounty proposed: $50 USD EVM Payout Address: 0x3dB8636F9C32DeF9D96079A9eCF272E174093A11 (Base / Arbitrum / Polygon USDC) Failure-Class: none

ajin558 avatar Oct 01 '26 04:10 ajin558

Implemented in PR https://github.com/BasedHardware/omi/pull/20454 with complete cycle-safe traversal, parameter sanitization, jitter resilience, observer hook isolation, and 26 hermetic unit tests with 100% pass rate.

pranav440 avatar Oct 03 '26 12:10 pranav440

PR https://github.com/BasedHardware/omi/pull/20454 addresses the full scope with 45 passing hermetic tests.

Beneficiary Payout Address:

pranav440 avatar Oct 03 '26 19:10 pranav440

Updated payout preferences for PR https://github.com/BasedHardware/omi/pull/20454:

  • EVM (Base / Arbitrum / Polygon USDC): 0x8dDaD7F7D9C173c427488AEC48346dA305cb03fF
  • Solana (USDC / SOL): Hy6jZiUbgefThi2hunTg3MacsuJyMe9n9QhJtWhxXSuF

pranav440 avatar Oct 04 '26 15:10 pranav440

Hi @drdz23, thanks for checking! Yes, this issue is already fully implemented and verified in PR #20454 (which has passed 2 review rounds with 39 passing hermetic tests and is currently awaiting final maintainer sign-off).

pranav440 avatar Oct 09 '26 17:10 pranav440

Thanks for putting this together. We aren't doing bounties at the moment, so we're closing the bounty proposal issues for now — thanks for the fix! 🙏

undivisible avatar Oct 22 '26 11:10 undivisible