Skip to content

bug: hand-rolled Redis single-flight locks — 7 divergent copies; system_seed_service releases with an unconditional tokenless delete #1920

Closed

Description

@webmixgamer

Summary

The SETNX single-flight lock idiom is hand-rolled in seven places with seven divergent behaviours, and the bug class #1919 fixed in restart_fleet (extending/releasing a lock you no longer own) is live elsewhere — worst in system_seed_service, whose _release_lock does an unconditional, tokenless r.delete(_PROVISION_LOCK_KEY) (src/backend/services/system_seed_service.py:476-482): it deletes a successor's lock, the severest form of the class. Mitigated today only by first-run-only usage + the existence backstop.

The seven sites

Site Shape
routers/ops.py SETNX+token, ownership-checked gate + CAD release (hardened by #1919 — shares redis_breaker_util.lock_token_matches)
services/agent_service/ephemeral.py:119-131,210-218 byte-identical to pre-#1919 ops.py, incl. the dead .encode() branch — first adoption candidate for the shared helper
services/skill_service.py:509-545 token + check-and-delete, TTL 1800
routers/sessions.py:180-186,309-383 async client, Lua CAS release, context manager — most mature
services/monitoring_service.py:957-1006 leader lease, own-lease-only refresh (#1464)
services/operator_queue_service.py:278-306 verbatim copy of monitoring (#1632)
services/system_seed_service.py:463-482 no token at all; unconditional delete on release

Proposal

  1. Fix system_seed_service._release_lock to a token + compare-and-delete release (smallest slice, ships alone if needed).
  2. Consolidate onto an ownership-checked SingleFlightLock primitive in redis_breaker_util (acquire / refresh_if_owned / release_if_owned; fail-open preserved; bug: fleet-restart lock — lease refresh isn't ownership-checked, and acquisition sits outside the try/finally (#1912 review follow-up) #1919's lock_token_matches is the first shared piece). Adopt in ops.py + ephemeral.py first; leader-lease sites (monitoring/operator_queue) may stay on their own-lease idiom or adopt a leader variant.
  3. Do NOT regress the deliberate per-site properties: fail-open on Redis down, sessions' Lua CAS, leader-lease semantics.

Context

Found by the #1919 strategy review (#1919); the 6-month-regret scenario named there is someone hitting copy #8 of this bug and writing private helper #2. Related precedent: #1464 (own-lease refresh), #1560 (single enumeration point pattern).

Acceptance Criteria

Activity

  1. added
    priority-p3
    type-bug
    theme-reliability
    complexity-medium
    status-ready
    on Jul 31, 2026
  2. added 2 commits that reference this issue on Aug 17, 2026
  3. added
    status-in-dev
    on Aug 17, 2026
  4. added a commit that references this issue on Aug 17, 2026
  5. removed
    status-in-dev
    status-ready
    on Aug 17, 2026
  6. added a commit that references this issue on Aug 21, 2026
  7. 3 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-mediumpriority-p3theme-reliabilitytype-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions