Skip to content

fix(cli): avoid macOS system CA exit hang in one-shot commands - #110341

Merged
steipete merged 1 commit into
mainfrom
claude/fix-cli-gateway-hang
Jul 18, 2026
Merged

steipete merged 1 commit into
mainfrom
claude/fix-cli-gateway-hang

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

On macOS, every gateway-backed one-shot CLI command (cron list/status/get/show/runs, channels list, agent, sessions, doctor, even config validate) prints its complete output within ~1s and then never exits — the process idles toward the 600s command timeout. Any script, cron job, or CI step that runs the CLI and waits for exit hangs forever. Reproduced 6+ times across QA rounds, including on the canonical non-QA build.

Why This Change Was Made

Root cause (confirmed by decisive A/B experiment): OpenClaw defaults NODE_USE_SYSTEM_CA=1 on darwin (src/bootstrap/node-startup-env.ts:36). Node's macOS system-CA support loads the Keychain trust store on a worker thread at first TLS import and joins that worker during process shutdown; under load that join does not complete, keeping the (respawned) CLI child alive (ProcessWrap+PipeWrap observed via process.getActiveResourcesInfo()). config validate with default env = hang; identical command with NODE_USE_SYSTEM_CA=0 = instant exit.

User Impact

One-shot, non-interactive CLI invocations on macOS now respawn with --use-openssl-ca (file-backed CA store) and exit promptly after completing. The trust-store scope is deliberate: foreground gateway run skips the respawn wrapper entirely (shouldSkipRespawnForArgv) and the TUI/interactive commands are excluded (isTerminalInteractiveRespawnArgv), so long-lived processes that make provider TLS calls keep macOS Keychain trust (enterprise/corporate CAs). An explicit --use-system-ca (execArgv or NODE_OPTIONS) remains the opt-in for Keychain-only trust on one-shots, and NODE_EXTRA_CA_CERTS is unaffected. No respawn loop: the child env carries NODE_USE_SYSTEM_CA=0, so the branch cannot re-trigger.

Evidence

  • Live isolated proof (fresh gateway, this branch's build): cron list --json and config validate EXIT with code 0; control run with explicit --use-system-ca reproduces the pre-fix hang, proving the opt-in path preserves old behavior.
  • Tests: 22 respawn plan tests (src/entry.respawn.test.ts) + new process-level regression proving a gateway-backed CLI process exits cleanly (src/cli/gateway-backed-exit.process.test.ts). All pass.
  • Path-scoped check-changed: production/test tsgo, formatting, lint, guards green.
  • Autoreview (codex gpt-5.6-sol, xhigh): clean — "patch is correct (0.84), no actionable findings".
  • QA provenance: found by R4 whole-system stress campaign (3 independent agents + 2 manual reproductions); also explains QA finding "doctor --fix repairs reported but never persisted" (write step sits after the hang point) — to be re-verified on this fix.

@openclaw-barnacle openclaw-barnacle Bot added cli CLI command changes size: M maintainer Maintainer-authored PR labels Jul 18, 2026
@steipete steipete self-assigned this Jul 18, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6ec12f42a6

ℹ️ 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".

Comment thread src/entry.respawn.ts
// Node loads the macOS Keychain off-thread on the first TLS import, then joins
// that worker during shutdown. One-shot CLIs use the file-backed CA store instead;
// an explicit --use-system-ca remains the opt-in for Keychain-only trust.
childEnv.NODE_USE_SYSTEM_CA = "0";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve the documented system-CA env opt-in

When a macOS user has explicitly exported NODE_USE_SYSTEM_CA=1, this respawn path rewrites it to 0 unless they also know to pass --use-system-ca. That breaks the documented private-CA setup (docs/plugins/vault.md tells users to export NODE_USE_SYSTEM_CA=1, and the Vault integration forwards that env key), so one-shot commands that resolve Vault-backed SecretRefs against a server trusted only by the macOS trust store will stop trusting that CA and fail TLS before the command can run. Please distinguish OpenClaw’s own defaulted env from an operator-provided env opt-in, or continue honoring the documented env contract.

Useful? React with 👍 / 👎.

Respawn one-shot macOS commands with Node's OpenSSL-backed CA store when system CA loading is inherited. This avoids an indefinite shutdown join on the Keychain loader worker while preserving system CA for Gateway, interactive, and explicit --use-system-ca processes.
@steipete
steipete force-pushed the claude/fix-cli-gateway-hang branch from 6ec12f4 to 3505c4b Compare July 18, 2026 02:59

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3505c4bd94

ℹ️ 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".

Comment thread src/entry.respawn.ts
Comment on lines +75 to +77
params.execArgv.includes(params.option) ||
nodeOptions.some((token) => {
if (token === params.option) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Recognize underscore CA flag aliases

When macOS operators use Node-supported underscore aliases such as --use_system_ca in process.execArgv or NODE_OPTIONS, this exact-match helper returns false, so the new Darwin branch treats the explicit system-CA opt-in as absent and rewrites NODE_USE_SYSTEM_CA to 0 / adds --use-openssl-ca. I verified Node accepts the underscore forms via process.allowedNodeEnvironmentFlags and node --use_openssl_ca; one-shot commands that rely on Keychain-only private CAs would therefore lose trust despite an explicit runtime flag.

Useful? React with 👍 / 👎.

@steipete
steipete merged commit 6e695c5 into main Jul 18, 2026
113 checks passed
@steipete
steipete deleted the claude/fix-cli-gateway-hang branch July 18, 2026 03:07
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 18, 2026
Respawn one-shot macOS commands with Node's OpenSSL-backed CA store when system CA loading is inherited. This avoids an indefinite shutdown join on the Keychain loader worker while preserving system CA for Gateway, interactive, and explicit --use-system-ca processes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes maintainer Maintainer-authored PR size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant