Skip to content

fix(gateway): surface systemd start-limit exhaustion - #98291

Merged
vincentkoc merged 2 commits into
openclaw:mainfrom
masatohoshino:fix/gateway-systemd-start-limit-hit
Jul 1, 2026
Merged

vincentkoc merged 2 commits into
openclaw:mainfrom
masatohoshino:fix/gateway-systemd-start-limit-hit

Conversation

@masatohoshino

@masatohoshino masatohoshino commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

The managed gateway's systemd unit ships Restart=always with StartLimitBurst=5 /
StartLimitIntervalSec=60. When the gateway crashes faster than that allows, systemd latches the
unit into failed and stops auto-restarting it entirely. OpenClaw collapses that terminal state
into a generic status: "stopped", so openclaw gateway status and openclaw doctor only report
"Service is loaded but not running (likely exited immediately)." — there is no signal that systemd
has given up, and nothing ever clears the failed latch, so on systemd versions that refuse a start in
that state the gateway cannot be recovered with openclaw gateway restart.

Why This Change Was Made

A crash-looped, given-up service is a distinct, actionable state (the systemd analogue of a
Kubernetes CrashLoopBackOff), not an ordinary stop. Surfacing it and making the existing restart
command recover it turns a silent dead end into a one-command fix, using state systemd already exposes.

The change is two small parts:

  1. Detect the give-up. readSystemdServiceRuntime now also reads Result, NRestarts, and
    StartLimitBurst. Detection is state === "failed" && (Result === "start-limit-hit" || (lastExitStatus !== 78 && StartLimitBurst > 0 && NRestarts >= StartLimitBurst)). The counter path
    is the important one: once the gateway process has actually run and exited non-zero, systemd keeps
    Result = exit-code and never overwrites it with start-limit-hit, so Result alone misses real
    crash loops. The counter path excludes the deliberate no-restart exit: the unit pins
    RestartPreventExitStatus=78, so a last exit of 78 (the gateway's config-error / duplicate-lock
    EX_CONFIG code) means systemd stopped on purpose — a leftover NRestarts from earlier crashes is
    stale and must keep the ordinary stopped/config diagnostic instead of restart guidance. The explicit
    Result === "start-limit-hit" signal stays authoritative regardless of exit status. While a unit is
    still inside its restart cycle its state is activating, not failed, so the check only fires at
    the terminal give-up.
  2. Recover on restart. openclaw gateway restart now runs systemctl reset-failed <unit> before
    restart. It is idempotent (a no-op on a healthy unit) and clears the failed/start-limit latch so a
    crash-looped gateway comes back up.

Presentation changes are confined to the existing stopped branch of openclaw gateway status and
openclaw doctor; all other wording, health computation, the generated unit, and non-systemd backends
are untouched.

User Impact

  • A crash-looped gateway now reports an actionable line — "systemd stopped restarting the gateway
    after repeated crashes; run openclaw gateway restart or inspect logs."
    — instead of the
    misleading "exited immediately."
  • openclaw gateway restart reliably recovers a unit that systemd has stopped restarting.
  • No new config, env var, flag, or unit change. An ordinary single failure still shows the existing
    "likely exited immediately" hint.

Evidence

  • Unit tests (pnpm test): service-runtime.test.ts + doctor-format.test.ts (18, incl. the
    exit-78 no-restart guard on the counter path), systemd.test.ts (92, incl. parse fixtures, runtime
    classification, and reset-failed-before-restart ordering across user/sudo/machine-fallback/system
    scopes), status.print.test.ts (19). oxfmt --check clean; tsgo core + test-types green;
    pnpm build clean.

  • Patched-OpenClaw production-path proof on real systemd 249. The real patched functions were
    invoked (via tsx) against a throwaway systemd user unit openclaw-gateway-slproof.service,
    targeted through OPENCLAW_PROFILE=slproof (which namespaces the unit name, so the real managed
    gateway unit is never referenced or touched). It is labeled as a production-path invocation rather
    than a full openclaw gateway restart CLI run because the CLI restart path also waits on a gateway
    health probe that a throwaway /bin/false-style unit cannot satisfy. Paths below redacted (~).

    1. Throwaway unit driven past StartLimitBurst:
      $ systemctl --user show openclaw-gateway-slproof.service -p ActiveState -p SubState -p Result -p NRestarts -p StartLimitBurst
      ActiveState=failed
      SubState=failed
      Result=exit-code        # note: not start-limit-hit — systemd keeps the exec result
      NRestarts=2
      StartLimitBurst=2
      
    2. Patched readSystemdServiceRuntime reads the give-up state, and the predicate fires:
      { "status": "stopped", "state": "failed",
        "systemd": { "result": "exit-code", "nRestarts": 2, "startLimitBurst": 2 } }
      isSystemdStartLimitHit(runtime) => true
      
    3. Patched openclaw doctor render (buildGatewayRuntimeHints):
      systemd stopped restarting the gateway after repeated crashes.
      Recover with: openclaw --profile slproof gateway restart, then inspect logs if it keeps crashing.
      
    4. Patched openclaw gateway status render (printDaemonStatus):
      Runtime: stopped (state failed, sub failed, last exit 1, reason 1)
      systemd stopped restarting the gateway after repeated crashes; run openclaw gateway restart or inspect logs.
      
      Neither surface prints the old Service is loaded but not running (likely exited immediately). line.
    5. Patched restart action (restartSystemdService) — systemctl call ordering, captured via a PATH shim:
      systemctl --user status
      systemctl --user reset-failed openclaw-gateway-slproof.service
      systemctl --user restart openclaw-gateway-slproof.service
      
      reset-failed runs before restart.
    6. After the patched restart — failed latch cleared, unit recovered:
      ActiveState=active
      SubState=running
      Result=success
      NRestarts=0
      

    The throwaway unit was removed afterward (stop + reset-failed + unit-file delete + daemon-reload).

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime cli CLI command changes commands Command implementations size: M labels Jun 30, 2026
@clawsweeper

clawsweeper Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 1, 2026, 12:45 AM ET / 04:45 UTC.

Summary
The branch parses systemd Result/NRestarts/StartLimitBurst, adds a shared start-limit classifier, updates status and doctor hints, runs reset-failed before systemd restart, and adds regression coverage.

PR surface: Source +112, Tests +328. Total +440 across 8 files.

Reproducibility: yes. at source level: current main does not read Result/NRestarts/StartLimitBurst and both status and doctor always use the generic stopped-service hint. I did not run a live current-main crash-loop repro in this read-only checkout.

Review metrics: 1 noteworthy metric.

  • Systemd restart pre-step: 1 reset-failed pre-step added. Every managed Linux systemd gateway restart now clears failed/start-rate counters before issuing restart, which is relevant operator behavior for maintainers to notice.

Stored data model
Persistent data-model change detected: unknown-data-model-change: src/commands/doctor-format.test.ts. Confirm migration or upgrade compatibility proof before merge.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🦞 diamond lobster
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • none.

Next step before merge

  • No ClawSweeper repair job is needed because the current PR head has sufficient proof and no blocking findings; the remaining action is ordinary maintainer review.

Security
Cleared: The diff changes local systemd parsing/control, CLI/doctor messages, and tests only; it adds no dependencies, workflows, credential handling, permissions, or supply-chain surfaces.

Review details

Best possible solution:

Land this focused systemd runtime and restart recovery fix through normal maintainer merge gates, leaving broader crash-recovery policy outside this PR.

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

Yes, at source level: current main does not read Result/NRestarts/StartLimitBurst and both status and doctor always use the generic stopped-service hint. I did not run a live current-main crash-loop repro in this read-only checkout.

Is this the best way to solve the issue?

Yes. The fix stays in the systemd runtime/control owner boundary, shares one classifier between status and doctor, and clears the systemd failed latch in the existing restart action without adding config or a parallel recovery controller.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against d68ba5edc598.

Label changes

Label changes:

  • remove merge-risk: 🚨 availability: Current PR review selected no merge-risk labels.

Label justifications:

  • P2: This is a bounded Linux gateway availability and diagnostics bug fix with focused tests and limited blast radius.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR body includes terminal proof from a real throwaway systemd user unit showing the failed start-limit state, patched runtime and user-facing hints, reset-failed ordering, and recovered active state.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes terminal proof from a real throwaway systemd user unit showing the failed start-limit state, patched runtime and user-facing hints, reset-failed ordering, and recovered active state.
Evidence reviewed

PR surface:

Source +112, Tests +328. Total +440 across 8 files.

View PR surface stats
Area Files Added Removed Net
Source 4 116 4 +112
Tests 4 334 6 +328
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 8 450 10 +440

