-
Notifications
You must be signed in to change notification settings - Fork 52.8k
fix(claw): warn if OpenClaw is running before migration #8102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
|
|
||
| import importlib.util | ||
| import logging | ||
| import subprocess | ||
| import sys | ||
| from datetime import datetime | ||
| from pathlib import Path | ||
|
|
@@ -52,6 +53,73 @@ | |
| # Known OpenClaw directory names (current + legacy) | ||
| _OPENCLAW_DIR_NAMES = (".openclaw", ".clawdbot", ".moldbot") | ||
|
|
||
| def _is_openclaw_running() -> bool: | ||
| """Check whether an OpenClaw process appears to be running.""" | ||
| if sys.platform == "win32": | ||
| try: | ||
| # First check for dedicated executables | ||
| for exe in ("openclaw.exe", "clawd.exe"): | ||
| result = subprocess.run( | ||
| ["tasklist", "/FI", f"IMAGENAME eq {exe}"], | ||
| capture_output=True, text=True, timeout=5 | ||
| ) | ||
| if exe in result.stdout.lower(): | ||
| return True | ||
|
|
||
| # Check node.exe processes for openclaw/clawd in command line. | ||
| # tasklist does not include command lines, so we use PowerShell. | ||
| ps_cmd = ( | ||
| 'Get-CimInstance Win32_Process -Filter "Name = \'node.exe\'" | ' | ||
| 'Where-Object { $_.CommandLine -match "openclaw|clawd" } | ' | ||
| 'Select-Object -First 1 ProcessId' | ||
| ) | ||
| result = subprocess.run( | ||
| ["powershell", "-NoProfile", "-Command", ps_cmd], | ||
| capture_output=True, text=True, timeout=5 | ||
| ) | ||
| return bool(result.stdout.strip()) | ||
| except Exception: | ||
| return False | ||
|
|
||
| for cmd in (["pgrep", "-f", "openclaw"], ["pgrep", "-f", "clawd"]): | ||
| try: | ||
| result = subprocess.run(cmd, capture_output=True, timeout=3) | ||
| if result.returncode == 0: | ||
| return True | ||
| except (FileNotFoundError, subprocess.TimeoutExpired): | ||
| continue | ||
| return False | ||
|
|
||
|
|
||
| def _warn_if_openclaw_running(auto_yes: bool) -> None: | ||
| """Warn if OpenClaw is still running before migration. | ||
|
|
||
| Telegram, Discord, and Slack only allow one active connection per bot | ||
| token. Migrating while OpenClaw is running causes both to fight for the | ||
| same token. | ||
| """ | ||
| if not _is_openclaw_running(): | ||
| return | ||
|
|
||
| print() | ||
| print_error("OpenClaw appears to be running.") | ||
| print_info( | ||
| "Messaging platforms (Telegram, Discord, Slack) only allow one " | ||
| "active session per bot token. If you continue, both OpenClaw and " | ||
| "Hermes may try to use the same token, causing disconnects." | ||
| ) | ||
| print_info("Recommendation: stop OpenClaw before migrating.") | ||
| print() | ||
| if auto_yes: | ||
| return | ||
| if not sys.stdin.isatty(): | ||
| print_info("Non-interactive session — continuing to preview only.") | ||
| return | ||
| if not prompt_yes_no("Continue anyway?", default=False): | ||
| print_info("Migration cancelled. Stop OpenClaw and try again.") | ||
| sys.exit(0) | ||
|
Comment on lines
+104
to
+120
|
||
|
|
||
|
|
||
| def _warn_if_gateway_running(auto_yes: bool) -> None: | ||
| """Check if a Hermes gateway is running with connected platforms. | ||
|
|
||
|
|
@@ -287,8 +355,11 @@ def _cmd_migrate(args): | |
| print_info(f"Workspace: {workspace_target}") | ||
| print() | ||
|
|
||
| # Check if a gateway is running with connected platforms — migrating tokens | ||
| # while the gateway is active will cause conflicts (e.g. Telegram 409). | ||
| # Check if OpenClaw is still running — migrating tokens while both are | ||
| # active will cause conflicts (e.g. Telegram 409). | ||
| _warn_if_openclaw_running(auto_yes) | ||
|
|
||
| # Check if a Hermes gateway is running with connected platforms. | ||
| _warn_if_gateway_running(auto_yes) | ||
|
|
||
| # Ensure config.yaml exists before migration tries to read it | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Windows detection likely won’t work as written:
tasklistoutput does not include process command lines (only the image name, PID, etc.), so searching stdout foropenclaw/clawdwill almost always return false even when OpenClaw is running undernode.exe. Consider querying command lines via PowerShell/WMI (e.g.,Get-CimInstance Win32_Processfiltering onnode.exeand inspectingCommandLine) or checking for an actualclawd.exe/openclaw*.exeimage name if applicable, and update the tests accordingly.