fix: restore automatic startup of local ADB server

This commit is contained in:
chenmingxuan committed 2026-10-09 10:17:07 +08:00
1 parent f826bdf3b0
commit 49873e7f05
9 files changed
+211 -23

No files matched your search

+15 -2
View File
@@ -92,6 +92,19 @@ class AdbEndpoint:
def mode(self) -> str:
return "local" if self.is_local_default else "remote"
@property
def client_host(self) -> str:
"""ADB only permits daemon auto-start for the literal host 'localhost'.
Keep forwarded/custom-port endpoints explicit, even on loopback, so a
missing tunnel never starts a different local daemon at that address.
"""
return "localhost" if self.is_local_default else self.host
@property
def client_socket(self) -> str:
return f"tcp:localhost:{self.port}" if self.is_local_default else self.socket
def to_dict(self) -> dict[str, Any]:
return {
**asdict(self),
@@ -108,7 +121,7 @@ class AdbEndpoint:
target = environment if environment is not None else os.environ
target["ADB_HOST"] = self.host
target["ADB_PORT"] = str(self.port)
target["ADB_SERVER_SOCKET"] = self.socket
target["ADB_SERVER_SOCKET"] = self.client_socket
target[ADB_ENDPOINT_ID_ENV] = self.identity
return target
@@ -145,7 +158,7 @@ class AdbSession:
return [
self.adb_path,
"-H",
self.endpoint.host,
self.endpoint.client_host,
"-P",
str(self.endpoint.port),
*arguments,
+14 -7
View File
@@ -25,6 +25,7 @@ import threading
import time
from typing import Any
from artemis.runtime.adb_endpoint import AdbSession, current_adb_endpoint
from artemis.runtime.device_lock import DeviceExecutionLock
from artemis.toolchain import toolchain
from artemis.utils.logger import get_logger
@@ -242,23 +243,29 @@ class DevicePool:
async def _start_adb_server(self, timeout: float) -> None:
"""Best-effort bounded `adb start-server` so later queries hit a warm daemon."""
endpoint = current_adb_endpoint()
if not endpoint.is_local_default:
return
adb = self._resolve_adb()
if not adb:
return
proc = None
try:
clean_env = os.environ.copy()
clean_env.pop("ADB_SERVER_SOCKET", None)
session = AdbSession(endpoint, adb_path=adb)
proc = await asyncio.create_subprocess_exec(
adb,
"start-server",
*session.command(["start-server"]),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=clean_env,
env=session.environment(),
)
await asyncio.wait_for(proc.communicate(), timeout=timeout)
stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout=timeout)
if proc.returncode != 0:
output = (stderr or stdout).decode(errors="replace").strip()
logger.warning(
f"Local ADB server startup failed (exit {proc.returncode}): {output}"
)
except Exception as exc:
logger.debug(f"adb start-server warm-up did not complete: {exc}")
logger.warning(f"Local ADB server startup did not complete: {exc}")
if proc is not None:
try:
proc.kill()