What I checked:

  • Repository policy read: Read the full root AGENTS.md; its review guidance affected the scope check, availability/config sensitivity pass, proof assessment, and no-CHANGELOG-review stance. (AGENTS.md:1, d68ba5edc598)
  • Current main runtime probe lacks start-limit fields: Current main asks systemctl show for Id, ActiveState, SubState, PID, exit, KillMode, TasksCurrent, and MemoryCurrent, but not Result, NRestarts, or StartLimitBurst, so it cannot classify the systemd start-limit latch. (src/daemon/systemd.ts:1267, d68ba5edc598)
  • Current main status and doctor use generic stopped hints: Current main prints the generic loaded-but-not-running hint for stopped services in both status output and doctor runtime hints, with no systemd start-limit branch. (src/cli/daemon-cli/status.print.ts:368, d68ba5edc598)
  • Generated unit contract supports the bug premise: The generated systemd unit uses StartLimitBurst=5, StartLimitIntervalSec=60, Restart=always, and RestartPreventExitStatus=78, making repeated-crash exhaustion and the exit-78 no-restart guard relevant. (src/daemon/systemd-unit.ts:73, d68ba5edc598)
  • PR head adds the shared classifier with exit-78 guard: PR head treats Result=start-limit-hit as authoritative, uses the NRestarts >= StartLimitBurst counter path for failed units, and avoids counter-based detection after lastExitStatus 78. (src/daemon/service-runtime.ts:121, 8fcd61bd333e)
  • PR head clears failed latch before restart: PR head runs systemctl reset-failed before system and user-scope restart calls, then preserves the existing restart call and error handling. (src/daemon/systemd.ts:1208, 8fcd61bd333e)

Likely related people:

  • ferminquant: Authored recent systemd gateway hygiene work touching service-runtime, systemd parsing, doctor-format, and tests that this PR extends. (role: recent area contributor; confidence: high; commits: 0a085bf15e64; files: src/daemon/service-runtime.ts, src/daemon/systemd.ts, src/commands/doctor-format.ts)
  • yetval: Authored the system-scope systemd gateway detection and restart/stop routing work on the same service action path. (role: systemd lifecycle contributor; confidence: high; commits: b988e2f92b0c; files: src/daemon/systemd.ts, src/daemon/systemd.test.ts, src/cli/daemon-cli/lifecycle.ts)
  • neo1027144-creator: Authored the config-validation restart-storm fix that introduced the generated unit's RestartPreventExitStatus=78 behavior guarded by this PR. (role: adjacent exit-status contract author; confidence: medium; commits: 2cf9ed782d9a; files: src/daemon/systemd-unit.ts, src/daemon/systemd-unit.test.ts, src/cli/gateway-cli/run.ts)
  • vincentkoc: Recent history shows WSL/systemd status hint and sudo-scope systemd work adjacent to the changed status and systemd runtime paths. (role: adjacent status and systemd contributor; confidence: medium; commits: 64785823d093, eb07ba511749, af3acf062634; files: src/daemon/systemd.ts, src/commands/doctor-format.ts, src/cli/daemon-cli/status.print.ts)
  • steipete: Earlier history shows gateway diagnostics, daemon service hints, and systemd helper documentation across the affected surfaces. (role: earlier diagnostics and lifecycle contributor; confidence: medium; commits: 61f5ed8bb755, 2761e8cc3b1a, b955ba168829; files: src/daemon/systemd.ts, src/daemon/service-runtime.ts, src/commands/doctor-format.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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.

How this review workflow works
  • 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.

@clawsweeper clawsweeper Bot added 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. P2 Normal backlog priority with limited blast radius. 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. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. 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 Jun 30, 2026
@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. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Jul 1, 2026
@vincentkoc
vincentkoc merged commit 76f9b6d into openclaw:main Jul 1, 2026
121 of 125 checks passed
@masatohoshino
masatohoshino deleted the fix/gateway-systemd-start-limit-hit branch July 1, 2026 14:44
chenyangjun-xy pushed a commit to chenyangjun-xy/openclaw that referenced this pull request Jul 1, 2026
* fix(gateway): surface systemd start-limit exhaustion

* fix(gateway): avoid start-limit false positive for exit 78
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 2, 2026
* fix(gateway): surface systemd start-limit exhaustion

* fix(gateway): avoid start-limit false positive for exit 78
Rorqualx pushed a commit to Rorqualx/cortex that referenced this pull request Jul 8, 2026
* fix(gateway): surface systemd start-limit exhaustion

* fix(gateway): avoid start-limit false positive for exit 78

(cherry picked from commit 76f9b6d)
Rorqualx pushed a commit to Rorqualx/cortex that referenced this pull request Jul 15, 2026
* fix(gateway): surface systemd start-limit exhaustion

* fix(gateway): avoid start-limit false positive for exit 78

(cherry picked from commit 76f9b6d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes commands Command implementations gateway Gateway runtime P2 Normal backlog priority with limited blast radius. 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.

2 participants