ux: wait for OpenClaw gateway to be ready before launching TUI (#1385)

Fixes #1354 - users experienced a ~30s delay with "gateway not connected"
errors when trying to use OpenClaw immediately after launch.

Root cause: gateway takes time to bind to port 18789, but TUI launched
after only 2 seconds.

Solution: Add wait_for_openclaw_gateway() helper that polls the gateway
port (max 30s) before launching TUI, ensuring immediate usability.

Changes:
- shared/common.sh: Add wait_for_openclaw_gateway() function
- All openclaw.sh scripts (10 files): Replace sleep 2 with gateway readiness check

Agent: ux-engineer

Co-authored-by: B <6723574+louisgv@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
A 2026-02-17 00:49:53 -08:00 committed by GitHub
parent 5678129126
commit f412fb69bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 39 additions and 10 deletions

View file

@ -26,7 +26,7 @@ agent_env_vars() {
agent_configure() { setup_openclaw_config "${OPENROUTER_API_KEY}" "${MODEL_ID}" cloud_upload cloud_run; }
agent_pre_launch() {
cloud_run "source ~/.zshrc && nohup openclaw gateway > /tmp/openclaw-gateway.log 2>&1 &"
sleep 2
wait_for_openclaw_gateway cloud_run
}
agent_launch_cmd() { echo 'source ~/.zshrc && openclaw tui'; }

View file

@ -32,7 +32,7 @@ agent_configure() {
agent_pre_launch() {
cloud_run "source ~/.zshrc && nohup openclaw gateway > /tmp/openclaw-gateway.log 2>&1 &"
sleep 2
wait_for_openclaw_gateway cloud_run
}
agent_launch_cmd() {

View file

@ -26,7 +26,7 @@ agent_env_vars() {
agent_configure() { setup_openclaw_config "${OPENROUTER_API_KEY}" "${MODEL_ID}" cloud_upload cloud_run; }
agent_pre_launch() {
cloud_run "source ~/.zshrc && nohup openclaw gateway > /tmp/openclaw-gateway.log 2>&1 &"
sleep 2
wait_for_openclaw_gateway cloud_run
}
agent_launch_cmd() { echo 'source ~/.zshrc && openclaw tui'; }

View file

@ -32,7 +32,7 @@ agent_configure() {
agent_pre_launch() {
cloud_run "source ~/.zshrc && nohup openclaw gateway > /tmp/openclaw-gateway.log 2>&1 &"
sleep 2
wait_for_openclaw_gateway cloud_run
}
agent_launch_cmd() {

View file

@ -26,7 +26,7 @@ agent_env_vars() {
agent_configure() { setup_openclaw_config "${OPENROUTER_API_KEY}" "${MODEL_ID}" cloud_upload cloud_run; }
agent_pre_launch() {
cloud_run "source ~/.zshrc && nohup openclaw gateway > /tmp/openclaw-gateway.log 2>&1 &"
sleep 2
wait_for_openclaw_gateway cloud_run
}
agent_launch_cmd() { echo 'source ~/.zshrc && openclaw tui'; }

View file

@ -26,7 +26,7 @@ agent_env_vars() {
agent_configure() { setup_openclaw_config "${OPENROUTER_API_KEY}" "${MODEL_ID}" cloud_upload cloud_run; }
agent_pre_launch() {
cloud_run "source ~/.zshrc && nohup openclaw gateway > /tmp/openclaw-gateway.log 2>&1 &"
sleep 2
wait_for_openclaw_gateway cloud_run
}
agent_launch_cmd() { echo 'source ~/.zshrc && openclaw tui'; }

View file

@ -32,7 +32,7 @@ agent_configure() {
agent_pre_launch() {
cloud_run "source ~/.zshrc 2>/dev/null; nohup openclaw gateway > /tmp/openclaw-gateway.log 2>&1 &"
sleep 2
wait_for_openclaw_gateway cloud_run
}
agent_launch_cmd() {

View file

@ -26,7 +26,7 @@ agent_env_vars() {
agent_configure() { setup_openclaw_config "${OPENROUTER_API_KEY}" "${MODEL_ID}" cloud_upload cloud_run; }
agent_pre_launch() {
cloud_run "source ~/.zshrc && nohup openclaw gateway > /tmp/openclaw-gateway.log 2>&1 &"
sleep 2
wait_for_openclaw_gateway cloud_run
}
agent_launch_cmd() { echo 'source ~/.zshrc && openclaw tui'; }

View file

@ -26,7 +26,7 @@ agent_env_vars() {
agent_configure() { setup_openclaw_config "${OPENROUTER_API_KEY}" "${MODEL_ID}" cloud_upload cloud_run; }
agent_pre_launch() {
cloud_run "source ~/.zshrc && nohup openclaw gateway > /tmp/openclaw-gateway.log 2>&1 &"
sleep 2
wait_for_openclaw_gateway cloud_run
}
agent_launch_cmd() { echo 'source ~/.zshrc && openclaw tui'; }

View file

@ -3002,6 +3002,35 @@ setup_openclaw_config() {
upload_config_file "${upload_callback}" "${run_callback}" "${openclaw_json}" "\$HOME/.openclaw/openclaw.json"
}
# Wait for OpenClaw gateway to be ready
# Usage: wait_for_openclaw_gateway RUN_CALLBACK
#
# Arguments:
# RUN_CALLBACK - Function to run commands: func(command)
#
# Returns:
# 0 if gateway starts successfully, 1 if timeout
wait_for_openclaw_gateway() {
local run_callback="${1}"
local max_wait=30
local elapsed=0
log_step "Waiting for OpenClaw gateway to start..."
while [ $elapsed -lt $max_wait ]; do
if ${run_callback} "nc -z 127.0.0.1 18789 2>/dev/null || (command -v telnet >/dev/null && timeout 1 telnet 127.0.0.1 18789 2>&1 | grep -q Connected)"; then
log_info "Gateway ready after ${elapsed}s"
return 0
fi
sleep 1
elapsed=$((elapsed + 1))
done
log_error "OpenClaw gateway failed to start after ${max_wait}s"
log_info "Check gateway logs: cat /tmp/openclaw-gateway.log"
return 1
}
# ============================================================
# Continue configuration setup
# ============================================================

View file

@ -32,7 +32,7 @@ agent_configure() {
agent_pre_launch() {
cloud_run "source ~/.zshrc && nohup openclaw gateway > /tmp/openclaw-gateway.log 2>&1 &"
sleep 2
wait_for_openclaw_gateway cloud_run
}
agent_launch_cmd() {