diff --git a/agent/lib/src/adb.dart b/agent/lib/src/adb.dart index 70c393638d..ae8f2321ea 100644 --- a/agent/lib/src/adb.dart +++ b/agent/lib/src/adb.dart @@ -84,6 +84,22 @@ class Adb { return results; } + /// Kills and restarts the `adb` server. + /// + /// Restarting `adb` helps with keeping device connections alive. When `adb` + /// runs non-stop for too long it loses connections to devices. + static Future restart() async { + int exitCode = await exec(config.adbPath, ['kill-server'], canFail: false); + + if (exitCode != 0) + throw 'Failed to kill ADB server'; + + exitCode = await exec(config.adbPath, ['start-server'], canFail: false); + + if (exitCode != 0) + throw 'Failed to start ADB server'; + } + static Future> get deviceIds async { List output = (await eval(config.adbPath, ['devices', '-l'], canFail: false)) .trim().split('\n'); diff --git a/agent/lib/src/commands/ci.dart b/agent/lib/src/commands/ci.dart index 4cac4b8174..9ae67b068d 100644 --- a/agent/lib/src/commands/ci.dart +++ b/agent/lib/src/commands/ci.dart @@ -47,6 +47,10 @@ class ContinuousIntegrationCommand extends Command { _listenToShutdownSignals(); while(!_exiting) { try { + // This increases the likelihood of obtaining a healthy connection to + // the device. + Adb.restart(); + // Check health before requesting a new task. health = await _performHealthChecks();