Skip to content

fix(codex): session stops responding after Codex unloads a thread with background terminals - #166693

Merged
jalehman merged 3 commits into
mainfrom
oc-276/codex-background-lease-deadlock
Oct 7, 2026
Merged

jalehman merged 3 commits into
mainfrom
oc-276/codex-background-lease-deadlock

Conversation

@jalehman

@jalehman jalehman commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes: a Codex-backed session stops responding to every message after Codex unloads an earlier thread of that session that had started background terminals.

User Impact

User impact: a session no longer hangs indefinitely after a thread rotation or idle unload. If startup still cannot finish, the turn fails after a bounded wait (10 minutes with the default requestTimeoutMs) instead of waiting up to the 48-hour turn budget.

Why This Change Was Made

A live Gateway's main session hung for 13 hours. A heap snapshot of the stuck process showed this chain:

  1. Each confirmed background terminal holds a lease on its Codex app-server client until OpenClaw sees the matching item/completed. Codex unloads an idle thread with no subscribers by clearing the thread listener, shutting the thread down (which ends its background terminals), and then broadcasting thread/closed (codex-rs/app-server/src/request_processors/thread_lifecycle.rs, unload_thread_without_subscribers). The item/completed events are written to the rollout after the listener is gone, so the connection never receives them. CodexNativeProcessClient ignored thread/closed, and four leases leaked on one app-server.
  2. That app-server was later retired, but it cannot close while leases remain. The next turn's releaseCodexBoundLiveThread waited for the retired app-server to exit before handing the thread to a new client. The wait ignored cancellation and ran while holding the thread mutation queue and the binding lease, so every later turn for the session queued behind it.
  3. App-server startup used the whole turn timeout as its limit, so the hang on an interactive turn produced no error.

This PR makes three changes:

  • CodexNativeProcessClient treats thread/closed, thread/archived, and thread/deleted as the final receipt for every native command on that thread. This releases the client lease and live-thread protection.
  • releaseCodexBoundLiveThread races the retired-owner exit wait with the lifecycle abort signal. Cancellation never continues to a write. It only releases the queue and lease so a later attempt can retry.
  • resolveCodexStartupTimeoutMs caps startup at ten app-server request timeouts when that is shorter than the turn budget. Observed startups take seconds to a few minutes.

Evidence

  • Regression tests fail on the parent commit and pass with the fix:
    • native-process-authority.test.ts: a confirmed background command is settled by thread/closed. Before the fix the source authority was never released. pnpm test extensions/codex/src/app-server/native-process-authority.test.ts --maxWorkers=1: 13.6 s wall, 3 ms of test time.
    • shared-client.test.ts, "lets cancellation end a writer handoff to a retired owner that other leases keep alive": before the fix the handoff stayed pending after abort. 16.4 s wall when run alone (module load), 20 ms of test time.
    • attempt-timeouts.test.ts: startup bound for a 48-hour turn budget with a 60 s request timeout is 600 s. Under 1 ms of test time.
  • Suites passing locally: shared-client.test.ts, thread-lifecycle*.test.ts, native-process-authority.test.ts, run-attempt-native-process-authority.test.ts, attempt-timeouts.test.ts, attempt-startup*.test.ts, and run-attempt*.test.ts. One unrelated failure, run-attempt.fork-provenance.test.ts ("forks a sender-attributed turn using the exact submitted text without changing its display"), fails the same way on origin/main without these changes.
  • node scripts/check-changed.mjs passes: extension typecheck, extension test typecheck, lint, format, and ratchets.
  • Live diagnosis: in the hung Gateway, the retired app-server's shared-client entry showed activeLeases: 4 and closeWhenIdle: true. The four leases belonged to background exec commands whose processes no longer existed, and the Codex rollout recorded item_completed for each at the moment the thread unloaded. Restarting the Gateway cleared the hang.

🤖 Generated with Claude Code

jalehman and others added 3 commits October 7, 2026 11:15
Codex unloads an idle thread with no subscribers by clearing its listener
before shutdown, so background terminals end without an item/completed the
OpenClaw connection can observe. The admission kept its shared app-server
client lease and live-thread protection forever, which later prevented a
retired client from closing.

Treat thread/closed, thread/archived, and thread/deleted as the final
receipt for every native command on that thread.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
releaseCodexBoundLiveThread waits for a retired app-server client to exit
before a writer handoff. Any other lease can keep that client alive
indefinitely, and the wait ran under the thread mutation queue and binding
lease without observing cancellation. One pinned client wedged every later
turn for the session, even after the run was aborted or timed out.

