Skip to content

refactor: per-agent start lock — complete the concurrent-start race hardening from #1809 #1817

Open

Description

@webmixgamer

Summary

#1809 shipped minimal recreate-race hardening: container_remove tolerates NotFound and a 409 name-conflict on the run adopts the winner's container. That converts the loser's 500 into a graceful success, but two concurrent starts can still both enter the recreate sequence (double stop/remove work, interleaved injections).

The full fix is a per-agent start lock serializing start_agent_internal, following the existing Redis SETNX+TTL precedent (agent:data_op:{name} in routers/agent_data.py).

Why deferred from #1809

New lock infrastructure on a hot path has its own failure modes (stuck lock blocks starts; TTL sizing; Redis-down behavior; wait-vs-409 contention semantics) — deliberately kept out of a P2 bug fix. See the #1809 plan's decision trail.

Design notes

Acceptance criteria

  • Concurrent starts of the same agent serialize; exactly one performs a needed recreate
  • Lock failure modes fail open (never blocks a start when Redis is down; no stuck-lock lockout beyond TTL)
  • Keyspace registered in agent_runtime_state.py with parity-test coverage

Refs #1809

Activity

  1. added
    priority-p3
    type-refactor
    theme-reliability
    complexity-medium
    status-ready
    on Jul 27, 2026
  2. webmixgamer commented on Jul 31, 2026

    @webmixgamer
    ContributorAuthor

    Heads-up from #1860 (PR #1912): lifecycle.restart_agent_internal(name, *, stop_timeout=30) now exists as the canonical stop→cold-start helper, consumed by restart_fleet. Its docstring names this issue as the future home of the per-agent start lock — when the lock lands there, the remaining inline copies of the stop→start_agent_internal shape can consolidate onto it in the same pass: routers/system_agent.py (restart/reinitialize), routers/systems.py (restart_system loop), and services/subscription_auto_switch.py (which additionally holds its own #799 process-local lock and uses stop timeout 10 — parameterized via stop_timeout).

  3. AndriiPasternak31 commented on Aug 11, 2026

    @AndriiPasternak31
    Contributor

    Field note from an OSS instance, in case it helps prioritise: the missing start lock has a
    side effect beyond the alarming log line.

    Observed on a cold boot: two uvicorn workers created a system-scoped MCP key 5 ms apart
    (10:41:48.059681Z and .064936Z), the loser logged
    ERROR … Failed to create system agent: 409 … name already in use plus
    WARNING: System agent deployment issue, and the winner logged System agent created and started
    137 ms later. The system agent was healthy throughout.

    Each race mints a duplicate MCP key, and nothing prunes superseded keys. That instance's
    trinity-system now holds 4 active agent-trinity-system-key rows (2026-06-08, 2026-07-09, and
    two 5 ms apart on 2026-08-08), and every one of them still authenticates. Instance-wide the key
    count went 52 → 57 (36 → 37 active) over three days, with duplicate active keys on 13 agents.

    So the race is not purely cosmetic: it accumulates valid credentials at one per worker per boot.
    Two things would help independently of the lock —

    1. make system-agent key creation idempotent (reuse the existing active key rather than minting), and
    2. deactivate superseded per-agent keys when a new one is minted.

    The human risk of the ERROR line is also real: an operator seeing it mid-upgrade will roll back a
    healthy deploy. Downgrading the loser's path to INFO/DEBUG once the lock lands would close that.

  4. added a commit that references this issue on Aug 17, 2026
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-p3status-readytheme-reliabilitytype-refactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions