Skip to content

fix(agents): Gateway livelocks at 100% CPU when an unpinned agent starts a run on a shared default model - #153389

Closed
stackingrockss wants to merge 1 commit into
openclaw:mainfrom
stackingrockss:fix/153313-prepared-runtime-lease-livelock
Closed

stackingrockss wants to merge 1 commit into
openclaw:mainfrom
stackingrockss:fix/153313-prepared-runtime-lease-livelock

Conversation

@stackingrockss

Copy link
Copy Markdown
Contributor

Closes #153313

What Problem This Solves

Fixes: the Gateway main thread spins at 100% CPU and stops answering /healthz, WebSocket clients, and the Docker healthcheck when an agent that inherits agents.defaults.model without a runtime pin starts a run, while the system agent pins a runtime for that same model. On 2026.9.5 this turns into a boot loop, because restart recovery resumes the interrupted session and re-triggers it on every start.

User Impact

User impact: agents on a shared default model no longer hang the Gateway on their first turn; if the same owner mismatch ever happens for another reason, the run fails with a clear error instead of livelocking the process. No config change is needed, and the agentRuntime pin workaround from the issue remains valid but is no longer required.

Why This Change Was Made

normalizePreparedModelRuntimeInput resolved each runtime plugin selection under the selection's agent, then dropped that agent id from the stored selection. Re-normalizing the same input later ran the lookup without an agent scope, fell back to the legacy compatibility agent, and could return that agent's pinned runtime instead. The lease loop in acquirePreparedModelRuntimeLeaseFromOwners keys the owner off the first normalization while prepareSnapshot re-normalizes, so the two never agreed and the loop's final check continued forever on already-resolved awaits, starving the event loop.

Two changes:

  • Resolve the selection runtime under the input's agentId when the selection carries none, so normalization is idempotent and always uses the requesting agent's scope.
  • Make the lease loop fail when a settled, unchanged owner mismatches the prepared snapshot on two consecutive iterations. Nothing can change between such iterations, so retrying can only spin.

Evidence

  • src/agents/prepared-model-runtime.owner.normalize.test.ts: reproduces the defect on unfixed code (second normalization flips runtime: "auto" → "openclaw" and changes the owner key), passes with the fix.
  • src/agents/prepared-model-runtime-lease.livelock.test.ts: a fake context whose prepareSnapshot never matches the owner now rejects with PreparedModelRuntimeOwnerNotPublishedError after exactly two attempts; on the previous loop this test runs out of memory.
  • node scripts/run-vitest.mjs run src/agents/prepared-model-runtime: 25 files, 325 tests pass.
  • node scripts/check-changed.mjs: core typecheck, core test typecheck, lint, boundary guards, and ratchets pass.
  • Live: on the affected 2026.9.5 Docker instance (5 agents, xai via the Grok OAuth proxy), the request that hung the Gateway within 10 s of a message to the unpinned agent was confirmed with --inspect: every sample was inside this loop, and Debugger.evaluateOnCallFrame showed the loop's input selection at runtime: "auto" while normalizePreparedModelRuntimeInput(input) returned "openclaw" (owner key changed, prepareSnapshot resolved the other owner's snapshot). Pinning the runtime in config, which makes both normalizations agree, stopped the hang; the same request then completed normally. Full captures are in the issue.

@clawsweeper

clawsweeper Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Sep 20, 2026
@clawsweeper clawsweeper Bot added P0 Emergency: data loss, security bypass, crash loop, or unusable core runtime. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Sep 20, 2026
@clawsweeper

clawsweeper Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed September 19, 2026, 11:33 PM ET / September 20, 2026, 03:33 UTC (Revision 2).

ClawSweeper review

What this changes

The PR preserves each agent’s runtime-selection scope and stops repeated snapshot mismatches from freezing the Gateway.

Regression provenance

Possible regression — suspected (failure trace). No predecessor PR is attributed.

Merge readiness

⛔ Blocked before merge - 2 items remain

Keep open: the defect remains on current main, and the same-author issue remains unresolved. No blocking patch defect was found, but the earlier request for after-fix runtime proof remains unmet.