Race the exit wait with the lifecycle signal. Cancellation still never
proceeds to a write; it only releases the queue and lease so the next attempt
can retry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Startup reused the whole-turn timeout, so an interactive turn (48h default)
could hang silently in app-server startup for hours. Cap startup at ten
app-server request timeouts (10 minutes by default) when that is shorter than
the turn budget; observed startups take seconds to a few minutes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clawsweeper

clawsweeper Bot commented Oct 7, 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.

@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 7, 2026
@clawsweeper

clawsweeper Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed October 7, 2026, 2:21 PM ET / 18:21 UTC.

ClawSweeper review

What this changes

The Codex plugin releases background-command leases when their thread closes, makes retired-client handoffs cancellable, and bounds startup independently of long turn budgets.

Merge readiness

✅ Ready for maintainer review

This focused repair remains necessary on current main. No concrete introduced correctness or security defect was found.

Priority: P1
Reviewed head: 9699df4b6d96154fdf64fcd85e310fdb4aaf86d3

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused owner-level repair with useful regression evidence and no identified blocking defect.
Proof confidence 🌊 off-meta tidepool Not applicable: Verified repository-admin authorship exempts this PR from the ordinary external-contributor proof gate. The native-custody and lifecycle changes have reported parent-failing mocked regressions and pre-fix Gateway diagnosis, but no observed after-fix live recovery; no stored-data contract changes.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: Verified repository-admin authorship exempts this PR from the ordinary external-contributor proof gate. The native-custody and lifecycle changes have reported parent-failing mocked regressions and pre-fix Gateway diagnosis, but no observed after-fix live recovery; no stored-data contract changes.
Evidence reviewed 9 items Pinned introduced change: Reviewed the complete merge-base-to-head patch. The original head is 9699df4; base-only changes were excluded from PR ownership.
Current-main necessity: Current main still resolves startup from the entire turn budget and lacks the introduced native-command thread receipts and cancellable retired-owner release. No merged fixing PR is established by the provided discussion or inspected history.
Authoritative dependency contract: The PR explicitly depends on Codex's unload_thread_without_subscribers behavior. Personally inspected the dependency: it removes listener state before shutdown and broadcasts thread/closed only after completed shutdown and matching-runtime removal. GitHub verified this dependency SHA belongs to openai/codex.
Findings None None.
Security None None.

How this fits together

OpenClaw’s Codex plugin sends agent turns to a Codex app-server and retains client leases while native background commands remain active. Thread lifecycle receipts and cancellation determine when those resources can be released so later session turns can proceed.

flowchart TD
  A[Session message] --> B[Codex plugin startup]
  B --> C[Thread ownership handoff]
  C --> D[Codex app-server]
  D --> E[Background command leases]
  D --> F[Thread lifecycle receipts]
  F --> E
  E --> G[Client release and later turns]
  B --> H[Bounded timeout or cancellation]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +49, Tests +73. Total +122 across 8 files.

View PR surface stats
Area Files Added Removed Net
Source 5 57 8 +49
Tests 3 73 0 +73
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 8 130 8 +122

Review metrics

Metric Value Why it matters
Default startup ceiling 48-hour turn budget → 10-minute startup limit A stalled startup becomes a bounded failure without shortening successful turn execution.
Production versus regression coverage production +49 net lines; tests/support +73 net lines Production growth is justified by receipt settlement, cancellation, and timeout budgeting, with focused regression coverage.

Technical review

Best possible solution:

Keep lease settlement and cancellable handoff in the existing Codex lifecycle owners, with startup bounded separately from turn execution.

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

Yes, source establishes the path: retain background-command leases, unload their thread, retire the client, then attempt a handoff that waits for its exit. The reported live diagnosis supports this mechanism; this review did not execute it.

Is this the best way to solve the issue?

Yes, the patch repairs the existing resource owners and reuses the shared abort helper while preserving concrete-process settlement and guarded writes.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 16095df1d750.

Labels

Label changes:

  • add P1: The reported live failure wedged a Codex-backed session for 13 hours and blocked every subsequent message.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: Verified repository-admin authorship exempts this PR from the ordinary external-contributor proof gate. The native-custody and lifecycle changes have reported parent-failing mocked regressions and pre-fix Gateway diagnosis, but no observed after-fix live recovery; no stored-data contract changes.

Label justifications:

  • P1: The reported live failure wedged a Codex-backed session for 13 hours and blocked every subsequent message.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: Verified repository-admin authorship exempts this PR from the ordinary external-contributor proof gate. The native-custody and lifecycle changes have reported parent-failing mocked regressions and pre-fix Gateway diagnosis, but no observed after-fix live recovery; no stored-data contract changes.

