Skip to content

fix: prevent stalled transcript workers from blocking Gateway shutdown - #170493

Merged
steipete merged 4 commits into
mainfrom
fix/transcript-reconcile-pool-close-bounded
Oct 13, 2026
Merged

steipete merged 4 commits into
mainfrom
fix/transcript-reconcile-pool-close-bounded

Conversation

@steipete

Copy link
Copy Markdown
Contributor

Fixes #169777

What Problem This Solves

Fixes Gateway shutdown waiting indefinitely for transcript reconciliation even when the supervised drain reports no active turns.

User Impact

A stalled transcript worker receives a five-second shutdown grace, then cancellation and native termination. Shutdown continues after lease recovery, with a warning naming the worker; committed transcripts remain available after restart. No operator configuration or migration is required.

Why This Change Was Made

The pool previously joined reconciliation owners before requesting cancellation; agent-database revocation happened later in shutdown. A withheld planner acknowledgement could therefore consume the supervisor's entire stop budget. The pool now owns cancellation of both operations and tasks, including admission waits before dispatch. Once a task is dispatched, shutdown still joins its native exit, parent writes, and independent lease recovery before closing databases. The shutdown ordering is preserved.

Evidence

The isolated real-Gateway comparison used three agents, public session/chat RPCs, gateway.suspend.prepare, gateway.suspend.handoff, and SIGTERM. The forced case injected synthetic transcript entries through chat.inject, rewound through sessions.rewind, and withheld the existing planner acknowledgement through a worker preload. Both forced stops reported activeCount=0, no blockers, and no write custody.

Local real-Gateway run Transcript-worker close SIGTERM to exit
Main baseline, normal 0.2 ms 11.25 s
Main baseline, withheld acknowledgement Began but never completed within observation Still running at 15.03 s; explicitly stopped
Fix, normal 0.1 ms 1.92 s
Fix, withheld acknowledgement 5.742 s, including native exit and lease recovery 16.17 s, exit 0

The baseline was 40c523b2c67792c9fb93069b519133590e988d57; its reconcile-pool source is unchanged through the integrated main base f9d5447c5946433d2a3fe63d299de3f93bb19fa9 and the later main tip be49163a5b5fbdf984e0c209f790d5823131f221. The fixed head is 2ed0da0f10900793ab0ad7b935dd0df8f5a9f9c2. Total exit times include unrelated SQLite cleanup, so they are not attributed wholly to this change. The baseline observation was censored at 15 seconds; this does not claim a measured 325-second reproduction.

The fixed run logged the expired grace and named planner, observed its native exit, dispatched the independent release task, then completed the pool close. Recovery readback passed on the same state: all three original replies survived, and the rewound second turn stayed absent; the recovery Gateway then completed another supervised stop (exit 0, transcript close 0.1 ms).

  • node scripts/run-vitest.mjs src/config/sessions/session-transcript-reconcile-pool.test.ts --maxWorkers=1: seven native/lease cases passed in the full run. The queued-admission test initially asserted before Node delivered MessagePort close; after an event-loop yield, its focused run passed in 28 ms (224.93 s wrapper wall time including cold compilation). Fresh Testbox full module proof passed: 365 files passed / 1 opt-in benchmark skipped, 3,325 tests passed / 1 skipped, 1,088.85 s command wall time. All eight pool tests passed on that run; the held-admission case took 2 ms, native-exit case 5.001 s, and pool-timeout case 2.292 s.
  • Production-only negative control retained the withheld-acknowledgement regression and restored main's pool implementation: it failed because no worker termination occurred after five fake seconds (157.72 s wrapper wall time). A separate held-admission negative control failed because close remained pending (151.31 s wrapper wall time).
  • pnpm tsgo:core passed; pnpm tsgo:core:test --ci-graphs-json '["core-test-agents-sessions"]' passed (19.46 s command wall time). Targeted oxlint, oxfmt, and git diff --check passed. An initial test-type command supplied a shard name where the runner expected a group; the explicit graph command corrected that invocation.
  • Fresh Codex autoreview: scoped-clean, no actionable P0–P2 findings. It specifically checked queued cancellation, native settlement, and prevention of late dispatch.
  • Fresh Blacksmith Testbox tbx_01m4sbnx3g5z3ez9s0fyw1nfxj, run 38329014234: exact source/tree verified. Core types passed in 54.78 s and the session test graph in 55.45 s. The live-only run completed with exit 0 in 203.291 s.
