Anurag commited on
Commit
2959b7b
Β·
1 Parent(s): d5203bf

Improve HF Spaces browser/dbus reliability and timeouts

Browse files
Files changed (3) hide show
  1. Dockerfile +2 -0
  2. env-builder.js +9 -0
  3. start.sh +65 -11
Dockerfile CHANGED
@@ -24,6 +24,8 @@ RUN apt-get update && apt-get install -y \
24
  ca-certificates \
25
  jq \
26
  curl \
 
 
27
  python3 \
28
  python3-pip \
29
  chromium \
 
24
  ca-certificates \
25
  jq \
26
  curl \
27
+ dbus \
28
+ dbus-x11 \
29
  python3 \
30
  python3-pip \
31
  chromium \
env-builder.js CHANGED
@@ -482,6 +482,15 @@ const FIELDS = [
482
  "common": 1,
483
  "tag": "build"
484
  },
 
 
 
 
 
 
 
 
 
485
  {
486
  "g": "Startup",
487
  "icon": "⚑",
 
482
  "common": 1,
483
  "tag": "build"
484
  },
485
+ {
486
+ "g": "Startup",
487
+ "icon": "🩺",
488
+ "k": "AUTO_DOCTOR",
489
+ "lbl": "Auto-fix config on boot (openclaw doctor --fix)",
490
+ "type": "toggle",
491
+ "ph": "false",
492
+ "tag": "advanced"
493
+ },
494
  {
495
  "g": "Startup",
496
  "icon": "⚑",
start.sh CHANGED
@@ -99,6 +99,9 @@ DEVDATA_ENABLED=true
99
  if ! hc_is_true "$DEVDATA_NORMALIZED"; then
100
  DEVDATA_ENABLED=false
101
  fi
 
 
 
102
  if [ -n "${SPACE_HOST:-}" ]; then
103
  OPENCLAW_CONSOLE_LOG_LEVEL="${OPENCLAW_CONSOLE_LOG_LEVEL:-warn}"
104
  OPENCLAW_FILE_LOG_LEVEL="${OPENCLAW_FILE_LOG_LEVEL:-info}"
@@ -508,10 +511,19 @@ inject_provider_models_from_env "github-copilot" "GITHUB_COPILOT_MODELS" "COPILO
508
  BROWSER_EXECUTABLE_PATH=""
509
  for candidate in /usr/bin/chromium /usr/bin/chromium-browser /snap/bin/chromium; do
510
  if [ -x "$candidate" ]; then
511
- BROWSER_EXECUTABLE_PATH="$candidate"
512
- break
 
 
 
 
 
 
513
  fi
514
  done
 
 
 
515
 
516
  BROWSER_SHOULD_ENABLE=false
517
  if [ "$BROWSER_PLUGIN_MODE" = "enabled" ] && [ -n "$BROWSER_EXECUTABLE_PATH" ] && [ -x "$BROWSER_EXECUTABLE_PATH" ]; then
@@ -569,7 +581,20 @@ if [ "$BROWSER_SHOULD_ENABLE" = "true" ]; then
569
  "defaultProfile": "openclaw",
570
  "headless": true,
571
  "noSandbox": true,
572
- "executablePath": $execPath
 
 
 
 
 
 
 
 
 
 
 
 
 
573
  }
574
  | .agents.defaults.sandbox.browser.allowHostControl = true' <<<"$CONFIG_JSON")
575
  fi