Priority: P0
Reviewed head: 04e6bf8accaabce910fd58ec7f6c198a50fa64e5

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) The focused patch has useful regression coverage and no blocking code finding, but lacks after-fix runtime evidence for this branch.
Proof confidence 🦪 silver shellfish (2/6) Needs stronger real behavior proof before merge: The Docker inspector output establishes the original lease-loop failure, and runtime pinning demonstrates a workaround. Neither exercises this branch’s normalization and mismatch guard after the fix; the prior request for an unpinned Gateway turn with responsive health checks remains outstanding. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Needs proof Needs stronger real behavior proof before merge: The Docker inspector output establishes the original lease-loop failure, and runtime pinning demonstrates a workaround. Neither exercises this branch’s normalization and mismatch guard after the fix; the prior request for an unpinned Gateway turn with responsive health checks remains outstanding. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed 8 items Current main retains the defect: Normalization resolves selections before stripping their agentId. Lease acquisition subsequently calls prepareSnapshot and retries without a progress bound when the returned snapshot differs from its keyed owner.
Introduced repair: The verified merge-base delta resolves selections under the input agent and rejects a repeated mismatch only when the published owner, generation, and snapshot remain unchanged and no publication is pending. Existing cancellation and generation checks remain intact.
OpenClaw implementation boundary: Runtime policy falls back to the legacy compatibility agent only when no agent scope is supplied. This establishes the reported scope drift within OpenClaw; the changed behavior does not depend on the Codex runtime or protocol.
Findings None None.
Security None None.

How this fits together

Prepared model runtimes combine an agent’s configuration, model selection, and published runtime snapshot. Lease admission supplies that snapshot to an agent turn while coordinating runtime publication and replacement.

flowchart TD
  A[Agent turn] --> B[Agent configuration and model]
  B --> C[Normalize runtime selection]
  C --> D[Find prepared runtime owner]
  D --> E{Snapshot matches owner}
  E -->|Yes| F[Admit agent turn]
  E -->|Repeated settled mismatch| G[Return explicit error]
Loading

Before merge

  • Add real behavior proof - Needs stronger real behavior proof before merge: The Docker inspector output establishes the original lease-loop failure, and runtime pinning demonstrates a workaround. Neither exercises this branch’s normalization and mismatch guard after the fix; the prior request for an unpinned Gateway turn with responsive health checks remains outstanding. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
  • Complete next step (P2) - Add after-fix evidence from this branch without the runtime-pin workaround, showing turn completion and responsive Gateway health checks. Redacted terminal output or logs count; a terminal screenshot or recording is welcome. Remove credentials, IP addresses, private endpoints, and user data. Updating the PR body should trigger another review; otherwise ask a maintainer to comment @clawsweeper re-review.
Agent review details

Security

None.

PR surface

Source +38, Tests +133. Total +171 across 4 files.

View PR surface stats
Area Files Added Removed Net
Source 2 39 1 +38
Tests 2 133 0 +133
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 172 1 +171

Review metrics

None.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #153313
Summary: This PR and the overlapping implementation address the reported runtime-selection identity drift.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Technical review

Best possible solution:

Keep runtime identity stable from agent-scoped selection through lease admission, with bounded failure for settled mismatches and normal waits for genuine publication progress.

Do we have a high-confidence way to reproduce the issue?

Yes, from source: an unpinned agent sharing a model with a pinned compatibility agent can change runtime selection on repeated normalization, while lease acquisition retries the resulting snapshot mismatch. This read-only review did not execute that flow.

Is this the best way to solve the issue?

Yes, the patch repairs agent scope at the existing normalization owner and bounds the reported mismatch without adding configuration or changing persisted state; runtime validation remains necessary.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 0537b888cd7d.

Labels

Label justifications:

  • P0: The reported defect starves the Gateway event loop, disables health checks and requests, and can recur during restart recovery.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs stronger real behavior proof before merge: The Docker inspector output establishes the original lease-loop failure, and runtime pinning demonstrates a workaround. Neither exercises this branch’s normalization and mismatch guard after the fix; the prior request for an unpinned Gateway turn with responsive health checks remains outstanding. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Evidence