Fresh Testbox real-Gateway run Transcript-worker close SIGTERM to exit
Main baseline, normal 0.1 ms 401 ms
Main baseline, withheld acknowledgement Began but did not finish Still running at 15.045 s; explicitly stopped
Fix, normal 0.1 ms 403 ms
Fix, withheld acknowledgement 5.264 s 5.610 s, exit 0
Fix, recovery readback 0.2 ms 301 ms, exit 0

Every final remote stop reported activeCount=0, blockers=[], writeCustody=[], and an armed handoff. The fixed fault log names the stalled task, observes worker 19 exiting, then a separate lease-release task on worker 21 before pool close. Recovery verified all three original replies and absence of the rewound turn.

The first remote live attempt requested suspension while startup:subagent-recovery was still active and timed out before arming the fault. The final harness waits for the existing startup-recovery completion trace before draining; no production change or guard bypass was involved. An earlier lease expired during local source staging, and an initial source check incorrectly compared the transport-carrier commit with the source commit; subsequent runs verified the full source tree and file hashes using the wrapper's documented source identity.

Two earlier local native-exit attempts timed out at 30 seconds; the later traced run passed (21.829 s), showing native exit and independent lease recovery. The earlier failures remain recorded; a passing replay is not treated as a separate fix.

The production LOC exception is +39 (74 added / 35 removed), tests +62 (66 / 4), docs +7 (7 / 0). The added state keeps cancellation separate from native settlement; collapsing those owners would weaken the lease guarantee. No changelog or public protocol/SDK/schema changes.

Give accepted transcript reconciliation a five-second close grace, then cancel publication and native tasks through their existing owners. Keep handler settlement and independent lease recovery ahead of agent database retirement. Covers a withheld final acknowledgement without losing committed transcript bytes.
Abort queued admission before worker dispatch while preserving native task settlement and independent lease recovery. Forward cancellation through first-creation admission and cover delayed dispatch with a deterministic regression.
@clawsweeper

clawsweeper Bot commented Oct 13, 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 docs Improvements or additions to documentation gateway Gateway runtime size: S labels Oct 13, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 13, 2026
@clawsweeper

clawsweeper Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR cancels stalled transcript reconciliation after a five-second shutdown grace while retaining native-exit and database-lease cleanup barriers.

Example: A three-agent Gateway receives SIGTERM while a transcript planner acknowledgement is withheld.

  • Before: The baseline remained running after 15 seconds with transcript-worker close unfinished.
  • After: The supplied fixed run exited successfully after 5.610 seconds, then recovered all three original replies without restoring the rewound turn.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) Focused lifecycle repair with shipped Gateway fault and recovery evidence, meaningful negative controls, preserved settlement contracts, and no actionable findings.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The supplied real-Gateway results exercise public session/chat RPCs, supervised handoff, SIGTERM, a withheld planner acknowledgement, native termination, independent lease recovery, and successful transcript readback on the reviewed head.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Gateway stops can remain pending until the supervisor deadline despite having no active turns.
Reason: The reported release failure and reproduced stalled-planner path justify bounded cancellation without changing configuration, stored formats, or normal drain behavior.

Merge readiness

✅ Ready for maintainer review

Keep open: this PR fixes a shutdown stall still present on current main, preserves native and lease settlement, and has no actionable review findings; exact-head CI remains in progress.

Priority: P2
Reviewed head: 2ed0da0f10900793ab0ad7b935dd0df8f5a9f9c2

Before merge

None.

Findings

None.

Agent review details

How this fits together

Transcript reconciliation rebuilds derived indexes from stored session history; Gateway shutdown closes its shared worker pool before retiring agent databases.

flowchart TD
A[Gateway shutdown] --> B[Reconcile pool]
B --> C[Five second grace]
C --> D[Cancel operations and tasks]
D --> E[Join native exit and accepted writes]
E --> F[Recover database leases]
F --> G[Close agent databases]
Loading

Technical review

Best possible solution:

Land lifecycle-owned shutdown cancellation while preserving accepted writes, native exit, independent lease recovery, and restart rediscovery of unfinished indexes.

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

Current source establishes the unbounded wait and later database revocation, while the supplied Gateway comparison reproduces the withheld-acknowledgement stall against matching pool code.

Is this the best way to solve the issue?