@@ -821,16 +846,16 @@ warmup_browser() {
821
  [ "$BROWSER_SHOULD_ENABLE" = "true" ] || return 0
822
 
823
  (
824
- sleep 5
825
 
826
  local attempt
827
- for attempt in 1 2 3 4 5; do
828
  if openclaw browser --browser-profile openclaw start >/dev/null 2>&1; then
829
  openclaw browser --browser-profile openclaw open about:blank >/dev/null 2>&1 || true
830
  echo "Managed browser ready."
831
  return 0
832
  fi
833
- sleep 2
834
  done
835
 
836
  echo "Warning: managed browser warm-up did not complete; first browser action may need a retry."
@@ -1438,7 +1463,9 @@ if [ -n "${HUGGINGCLAW_OPENCLAW_PLUGINS:-}" ]; then
1438
  fi
1439
 
1440
  # ── Fix config before running startup commands ──
1441
- openclaw doctor --fix || true
 
 
1442
 
1443
  # ── Arbitrary startup commands from HF Variables/Secrets ──
1444
  # Recommended: use one variable, HUGGINGCLAW_RUN, as a full bash script. If the
@@ -1581,7 +1608,19 @@ while true; do
1581
  fi
1582
  fi
1583
 
1584
- openclaw doctor --fix || true
 
 
 
 
 
 
 
 
 
 
 
 
1585
  echo "Launching OpenClaw gateway on port 7860..."
1586
 
1587
  GATEWAY_ARGS=(gateway run --port 7860 --bind lan)
@@ -1618,14 +1657,29 @@ while true; do
1618
  echo "Gateway failed to start. Last 30 lines of log:"
1619
  echo "────────────────────────────────────────────"
1620
  tail -30 /home/node/.openclaw/gateway.log
1621
- echo "Gateway failed β€” JupyterLab and env-builder still running. Retrying in 10s..."
1622
- sleep 10
1623
- continue
 
 
 
 
 
1624
  fi
1625
 
1626
  # 11. Start WhatsApp Guardian after the gateway is accepting connections
1627
  start_guardian_once
1628
 
 
 
 
 
 
 
 
 
 
 
1629
  # 11.5 Warm up the managed browser so first browser actions have a live tab
1630
  warmup_browser
1631
 
 
99
  if ! hc_is_true "$DEVDATA_NORMALIZED"; then
100
  DEVDATA_ENABLED=false
101
  fi
102
+ # On HF Spaces, browser is disabled by default (no display server).
103
+ # To enable: set BROWSER_PLUGIN_MODE=enabled as an HF Space secret.
104
+ # WARNING: requires at least CPU Upgrade tier (2 vCPU / 16GB RAM).
105
  if [ -n "${SPACE_HOST:-}" ]; then
106
  OPENCLAW_CONSOLE_LOG_LEVEL="${OPENCLAW_CONSOLE_LOG_LEVEL:-warn}"
107
  OPENCLAW_FILE_LOG_LEVEL="${OPENCLAW_FILE_LOG_LEVEL:-info}"
 
511
  BROWSER_EXECUTABLE_PATH=""
512
  for candidate in /usr/bin/chromium /usr/bin/chromium-browser /snap/bin/chromium; do
513
  if [ -x "$candidate" ]; then
514
+ # Reject snap stubs β€” they silently fail in Docker
515
+ if file "$candidate" 2>/dev/null | grep -q "ELF"; then
516
+ BROWSER_EXECUTABLE_PATH="$candidate"
517
+ break
518
+ elif head -1 "$candidate" 2>/dev/null | grep -qv "snap\|exec"; then
519
+ BROWSER_EXECUTABLE_PATH="$candidate"
520
+ break
521
+ fi
522
  fi
523
  done
524
+ if [ -z "$BROWSER_EXECUTABLE_PATH" ]; then
525
+ echo "Warning: No real Chromium binary found. Browser plugin will be disabled."
526
+ fi
527
 
528
  BROWSER_SHOULD_ENABLE=false
529
  if [ "$BROWSER_PLUGIN_MODE" = "enabled" ] && [ -n "$BROWSER_EXECUTABLE_PATH" ] && [ -x "$BROWSER_EXECUTABLE_PATH" ]; then
 
581
  "defaultProfile": "openclaw",
582
  "headless": true,
583
  "noSandbox": true,
584
+ "executablePath": $execPath,
585
+ "localLaunchTimeoutMs": 45000,
586
+ "localCdpReadyTimeoutMs": 30000,
587
+ "extraArgs": [
588
+ "--disable-dev-shm-usage",
589
+ "--disable-gpu",
590
+ "--disable-setuid-sandbox",
591
+ "--no-first-run",
592
+ "--disable-background-networking",
593
+ "--disable-sync",
594
+ "--disable-translate",
595
+ "--disable-notifications",
596
+ "--disable-speech-api"
597
+ ]
598
  }
599
  | .agents.defaults.sandbox.browser.allowHostControl = true' <<<"$CONFIG_JSON")
600
  fi
 
846
  [ "$BROWSER_SHOULD_ENABLE" = "true" ] || return 0
847
 
848
  (
849
+ sleep 8
850
 
851
  local attempt
852
+ for attempt in 1 2 3 4 5 6; do
853
  if openclaw browser --browser-profile openclaw start >/dev/null 2>&1; then
854
  openclaw browser --browser-profile openclaw open about:blank >/dev/null 2>&1 || true
855
  echo "Managed browser ready."
856
  return 0
857
  fi
858
+ sleep 5
859
  done
860
 
861
  echo "Warning: managed browser warm-up did not complete; first browser action may need a retry."
 
1463
  fi
1464
 
1465
  # ── Fix config before running startup commands ──
1466
+ if [ "${AUTO_DOCTOR:-false}" = "true" ]; then
1467
+ openclaw doctor --fix || true
1468
+ fi
1469
 
1470
  # ── Arbitrary startup commands from HF Variables/Secrets ──
1471
  # Recommended: use one variable, HUGGINGCLAW_RUN, as a full bash script. If the
 
1608
  fi
1609
  fi
1610
 
1611
+ if [ "${AUTO_DOCTOR:-false}" = "true" ]; then
1612
+ openclaw doctor --fix || true
1613
+ fi
1614
+ # ── Silence D-Bus errors for headless Chromium ──
1615
+ if [ -z "${DBUS_SESSION_BUS_ADDRESS:-}" ]; then
1616
+ if command -v dbus-launch >/dev/null 2>&1; then
1617
+ eval "$(dbus-launch --sh-syntax 2>/dev/null)" || true
1618
+ export DBUS_SESSION_BUS_ADDRESS="${DBUS_SESSION_BUS_ADDRESS:-disabled:}"
1619
+ else
1620
+ export DBUS_SESSION_BUS_ADDRESS="disabled:"
1621
+ fi
1622
+ fi
1623
+
1624
  echo "Launching OpenClaw gateway on port 7860..."
1625
 
1626
  GATEWAY_ARGS=(gateway run --port 7860 --bind lan)
 
1657
  echo "Gateway failed to start. Last 30 lines of log:"
1658
  echo "────────────────────────────────────────────"
1659
  tail -30 /home/node/.openclaw/gateway.log
1660
+ if [ "$DEV_MODE_ENABLED" = "true" ]; then
1661
+ echo "Gateway failed β€” DEV_MODE active, retrying in 10s..."
1662
+ sleep 10
1663
+ continue
1664
+ else
1665
+ echo "Gateway failed β€” exiting."
1666
+ exit 1
1667
+ fi
1668
  fi
1669
 
1670
  # 11. Start WhatsApp Guardian after the gateway is accepting connections
1671
  start_guardian_once
1672
 
1673
+ # ── Silence D-Bus errors for headless Chromium ──
1674
+ if [ -z "${DBUS_SESSION_BUS_ADDRESS:-}" ]; then
1675
+ if command -v dbus-launch >/dev/null 2>&1; then
1676
+ eval "$(dbus-launch --sh-syntax 2>/dev/null)" || true
1677
+ export DBUS_SESSION_BUS_ADDRESS="${DBUS_SESSION_BUS_ADDRESS:-disabled:}"
1678
+ else
1679
+ export DBUS_SESSION_BUS_ADDRESS="disabled:"
1680
+ fi
1681
+ fi
1682
+
1683
  # 11.5 Warm up the managed browser so first browser actions have a live tab
1684
  warmup_browser
1685