Skip to content

fix(macos): app restarts a slow-starting gateway in a loop after reboot - #158954

Merged
RomneyDa merged 3 commits into
openclaw:mainfrom
MertBasar0:fix/158936-macos-readiness-cold-start
Oct 9, 2026
Merged

RomneyDa merged 3 commits into
openclaw:mainfrom
MertBasar0:fix/158936-macos-readiness-cold-start

Conversation

@MertBasar0

@MertBasar0 MertBasar0 commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Closes #158936

What Problem This Solves

Fixes: OpenClaw.app kills and reinstalls a gateway that is still legitimately starting whenever a cold start (for example after a reboot) takes longer than the app's 6-second readiness window, looping until the app is quit.

User Impact

User impact: a slow-starting gateway that launchd started on its own is left to finish starting (up to 300s, while the same PID keeps owning the port) instead of being SIGTERMed and reinstalled every cycle. A gateway that is still unready after that budget is still repaired.

Tradeoff: an unchanged launchd PID that owns the port but has a wedged health RPC is now repaired after up to 300s instead of right after the first window. A PID that exits, is replaced, or loses the port still fails at the next 6s deadline, so the longer wait only applies to a process that is still alive and still holding the port.

Why This Change Was Made

The readiness deadline could only be extended past the first window with fresh-install evidence (or a responsive "still starting" reply). A PID launchd starts at login, or one started by a repair, never has install evidence, so the first window timed out, recorded that PID as failed, and the next enable request force-reinstalled it. The new process hit the same path, which produced the loop in the report.

The extension authorization now also accepts an unchanged launchd PID that still owns the port (or while nothing is listening yet), bounded by a new reusedLaunchdColdStartTolerance (300s). Fresh installs keep startupMigrationTolerance (120s). The reused budget started out shared with the fresh-install one; the reboot trace below measured a ~139s cold start that ran past 120s, so it now has its own, sized with about 2x headroom over that measurement for slower hardware. Unlike fresh-install grace, this grace is not standing: the PID is re-proven against launchd at every deadline, so a PID that exits or is replaced mid-wait stops extending at the next deadline. A PID that changed or lost the port gets no grace and keeps today's behavior.

Evidence

New GatewayProcessManagerTests cases, driving the launchd readiness path without install evidence:

  • reused launchd gateway gets the cold-start budget before repair: the first health probe stalls past the window and a later probe succeeds. On main this fails with Gateway did not start in time and arms repair. With this change it ends .running, records no failure, and the next enable request does not reinstall.
  • reused launchd gateway outlasting the first-run budget is not repaired: every health probe hangs; the test releases them only after the sixth launchd inspection, which is past the fresh-install budget passed to the hook (0.2s) but inside the reused budget (5s). It ends .running with no repair evidence and no reinstall. With a shared budget (the previous head), the attempt fails before that point, which is the case the reboot trace hit.
  • reused launchd gateway still repairs after its bounded budget: a never-ready reused PID still fails after the budget, and the next enable request still installs once.
  • changed launchd PID does not inherit the cold-start budget: launchd now reports a different PID and nothing owns the port, so there is no extension and no repair evidence.
  • reused launchd PID replaced during its grace loses the grace: 4242 earns the first extension, then launchd reports 4243; the next deadline re-checks and stops (3 launchd inspections instead of probing 4242 until the budget).

The cold-start case releases the first health response only after the first launchd inspection, which happens only once the first window has timed out, so it cannot pass without exercising an extension.

Existing fresh-install, repair, and deadline tests are unchanged.

macos-swift (tests) on the first head (5f722d3): Test run with 2294 tests in 263 suites passed. The follow-up commit also removes the two unused setProjectRoot/projectRootPath wrappers that Scan macOS dead code flags on this file.