The fix belongs in the existing reconciliation owner and correctly separates cancellation before dispatch from native and lease settlement after dispatch.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 8e0d08ad52a0.

Provenance checked

  • Shared reconciliation pool shutdown changes intended behavior with a stated reason (349aef7: Share bounded workers across agents while draining accepted transcript writes and exact lease recovery.)
  • Agent-resource settlement and independent lease recovery keeps the original intent (2fe6908: Prevent update cleanup from closing databases while admitted transcript work still owns leases.)
  • Cold admission and metadata coordination keeps the original intent (bb7d32e: Reuse physical-database admission through existing worker transports while retaining current authority checks.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 52df080 adds src/config/sessions/session-transcript-reconcile-pool.ts:271 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 52df080415e3; files: src/config/sessions/session-transcript-reconcile-pool.ts)

PR surface

Source +39, Tests +62, Docs +7. Total +108 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 74 35 +39
Tests 1 66 4 +62
Docs 1 7 0 +7
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 147 39 +108

Review metrics

Metric Value Why it matters
Production versus test LOC Production +39 net; tests +62 net. The production growth tracks cancellation separately from settlement, and the tests cover timeout recovery and prevention of late dispatch.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: Intermittent shutdown hangs delay supervised maintenance and upgrades, with supervisor deadlines limiting incidents and no reported data loss.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 12, 2026, 9:33 PM ET / October 13, 2026, 01:33 UTC.

@steipete
steipete merged commit f7e3fef into main Oct 13, 2026
137 of 138 checks passed
@steipete
steipete deleted the fix/transcript-reconcile-pool-close-bounded branch October 13, 2026 01:43
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 13, 2026
openclaw#170493)

* fix: stop stalled transcript reconciliation during shutdown

Give accepted transcript reconciliation a five-second close grace, then cancel publication and native tasks through their existing owners. Keep handler settlement and independent lease recovery ahead of agent database retirement. Covers a withheld final acknowledgement without losing committed transcript bytes.

* fix: cancel transcript admission waits during shutdown

Abort queued admission before worker dispatch while preserving native task settlement and independent lease recovery. Forward cancellation through first-creation admission and cover delayed dispatch with a deterministic regression.
RomneyDa pushed a commit that referenced this pull request Oct 14, 2026
#170493)

* fix: stop stalled transcript reconciliation during shutdown

Give accepted transcript reconciliation a five-second close grace, then cancel publication and native tasks through their existing owners. Keep handler settlement and independent lease recovery ahead of agent database retirement. Covers a withheld final acknowledgement without losing committed transcript bytes.

* fix: cancel transcript admission waits during shutdown

Abort queued admission before worker dispatch while preserving native task settlement and independent lease recovery. Forward cancellation through first-creation admission and cover delayed dispatch with a deterministic regression.

Backport note: the release line still uses the ReconcileAdmission permit queue instead of main's SQLite database admission turn, so the grace timer cancels queued work by aborting operation controllers (the existing permit waiters already cancel on that signal) rather than racing runWithSqliteDatabaseAdmissionTurn. Admission permits, generation bumps, and the waiter-inclusive pendingTasks snapshot are unchanged. The queued-admission regression holds every permit with stalled workers and proves the waiter is cancelled without dispatch.

(cherry picked from commit f7e3fef)
github-actions Bot pushed a commit to clawhaven/openclaw that referenced this pull request Oct 16, 2026
openclaw#170493)

* fix: stop stalled transcript reconciliation during shutdown

Give accepted transcript reconciliation a five-second close grace, then cancel publication and native tasks through their existing owners. Keep handler settlement and independent lease recovery ahead of agent database retirement. Covers a withheld final acknowledgement without losing committed transcript bytes.

* fix: cancel transcript admission waits during shutdown

Abort queued admission before worker dispatch while preserving native task settlement and independent lease recovery. Forward cancellation through first-creation admission and cover delayed dispatch with a deterministic regression.

Backport adaptation: release has no SQLite database admission turn, so only the shutdown grace deadline, task cancellation, and first-creation signal forwarding were ported, without the queued-admission wrapper and its test.

(cherry picked from commit f7e3fef)
wheakerd pushed a commit to wheakerd/clawdbot that referenced this pull request Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor 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.

[Bug]: Gateway shutdown hangs ~325 s in restart.close.transcript-workers with no active work (reconcile pool join is unbounded)

1 participant