What I checked:

  • Current main retains the defect: Normalization resolves selections before stripping their agentId. Lease acquisition subsequently calls prepareSnapshot and retries without a progress bound when the returned snapshot differs from its keyed owner. (src/agents/prepared-model-runtime.owner.ts:241, 0537b888cd7d)
  • Introduced repair: The verified merge-base delta resolves selections under the input agent and rejects a repeated mismatch only when the published owner, generation, and snapshot remain unchanged and no publication is pending. Existing cancellation and generation checks remain intact. (src/agents/prepared-model-runtime-lease.ts:350, 04e6bf8accaa)
  • OpenClaw implementation boundary: Runtime policy falls back to the legacy compatibility agent only when no agent scope is supplied. This establishes the reported scope drift within OpenClaw; the changed behavior does not depend on the Codex runtime or protocol. (src/agents/model-runtime-policy.ts:246, 04e6bf8accaa)
  • Latest release still has scope-dropping normalization: The release API identifies v2026.9.5 as latest, and its source at the supplied release SHA retains the old normalization. No shipped fix was established. (src/agents/prepared-model-runtime.owner.ts:239, ec9c1a13db89)
  • Proof and review continuity: The complete captured body at source revision 7787ca8fcb280e849ecaa8711820ca66e90aa75bc7b36a0f564440522e506d10 reports focused tests, a Docker inspector trace, and successful config pinning. The live body matched those claims; neither demonstrates this branch after the fix without pinning. The prior review had no findings and requested that specific runtime evidence. The earlier reviewed Git object was unavailable, so no unchanged-code or late-finding attribution is made. (04e6bf8accaa)
  • Related implementation remains open: fix: prevent Gateway hangs for agents without a runtime pin #153386 explicitly addresses the same bug and remains open, unmerged, and mergeable. Its body reports an isolated real Gateway turn returning OK, 185 successful health probes, and 0.7% idle CPU using a synthetic provider endpoint. This is positive evidence for that candidate, not proof for the reviewed branch. (1369944c1b81)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Provide redacted evidence that this branch completes an unpinned agent turn while Gateway health checks remain responsive.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (1 earlier review cycle)
  • reviewed 2026-09-20T03:18:10.335Z sha 271fb9113266df7d181a5ec456aa6a2ac99cfdc7 :: needs real behavior proof before merge. :: none

normalizePreparedModelRuntimeInput resolved each runtime plugin selection under the
selection's agentId, then dropped that agentId from the stored selection. Every later
normalization of the same input therefore resolved without an agent scope, fell back to
the legacy compatibility agent, and could pick up that agent's model runtime pin. For an
agent inheriting a default model with no pin of its own the runtime flipped between
"auto" and the system agent's pinned runtime, which changed the owner key.

acquirePreparedModelRuntimeLeaseFromOwners keys the owner off the first normalization
but prepareSnapshot re-normalizes, so the prepared snapshot came from a different owner
and `published.snapshot !== snapshot` held on every iteration. Each await in that path
was already resolved, so the loop spun on the microtask queue and starved the Gateway
event loop: 100% CPU, no HTTP or WebSocket responses, healthcheck failure, SIGKILL only.

- Resolve the selection runtime under the input's agentId when the selection has none,
  so re-normalization is a no-op.
- Fail the lease acquisition when a settled, unchanged owner mismatches the prepared
  snapshot twice in a row, instead of retrying forever.

Closes openclaw#153313
@stackingrockss

Copy link
Copy Markdown
Contributor Author

Closing as superseded by #153386 (merged as 8720fbf). That change fixes the same #153313 root cause and includes after-fix proof of an unpinned Gateway turn with responsive health checks. This branch was not merged and has no distinct fix to carry forward.

— Mote, assisting @stackingrockss

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

Labels

agents Agent runtime and tooling P0 Emergency: data loss, security bypass, crash loop, or unusable core runtime. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

1 participant