Real behavior proof (macOS reboot, reporter's host)

The reporter of #158936 (@cjn119-ui) built this branch at f03c992f1ce on their Mac, ran it as the Local-mode app against the same LaunchAgent, and rebooted (comment). The cold start was slow again (~2m19s, slow SQLite validation, same as the original report), and the gateway was left to finish.

gateway.log: one continuous run, a single PID, no SIGTERM in between:

2026-09-27T17:29:11.795-04:00 [gateway] loading configuration…
2026-09-27T17:30:13.484-04:00 [gateway] http server listening (21 plugins; 60.0s)
2026-09-27T17:31:30.439-04:00 [gateway] ready

App unified log (log show --last 15m --predicate 'process == "OpenClaw"') over the same window:

17:28:52 gateway readiness failed reason=Gateway service inspection failed
17:30:13 gateway watchdog reconnect failed gateway starting; retry shortly
17:31:22 gateway readiness failed reason=Gateway did not start in time
17:31:31 gateway readiness failed reason=Gateway listener found on port 18789 (pid 1005 node ...) but health check failed: Gateway health probe timed out after 1025ms

There are no repairing or enabling launchd lines anywhere in that window. The app kept probing the same reused PID instead of killing and reinstalling it, which is what it did on every cycle before this change.

That trace ran on f03c992f1ce, where the reused-PID path still shared the 120s budget: the 17:31:22 … did not start in time line shows it ran out about 8s before ready. The gateway got there before the next enable request acted on that failure, but a slower start could have been reinstalled. 3d7e30e8ba7 addresses exactly that by giving this path its own 300s budget (see above); the trace otherwise shows the intended behavior (one PID, no repair).

The tests still run only in the macos-swift CI lane (I don't have a macOS host); the runtime proof above is the reporter's, on the previous head.

A launchd PID the app did not install (started at login after a reboot, or
by a repair) had no fresh-install evidence, so its readiness never extended
past the first 6s window. The timeout armed a forced LaunchAgent repair, and
the reinstall SIGTERMed the gateway mid-startup; every slow cold start hit
the same path, looping until the app was quit.

Let an unchanged launchd PID that still owns the port use the same bounded
startupMigrationTolerance budget as a fresh install. A PID that changes or
loses the port keeps today's behavior, and a PID still unready after the
budget is still repaired.

Fixes openclaw#158936
@clawsweeper

clawsweeper Bot commented Sep 26, 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 P0 Emergency: data loss, security bypass, crash loop, or unusable core runtime. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Sep 26, 2026
@clawsweeper

clawsweeper Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge. Reviewed September 28, 2026, 6:57 PM ET / 22:57 UTC (Revision 4).

ClawSweeper review

What this changes

The macOS app gives an unchanged launchd Gateway process up to 300 seconds to become ready after a cold start, with tests for recovery, timeout, and PID replacement.

Regression provenance

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

Merge readiness

⛔ Blocked before merge - 3 items remain

This PR remains necessary: current main still denies extended readiness to a reused launchd Gateway. The latest commit addresses the prior 120-second finding. The remaining choice is whether delaying repair of a same-PID but wedged Gateway for up to 300 seconds is acceptable.

Priority: P0
Reviewed head: 3d7e30e8ba73fd683060b429248b226833526c07
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) Real reboot evidence and focused lifecycle tests support the repair, with the longer worst-case recovery delay left for owner acceptance.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (logs): The reporter’s real macOS reboot on the preceding head exercised the app and LaunchAgent and showed one Gateway process reach ready after about 139 seconds without reinstall. That trace exposed the old 120-second limit; the latest head raises the reused-PID limit to 300 seconds, with added boundary tests, but has no separate reboot trace at that exact head. No stored-data contract changes.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (logs): The reporter’s real macOS reboot on the preceding head exercised the app and LaunchAgent and showed one Gateway process reach ready after about 139 seconds without reinstall. That trace exposed the old 120-second limit; the latest head raises the reused-PID limit to 300 seconds, with added boundary tests, but has no separate reboot trace at that exact head. No stored-data contract changes.
Evidence reviewed 6 items Current main still has the reported path: Current main requires fresh-install generation evidence for a launchd readiness extension; the reused-process budget is absent.
Latest-head readiness behavior: The introduced path selects a 300-second budget for a reused PID and rechecks launchd ownership at each extended deadline; a changed PID loses that grace.
Bounded-repair tradeoff: A stable launchd PID may keep the port while its health RPC is wedged; the new budget can defer the existing failure-and-repair path for up to 300 seconds.
Findings None None.
Security None None.

How this fits together

The macOS app checks the health of a Gateway started by launchd. Its readiness result determines whether the app attaches to that process or records a failure that can lead to LaunchAgent repair.

flowchart LR
A[App starts or reconnects] --> B[Inspect launchd Gateway]
B --> C[Probe Gateway health]
C --> D{Ready?}
D -->|Yes| E[Use running Gateway]
D -->|No| F{Same launchd PID within budget?}
F -->|Yes| C
F -->|No| G[Record failure for repair]
Loading

Decision needed

Question Recommendation
Is a 300-second maximum delay before repairing a same-PID Gateway with a wedged health RPC acceptable to prevent the measured slow-reboot restart loop? Accept the measured bound: Keep 300 seconds for a stable launchd PID and retain the existing bounded repair path after expiry.

Why: The real reboot took about 139 seconds, while the new 300-second bound also changes recovery time for an unhealthy process; choosing that operator tradeoff requires macOS lifecycle ownership.

Before merge

  • Resolve merge risk (P1) - An unchanged launchd PID with a wedged health RPC may now delay automatic repair for up to 300 seconds. The reporter’s trace supports a longer cold-start allowance, but a maintainer has not accepted this recovery-delay tradeoff.
  • Complete next step (P2) - Have the macOS Gateway lifecycle owner decide whether the 300-second same-PID recovery delay is acceptable before merge.
  • Resolve maintainer decision - Resolve the maintainer decision shown above before merge.
Agent review details

Security

None.

PR surface

Other +251. Total +251 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 0 0 0 0
Tests 0 0 0 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 3 277 26 +251
Total 3 277 26 +251

Review metrics

Metric Value Why it matters
Production and test growth production +8 net lines, tests +243 lines The production change is narrow; the added tests cover the distinct cold-start, timeout, and PID-replacement outcomes.

Merge-risk options

Maintainer options:

  1. Accept the 300-second bound (recommended)
    Accept slower repair of a stable but unhealthy Gateway in exchange for allowing the measured reboot cold start to finish.
  2. Refine startup detection
    Hold the PR until a reliable progress signal limits grace for a wedged Gateway without reviving the restart loop.

Technical review

Best possible solution:

Keep bounded grace for the same launchd PID, recheck it throughout the wait, and surface a clear recovery state when the budget expires.

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

Yes, at source level: current main denies extended readiness to a reused launchd PID, and the linked issue supplies a matching reboot and SIGTERM trace. This read-only review did not run the native app.

Is this the best way to solve the issue?

Yes, the patch changes the macOS readiness owner and preserves a bounded repair path. The 300-second recovery tradeoff still needs owner acceptance.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 0ce096721a54.

Labels

Label changes:

No label changes.

Label justifications:

  • P0: The reported reboot loop repeatedly terminates the Gateway and leaves core use unavailable while the app is open.
  • merge-risk: 🚨 availability: The introduced 300-second grace can postpone repair when a stable Gateway process is alive but unhealthy.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (logs): The reporter’s real macOS reboot on the preceding head exercised the app and LaunchAgent and showed one Gateway process reach ready after about 139 seconds without reinstall. That trace exposed the old 120-second limit; the latest head raises the reused-PID limit to 300 seconds, with added boundary tests, but has no separate reboot trace at that exact head. No stored-data contract changes.
  • proof: sufficient: Contributor real behavior proof is sufficient. The reporter’s real macOS reboot on the preceding head exercised the app and LaunchAgent and showed one Gateway process reach ready after about 139 seconds without reinstall. That trace exposed the old 120-second limit; the latest head raises the reused-PID limit to 300 seconds, with added boundary tests, but has no separate reboot trace at that exact head. No stored-data contract changes.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Vincent Koc: 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.

  • Record the macOS lifecycle owner’s decision on the 300-second repair-delay tradeoff.

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 (3 earlier review cycles)
  • reviewed 2026-09-26T15:10:06.132Z sha 5f722d3 :: needs real behavior proof before merge. :: [P2] Recheck the reused PID after granting startup grace | [P2] Gate the regression test on the readiness deadline
  • reviewed 2026-09-26T21:02:04.852Z sha f03c992 :: needs real behavior proof before merge. :: none
  • reviewed 2026-09-28T20:58:12.090Z sha f03c992 :: blocked before merge. :: [P1] Cover cold starts beyond the reused PID's 120-second budget

Address review on openclaw#158954: a reused PID's startup grace no longer becomes
standing grace after the first extension, so a PID replaced during the wait
stops extending at the next deadline. Fresh installs and responsive
progress keep their existing standing grace.

Gate the cold-start regression test on the first launchd inspection instead
of a wall-clock sleep, add coverage for a PID replaced after its first
extension, and drop the unused setProjectRoot/projectRootPath wrappers the
dead-code scan flags on this file.
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Sep 26, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed 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 28, 2026
A real reboot on this branch took about 139s to reach ready, past the
120s first-run budget the reused-PID path shared. The timeout left repair
evidence for a gateway that was still starting. Reused launchd PIDs now
probe through their own 300s budget; the PID is still re-proven against
launchd at every deadline, so a replaced or dead PID loses it at once.
@clawsweeper clawsweeper Bot added 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. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Sep 28, 2026
@MertBasar0

Copy link
Copy Markdown
Contributor Author

@steipete since you've been in GatewayProcessManager.swift this week, could you take a look at this one when you have a moment? It's the P0 macOS restart loop from #158936 (the app SIGTERMs and reinstalls a launchd gateway that is still cold-starting after a reboot).

Where it stands:

The one open item is an owner call: a same-PID gateway whose health RPC is wedged now waits up to 300s before repair, instead of being repaired after the first 6s window. A PID that exits, gets replaced, or loses the port still fails at the next deadline, so the longer wait only covers a process that is alive and still holding the port. If you'd prefer a different bound or a progress-based signal instead, I'm happy to change it.

@RomneyDa
RomneyDa merged commit 418f22b into openclaw:main Oct 9, 2026
81 of 82 checks passed
steipete added a commit that referenced this pull request Oct 9, 2026
Complete the readiness authorization tuple introduced by 418f22b (#158954). App-owned children retain their existing standing grace, while every extension still checks the current generation and child PID. Reused launchd PIDs keep the new per-deadline proof requirement.
steipete added a commit that referenced this pull request Oct 9, 2026
The cold-start tests introduced in 418f22b (#158954) still referenced setTestingLastFailureReason and the bundlePath argument. Those were intentionally removed by 6cc8e7f (#163372) and 669db5d (#160710). Use the same direct property writes and port-only calls as the sibling tests, preserving all assertions.
RomneyDa pushed a commit that referenced this pull request Oct 9, 2026
…ot (#158954)

* fix(macos): give reused launchd gateways the cold-start readiness budget

A launchd PID the app did not install (started at login after a reboot, or
by a repair) had no fresh-install evidence, so its readiness never extended
past the first 6s window. The timeout armed a forced LaunchAgent repair, and
the reinstall SIGTERMed the gateway mid-startup; every slow cold start hit
the same path, looping until the app was quit.

Let an unchanged launchd PID that still owns the port use the same bounded
startupMigrationTolerance budget as a fresh install. A PID that changes or
loses the port keeps today's behavior, and a PID still unready after the
budget is still repaired.

Fixes #158936

* fix(macos): re-prove a reused launchd PID at every readiness extension

Address review on #158954: a reused PID's startup grace no longer becomes
standing grace after the first extension, so a PID replaced during the wait
stops extending at the next deadline. Fresh installs and responsive
progress keep their existing standing grace.

Gate the cold-start regression test on the first launchd inspection instead
of a wall-clock sleep, add coverage for a PID replaced after its first
extension, and drop the unused setProjectRoot/projectRootPath wrappers the
dead-code scan flags on this file.

* fix(macos): give reused launchd gateways a reboot-sized readiness budget

A real reboot on this branch took about 139s to reach ready, past the
120s first-run budget the reused-PID path shared. The timeout left repair
evidence for a gateway that was still starting. Reused launchd PIDs now
probe through their own 300s budget; the PID is still re-proven against
launchd at every deadline, so a replaced or dead PID loses it at once.

(cherry picked from commit 418f22b)

Backport adaptations: preserve release child readiness grace, use the
current port-only launch-agent API, and assign the writable failure field.

(cherry picked from commit a094425f4b96b6013cd0cb029df96123b5472c61)
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 10, 2026
…ot (openclaw#158954)

* fix(macos): give reused launchd gateways the cold-start readiness budget

A launchd PID the app did not install (started at login after a reboot, or
by a repair) had no fresh-install evidence, so its readiness never extended
past the first 6s window. The timeout armed a forced LaunchAgent repair, and
the reinstall SIGTERMed the gateway mid-startup; every slow cold start hit
the same path, looping until the app was quit.

Let an unchanged launchd PID that still owns the port use the same bounded
startupMigrationTolerance budget as a fresh install. A PID that changes or
loses the port keeps today's behavior, and a PID still unready after the
budget is still repaired.

Fixes openclaw#158936

* fix(macos): re-prove a reused launchd PID at every readiness extension

Address review on openclaw#158954: a reused PID's startup grace no longer becomes
standing grace after the first extension, so a PID replaced during the wait
stops extending at the next deadline. Fresh installs and responsive
progress keep their existing standing grace.

Gate the cold-start regression test on the first launchd inspection instead
of a wall-clock sleep, add coverage for a PID replaced after its first
extension, and drop the unused setProjectRoot/projectRootPath wrappers the
dead-code scan flags on this file.

* fix(macos): give reused launchd gateways a reboot-sized readiness budget

A real reboot on this branch took about 139s to reach ready, past the
120s first-run budget the reused-PID path shared. The timeout left repair
evidence for a gateway that was still starting. Reused launchd PIDs now
probe through their own 300s budget; the PID is still re-proven against
launchd at every deadline, so a replaced or dead PID loses it at once.
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 10, 2026
Complete the readiness authorization tuple introduced by bac884e (openclaw#158954). App-owned children retain their existing standing grace, while every extension still checks the current generation and child PID. Reused launchd PIDs keep the new per-deadline proof requirement.
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 10, 2026
The cold-start tests introduced in bac884e (openclaw#158954) still referenced setTestingLastFailureReason and the bundlePath argument. Those were intentionally removed by 9c6018f (openclaw#163372) and 4e79f38 (openclaw#160710). Use the same direct property writes and port-only calls as the sibling tests, preserving all assertions.
wheakerd pushed a commit to wheakerd/clawdbot that referenced this pull request Oct 22, 2026
Backport openclaw#158954 with branch-local readiness coverage.

Co-authored-by: RomneyDa <[email protected]>
wheakerd pushed a commit to wheakerd/clawdbot that referenced this pull request Oct 22, 2026
Backport the released follow-up to the openclaw#158954 readiness repair.

Co-authored-by: RomneyDa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: macos App: macos merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. P0 Emergency: data loss, security bypass, crash loop, or unusable core runtime. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M 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]: macOS app readiness watchdog SIGTERMs a slow-starting gateway, causing a restart loop until the app is quit

2 participants