mirror of
https://github.com/agent0ai/agent-zero.git
synced 2026-04-28 03:30:23 +00:00
fix: kill orphaned bridge process on port before starting new one
This commit is contained in:
parent
45b21c093a
commit
50af7c2bde
1 changed files with 36 additions and 0 deletions
|
|
@ -6,6 +6,7 @@ No agent/tool dependencies.
|
|||
|
||||
import asyncio
|
||||
import os
|
||||
import platform
|
||||
import subprocess
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
|
@ -50,6 +51,7 @@ async def start_bridge(
|
|||
if allowed_users:
|
||||
cmd += ["--allowed-users", ",".join(allowed_users)]
|
||||
|
||||
_kill_port_process(port)
|
||||
PrintStyle.info("WhatsApp: starting bridge")
|
||||
_bridge_process = subprocess.Popen(
|
||||
cmd,
|
||||
|
|
@ -128,6 +130,7 @@ async def ensure_bridge_http_up(
|
|||
if allowed_users:
|
||||
cmd += ["--allowed-users", ",".join(allowed_users)]
|
||||
|
||||
_kill_port_process(port)
|
||||
PrintStyle.info("WhatsApp: starting bridge for pairing")
|
||||
_bridge_process = subprocess.Popen(
|
||||
cmd,
|
||||
|
|
@ -195,6 +198,39 @@ async def _ensure_npm_install() -> None:
|
|||
raise RuntimeError("npm install failed")
|
||||
|
||||
|
||||
def _kill_port_process(port: int) -> None:
|
||||
"""Kill any orphaned process listening on the given TCP port."""
|
||||
try:
|
||||
if platform.system() == "Windows":
|
||||
result = subprocess.run(
|
||||
["netstat", "-ano", "-p", "TCP"],
|
||||
capture_output=True, text=True, timeout=5,
|
||||
)
|
||||
for line in result.stdout.splitlines():
|
||||
parts = line.split()
|
||||
if len(parts) >= 5 and parts[3] == "LISTENING":
|
||||
if parts[1].endswith(f":{port}"):
|
||||
try:
|
||||
subprocess.run(
|
||||
["taskkill", "/PID", parts[4], "/F"],
|
||||
capture_output=True, timeout=5,
|
||||
)
|
||||
except subprocess.SubprocessError:
|
||||
pass
|
||||
else:
|
||||
result = subprocess.run(
|
||||
["fuser", f"{port}/tcp"],
|
||||
capture_output=True, timeout=5,
|
||||
)
|
||||
if result.returncode == 0:
|
||||
subprocess.run(
|
||||
["fuser", "-k", f"{port}/tcp"],
|
||||
capture_output=True, timeout=5,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _start_log_reader(process: subprocess.Popen) -> None:
|
||||
def _reader():
|
||||
assert process.stdout
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue