Repository navigation
fix(heartbeat): isolated heartbeat runs leak bundle MCP child processes until Gateway restart - #143433
Conversation
Isolated heartbeats mint a fresh session ID per run, so the bundle MCP runtime created for that run is never reused, but nothing retired it. Each heartbeat left its stdio MCP child processes alive until a gateway restart or an opt-in idle TTL. Carry the existing one-shot cleanupBundleMcpOnRunEnd flag through the reply pipeline so isolated heartbeat runs retire their exact runtime at settlement, like isolated cron runs already do. Shared heartbeats keep their persistent runtime. Fixes openclaw#143381
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed September 15, 2026, 4:16 PM ET / 20:16 UTC (Revision 7). ClawSweeper reviewWhat this changesThe PR retires MCP server processes after isolated heartbeats through existing run cleanup, preserves shared-session reuse, and adds regression and Gateway process-lifetime coverage. Merge readiness✅ Ready for maintainer review The PR remains necessary: current main still lacks isolated-heartbeat retirement wiring. The earlier polling finding is resolved, retained process-lifetime proof remains applicable, and no blocking defect was found. Priority: P1 Review scores
Verification
How this fits togetherHeartbeats run scheduled agent turns through OpenClaw’s reply pipeline. Those turns acquire session-scoped MCP tool servers; run settlement must retire servers belonging to disposable sessions while retaining persistent sessions. flowchart TD
A[Heartbeat wake] --> B{Isolated session?}
B -->|Yes| C[Fresh session with cleanup enabled]
B -->|No| D[Persistent shared session]
C --> E[Embedded or CLI agent turn]
D --> E
E --> F[Run settlement]
F --> G[Retire isolated MCP runtime]
F --> H[Retain shared MCP runtime]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +7, Tests +604. Total +611 across 7 files. View PR surface stats
Review metrics
Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Technical reviewBest possible solution: Use the existing settlement lifecycle to retire disposable heartbeat runtimes while preserving persistent-session reuse and active leases. Do we have a high-confidence way to reproduce the issue? Yes: current source shows fresh heartbeat sessions bypassing opt-in settlement cleanup, and retained Gateway traces demonstrate accumulation versus retirement. This read-only refresh did not execute a new current-main reproduction. Is this the best way to solve the issue? Yes: forwarding the existing cleanup flag repairs the lifecycle owner without introducing another reaper or changing shared-session defaults. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 41d443f12318. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (6 earlier review cycles)
|
Opt-in qa-lab product proof: a built Gateway with one stdio MCP fixture server and a forced isolated heartbeat. Counts the fixture child before, during, and after each run so a post-run zero means retired rather than never spawned; the main build leaks one child per run, the fixed build returns to zero, and the shared-session mode keeps one reused child.
…served Review follow-up for the live proof: redact Bearer credentials before the generic key/value pass so a token cannot survive the scheme word, and gate each mock model reply on the test having observed the fixture child so the peak sample cannot race a fast run.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: aa1bc7daef
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
Revision 5 (
|
|
@clawsweeper re-review Native merge verification requires a completed review less than 12 hours old; the existing review has expired. The PR head is unchanged at |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
|
@clawsweeper re-review Please refresh the completed review for the unchanged head b5e33ef. The retained isolated/shared heartbeat behavior proof still applies; this request satisfies the native review-completion age gate, not a request to repeat runtime experiments or perform repair. |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
|
Merged via squash.
|
…es until Gateway restart (openclaw#143433) * fix(heartbeat): retire isolated heartbeat bundle MCP runtimes on run end Isolated heartbeats mint a fresh session ID per run, so the bundle MCP runtime created for that run is never reused, but nothing retired it. Each heartbeat left its stdio MCP child processes alive until a gateway restart or an opt-in idle TTL. Carry the existing one-shot cleanupBundleMcpOnRunEnd flag through the reply pipeline so isolated heartbeat runs retire their exact runtime at settlement, like isolated cron runs already do. Shared heartbeats keep their persistent runtime. Fixes openclaw#143381 * test(e2e): prove isolated heartbeat MCP retirement on a built Gateway Opt-in qa-lab product proof: a built Gateway with one stdio MCP fixture server and a forced isolated heartbeat. Counts the fixture child before, during, and after each run so a post-run zero means retired rather than never spawned; the main build leaks one child per run, the fixed build returns to zero, and the shared-session mode keeps one reused child. * test(e2e): hold the fixture reply until the heartbeat MCP child is observed Review follow-up for the live proof: redact Bearer credentials before the generic key/value pass so a token cannot survive the scheme word, and gate each mock model reply on the test having observed the fixture child so the peak sample cannot race a fast run. * test(e2e): redact bearer credentials case-insensitively in the heartbeat proof * test(e2e): poll for heartbeat MCP child retirement instead of a fixed settle delay
Closes #143381
What Problem This Solves
Fixes an issue where operators running an agent heartbeat with
isolatedSession: trueand stdio MCP servers would accumulate one full set of MCP child processes per heartbeat run. The processes stayed resident until a Gateway restart, growing to hundreds of idle children and many gigabytes of RSS over a few hours, unless the operator discovered the opt-inmcp.sessionIdleTtlMsreaper.Why This Change Was Made
Isolated heartbeats mint a fresh session ID for every run, so the bundle MCP runtime built for that run is never reused, yet nothing retired it. Isolated cron runs already retire their runtime through the existing one-shot
cleanupBundleMcpOnRunEndsettlement step; this change carries that same flag from the heartbeat runner through the reply pipeline (embedded and CLI candidates) so isolated heartbeat runs retire their exact runtime at settlement. Shared (non-isolated) heartbeats are unchanged and keep the persistent main-session runtime. Active leases are preserved through the existing deferred-retirement path.User Impact
Heartbeat runs with
isolatedSession: trueno longer leak stdio MCP child processes. Process count returns to baseline after each run without configuring an idle TTL or restarting the Gateway.Evidence
Real Gateway proof (
test/e2e/qa-lab/runtime/heartbeat-mcp-runtime-retire.product-proof.e2e.test.ts, opt-in withOPENCLAW_HEARTBEAT_MCP_RETIRE_PROOF=1): a built Gateway child (dist/index.js,2026.9.3@f8a06baad7e) withagents.defaults.heartbeat = { isolatedSession: true, target: "none", lightContext: true }, a loopback mock Responses provider, and one stdio MCP server (mcp.servers.leakprobe, a tiny@modelcontextprotocol/sdkscript with a unique marker in its path). Three heartbeats are forced throughcron.run { mode: "force" }on the Gateway's heartbeat monitor job; each reachesstatus: "ok"(viacron.runs) and each run's model request advertises the probe tool, so the MCP server really was connected. MCP child processes are counted withpgrep -f <marker>before, during (peak), and after each run. The after-run and after-stop samples pollpgrepfor the variant's expected state (isolated fixed: 0 children; shared: the first run's PID;mainbaseline: one child per run) with a 30 s bound instead of sampling once after a fixed delay, since run settlement is not a process-closure barrier; the deadline sample is recorded as-is so the assertions still judge the real state. Re-run atb5e33ef9f14on theaa1bc7daefcbuild: isolated peaks 1/1/1 with 0 after every run, shared PID unchanged across all three runs, 0 after Gateway stop; retirement was observed within about 60 ms ofcron.runsreportingok.main, isolated (runner file at9601bb22f39)isolatedSession: false)On
mainthe three leaked children are the same three PIDs across every later stage (one new child per run, none reclaimed until the Gateway exits); with the fix each isolated run spawns one child and it is gone before the next run. The shared-session control on the fixed build keeps a single child with the same PID through all three runs and reaps it only at Gateway stop, so persistent-session MCP runtimes stay usable and are not retired by this change. The redacted Gateway log shows every isolated heartbeat dispatching under a freshsessionIdfor the sameagent:qa:main:heartbeatkey, which is why the per-run runtime can never be reused:Recorded counts (PIDs are the fixture children on this machine):
Reproduce on a built checkout:
OPENCLAW_HEARTBEAT_MCP_RETIRE_PROOF=1 OPENCLAW_E2E_SKIP_BUILD=1 OPENCLAW_HEARTBEAT_MCP_RETIRE_PROOF_SESSION=isolated node scripts/run-vitest.mjs run --config test/vitest/vitest.e2e.config.ts test/e2e/qa-lab/runtime/heartbeat-mcp-runtime-retire.product-proof.e2e.test.ts(set..._SESSION=sharedfor the control;..._VARIANT=mainonly labels a build made with the runner file atmain).Method notes: the heartbeat cadence is set to
24hso only the forced runs execute and every count maps to one known run; the fixture provider holds each model reply until the test has observed the fixture child, so a later zero means "retired", not "never spawned" and the sample cannot race a fast run;cron.runsconfirms each run'sstatus: "ok". The proof test is parameterized (..._VARIANT=fixed|mainlabels the build,..._SESSION=isolated|shared), writesproof-<label>.jsonwith per-stage PIDs and redacted Gateway log excerpts, and was run three times on this machine (Linux, Node 24.18) against dist builds of this branch and of this branch with the runner file atmain. Successful bundle-MCP retirement emits no log line, so the process counts plus the advertised probe tool are the evidence.Regression tests
src/infra/heartbeat-runner.model-override.test.ts("retires the bundle MCP runtime only for isolated heartbeat runs") fails onmainwithexpected undefined to deeply equal trueand passes with the fix; the shared-heartbeat half proves the flag stays unset for persistent sessions.src/auto-reply/reply/agent-runner-execution-lifecycle.test.tscovers forwarding through both the embedded and CLI run candidates.Checks:
node scripts/check-changed.mjspassed on the fix (lint, formatting, core typecheck, core test typecheck shards, boundary and guard scripts);autoreview(Codex, P2, branch vsorigin/main) scoped-clean on the fix. Two further autoreview rounds on the proof test found three P2s (Bearer redaction order, a peak-sample race against a fast run, and case-sensitive Bearer matching); all three are fixed and the three variants were rerun with the corrected test before the counts above were recorded. The proof test typechecks with zero diagnostics.