Skip to content

Gateway double-spawn when process undetected by listProcesses() #289

@amit-blotout

Description

@amit-blotout

Problem

When the OpenClaw gateway is already running inside the container (e.g. started automatically on container boot), ensureMoltbotGateway can fail to detect it via sandbox.listProcesses() and attempts to spawn a second instance, resulting in:

Gateway failed to start: gateway already running (pid 402); lock timeout after 5000ms
Port 18789 is already in use.

Root Cause

findExistingMoltbotProcess matches command strings like start-openclaw.sh and openclaw gateway, but the sandbox process list may expose the process as bash /usr/local/bin/start-openclaw.sh (with full path and shell prefix), which the current string-matching misses.

Fix

Two changes:

  1. Broaden command matching — also match /usr/local/bin/start-openclaw.sh (full path) so bash-invoked scripts are detected correctly.

  2. Port pre-check before spawning — before starting a new process, check if port 18789 is already listening via a TCP probe. If it is, skip the spawn entirely even if listProcesses returned nothing useful. This acts as a safety net against any future process-detection gaps.

Steps to Reproduce

  1. Deploy to Cloudflare Workers with Sandbox
  2. Let the container start and the gateway boot
  3. Trigger a new request that calls ensureMoltbotGateway
  4. Observe the "gateway already running" / "port in use" error in logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions