Skip to content

fix(tui): honor active Gateway runtime port in TUI connection#393

Open
BingqingLyu wants to merge 4 commits into
mainfrom
fork-pr-42496-fix-issue-42461
Open

fix(tui): honor active Gateway runtime port in TUI connection#393
BingqingLyu wants to merge 4 commits into
mainfrom
fork-pr-42496-fix-issue-42461

Conversation

@BingqingLyu

@BingqingLyu BingqingLyu commented Apr 27, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes openclaw#42461

The TUI connection path now applies the active Gateway runtime port override before resolving the connection target. This ensures that when the Gateway is running on a custom port (e.g., 48789), the TUI automatically connects to that port instead of defaulting to 18789.

Changes

  • Added port field to the gateway lock payload in src/infra/gateway-lock.ts
  • Gateway now writes its port to the lock file when acquiring the lock
  • Added applyGatewayRuntimePortEnvOverride() function in src/config/paths.ts to read the lock file and set OPENCLAW_GATEWAY_PORT env var
  • Called applyGatewayRuntimePortEnvOverride() at the start of resolveGatewayConnection() in src/tui/gateway-chat.ts

Testing

  • Verified the code change applies the port override correctly
  • Existing tests should continue to pass

Fixes openclaw#42461

fuleinist and others added 4 commits March 11, 2026 04:42
Fixes openclaw#42461

- Apply applyGatewayRuntimePortEnvOverride() before resolving TUI Gateway URL
- Ensures TUI connects to the correct port when Gateway runs on custom port
- Added port field to gateway lock payload for runtime port discovery
…PID in TUI port override

- In acquireGatewayLock, pass lastPayload?.port (existing gateway's port)
  to resolveGatewayOwnerStatus instead of opts.port (new gateway's port).
  This prevents incorrectly declaring a live gateway as dead when two
  gateways target different ports.

- In applyGatewayRuntimePortEnvOverride, verify the PID is still alive
  before applying the port from the lock file. This prevents using a
  stale lock from a crashed gateway.

- Update tests to reflect correct behavior: lock files now include port
  field, and liveness checks use the lock holder's port.

Fixes review feedback from PR openclaw#42496.
Two review fixes for PR openclaw#42496:

1. readLockPayload: validate port range (1-65535) before returning.
   Prevents ERR_SOCKET_BAD_PORT when a stale/corrupted lock contains
   an out-of-range port.

2. applyGatewayRuntimePortEnvOverride: also check legacy
   CLAWDBOT_GATEWAY_PORT env var when deciding whether to apply
   the lock file port override, preserving backward compatibility.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…non-integer ports

- Guard applyGatewayRuntimePortEnvOverride against PID reuse: when a lock
  carries startTime, compare it against the current process start time
  read from /proc/{pid}/stat before trusting the runtime-port override.
  If the times differ the lock is stale (PID recycled) and is ignored.
- Add Number.isInteger check in readLockPayload so malformed locks with
  fractional ports (e.g. 18789.5) are treated as missing, preventing
  ERR_SOCKET_BAD_PORT from net.createConnection during owner probes.
- Export readLinuxStartTime so it can be used by paths.ts for the
  startTime verification above.

Addresses chatgpt-codex-connector[bot] P2 comments on openclaw#42496.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: openclaw tui does not honor the active Gateway runtime port and still targets 18789

3 participants