Evidence

What I checked:

  • Pinned introduced change: Reviewed the complete merge-base-to-head patch. The original head is 9699df4; base-only changes were excluded from PR ownership. (9699df4b6d96)
  • Current-main necessity: Current main still resolves startup from the entire turn budget and lacks the introduced native-command thread receipts and cancellable retired-owner release. No merged fixing PR is established by the provided discussion or inspected history. (extensions/codex/src/app-server/attempt-timeouts.ts:96, 16095df1d750)
  • Authoritative dependency contract: The PR explicitly depends on Codex's unload_thread_without_subscribers behavior. Personally inspected the dependency: it removes listener state before shutdown and broadcasts thread/closed only after completed shutdown and matching-runtime removal. GitHub verified this dependency SHA belongs to openai/codex. (codex-rs/app-server/src/request_processors/thread_lifecycle.rs:416, a956835d0207)
  • Dependency identity verification: The GitHub commit endpoint resolves the inspected sibling revision to openai/codex@a956835; ownership was not inferred from the checkout directory. (a956835d0207)
  • Custody and cancellation invariants: Thread receipts close command admission, while forgetSettled retains commands with concrete unsettled processes. The retired-owner lease is released before racing its exit against cancellation; rejection prevents the lifecycle caller from continuing. Existing client-runtime handling already invalidates exact local thread ownership for the same three receipt types. (extensions/codex/src/app-server/native-process-authority.ts:111, 9699df4b6d96)
  • Verified test-merge parentage: Raw recorded parents match the pinned base followed by the exact PR head. Its result contains the eight introduced Codex paths; unrelated endpoint drift was not treated as a regression. (2dc4874811d0)

Likely related people:

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

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.

@jalehman
jalehman merged commit 94fc081 into main Oct 7, 2026
111 of 112 checks passed
@jalehman
jalehman deleted the oc-276/codex-background-lease-deadlock branch October 7, 2026 18:42
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 8, 2026
…h background terminals (openclaw#166693)

* fix(codex): settle background commands when Codex closes their thread

Codex unloads an idle thread with no subscribers by clearing its listener
before shutdown, so background terminals end without an item/completed the
OpenClaw connection can observe. The admission kept its shared app-server
client lease and live-thread protection forever, which later prevented a
retired client from closing.

Treat thread/closed, thread/archived, and thread/deleted as the final
receipt for every native command on that thread.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(codex): let cancellation end a retired-owner exit wait

releaseCodexBoundLiveThread waits for a retired app-server client to exit
before a writer handoff. Any other lease can keep that client alive
indefinitely, and the wait ran under the thread mutation queue and binding
lease without observing cancellation. One pinned client wedged every later
turn for the session, even after the run was aborted or timed out.

Race the exit wait with the lifecycle signal. Cancellation still never
proceeds to a write; it only releases the queue and lease so the next attempt
can retry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(codex): bound app-server startup independently of the turn budget

Startup reused the whole-turn timeout, so an interactive turn (48h default)
could hang silently in app-server startup for hours. Cap startup at ten
app-server request timeouts (10 minutes by default) when that is shorter than
the turn budget; observed startups take seconds to a few minutes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
RomneyDa pushed a commit that referenced this pull request Oct 11, 2026
…h background terminals (#166693)

* fix(codex): settle background commands when Codex closes their thread

Codex unloads an idle thread with no subscribers by clearing its listener
before shutdown, so background terminals end without an item/completed the
OpenClaw connection can observe. The admission kept its shared app-server
client lease and live-thread protection forever, which later prevented a
retired client from closing.

Treat thread/closed, thread/archived, and thread/deleted as the final
receipt for every native command on that thread.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(codex): let cancellation end a retired-owner exit wait

releaseCodexBoundLiveThread waits for a retired app-server client to exit
before a writer handoff. Any other lease can keep that client alive
indefinitely, and the wait ran under the thread mutation queue and binding
lease without observing cancellation. One pinned client wedged every later
turn for the session, even after the run was aborted or timed out.

Race the exit wait with the lifecycle signal. Cancellation still never
proceeds to a write; it only releases the queue and lease so the next attempt
can retry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(codex): bound app-server startup independently of the turn budget

Startup reused the whole-turn timeout, so an interactive turn (48h default)
could hang silently in app-server startup for hours. Cap startup at ten
app-server request timeouts (10 minutes by default) when that is shorter than
the turn budget; observed startups take seconds to a few minutes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 94fc081)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extensions: codex P1 High-priority user-facing bug, regression, or broken workflow. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant