Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 109 additions & 9 deletions src/Xamarin.Android.Build.Debugging.Tasks/Tasks/FastDeploy2.Adb.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Android.Tools;

namespace Xamarin.Android.Tasks
{
public partial class FastDeploy2
Expand Down Expand Up @@ -49,7 +52,7 @@ async Task ForceStopPackage (string packageName)
/// Uninstalls <paramref name="packageName"/> via <c>adb shell pm uninstall</c>, optionally
/// preserving the application's data and cache directories (<c>-k</c>).
/// </summary>
async Task UninstallPackage (string packageName, bool preserveData, string user)
internal virtual async Task UninstallPackage (string packageName, bool preserveData, string user)
{
var args = new List<string> { "pm", "uninstall" };
if (preserveData) {
Expand All @@ -66,12 +69,14 @@ async Task UninstallPackage (string packageName, bool preserveData, string user)
/// <summary>
/// Installs an APK via <c>adb install</c>. On an "already exists" or "requires uninstall"
/// failure the package is uninstalled (optionally preserving data) and the install is
/// retried once, mirroring the legacy behavior. Any other failure throws a
/// retried once, mirroring the legacy behavior. A recognized transient device transport
/// or package-manager failure waits for recovery and retries once. Any other failure throws a
/// <see cref="FastDeployInstallException"/> carrying the matching <c>ADB####</c> error code.
/// </summary>
async Task InstallApkWithRetry (string apkFile, bool reinstall, bool testOnly, string user)
internal async Task InstallApkWithRetry (string apkFile, bool reinstall, bool testOnly, string user)
{
var result = await RunInstallCommand (apkFile, reinstall, testOnly, user);
var retryState = new InstallRetryState ();
var result = await RunInstallCommandWithTransportRetry (apkFile, reinstall, testOnly, user, retryState);
var kind = ClassifyInstallResult (result);
if (kind == InstallResultKind.Success) {
return;
Expand All @@ -81,17 +86,56 @@ async Task InstallApkWithRetry (string apkFile, bool reinstall, bool testOnly, s
bool preserveData = kind == InstallResultKind.AlreadyExists;
LogDebugMessage ($"Package '{PackageName}' could not be installed directly ({kind}). Uninstalling (preserving data: {preserveData}) and retrying.");
await UninstallPackage (PackageName, preserveData: preserveData, user: user);
result = await RunInstallCommand (apkFile, reinstall: true, testOnly: testOnly, user: user);
result = await RunInstallCommandWithTransportRetry (apkFile, reinstall: true, testOnly: testOnly, user: user, retryState);
kind = ClassifyInstallResult (result);
if (kind == InstallResultKind.Success) {
return;
}
}

throw new FastDeployInstallException (GetInstallErrorCode (kind), result.Output);
throw new FastDeployInstallException (GetInstallErrorCode (kind), retryState.GetFailureMessage ());
}

async Task<AdbCommandResult> RunInstallCommand (string apkFile, bool reinstall, bool testOnly, string user)
async Task<AdbCommandResult> RunInstallCommandWithTransportRetry (
Comment thread
simonrozsival marked this conversation as resolved.
Resolved
string apkFile,
bool reinstall,
bool testOnly,
string user,
InstallRetryState retryState)
{
var result = await RunInstallCommand (apkFile, reinstall, testOnly, user);
retryState.AddInstallAttempt (result);
if (retryState.TransportRetryUsed || ClassifyInstallResult (result) != InstallResultKind.TransientTransport) {
return result;
}

retryState.TransportRetryUsed = true;
LogDebugMessage ($"Transient ADB/package-manager communication failure while installing '{PackageName}'. Waiting for recovery before retrying once.");
try {
await WaitForInstallTransportRecovery ();
retryState.AddRecoveryDiagnostic ("The device and Android package manager became responsive.");
} catch (OperationCanceledException) {
throw;
} catch (Exception ex) {
retryState.AddRecoveryDiagnostic ($"Recovery failed: {ex}");
throw new FastDeployInstallException ("ADB0010", retryState.GetFailureMessage ());
}

result = await RunInstallCommand (apkFile, reinstall, testOnly, user);
retryState.AddInstallAttempt (result);
return result;
}

internal virtual async Task WaitForInstallTransportRecovery ()
{
string serial = string.Equals (DeviceId, "any", StringComparison.OrdinalIgnoreCase) ? null : DeviceId;
var adbRunner = new AdbRunner (
ResolveAdbPath (),
logger: (_, message) => LogDiagnostic ($"ADB install recovery: {message}"));
await adbRunner.WaitForPackageManagerAsync (serial, TimeSpan.FromSeconds (10), CancellationToken);
}

internal virtual async Task<AdbCommandResult> RunInstallCommand (string apkFile, bool reinstall, bool testOnly, string user)
{
var args = new List<string> { "install" };
if (reinstall) {
Expand All @@ -112,22 +156,23 @@ async Task<AdbCommandResult> RunInstallCommand (string apkFile, bool reinstall,
return await RunLoggedDeviceOperation (operation, () => RunAdbCommand (args.ToArray ()));
}

enum InstallResultKind
internal enum InstallResultKind
{
Success,
AlreadyExists,
RequiresUninstall,
IncompatibleCpuAbi,
SdkNotSupported,
InsufficientSpace,
TransientTransport,
Failed,
}

/// <summary>
/// Classifies <c>adb install</c> output, mirroring the failure categories that the legacy
/// install path raised as typed exceptions.
/// </summary>
static InstallResultKind ClassifyInstallResult (AdbCommandResult result)
internal static InstallResultKind ClassifyInstallResult (AdbCommandResult result)
{
string output = result.Output ?? "";

Expand Down Expand Up @@ -156,6 +201,9 @@ static InstallResultKind ClassifyInstallResult (AdbCommandResult result)
if (output.Contains ("[INSTALL_FAILED_CPU_ABI_INCOMPATIBLE") || output.Contains ("[INSTALL_FAILED_NO_MATCHING_ABIS")) {
return InstallResultKind.IncompatibleCpuAbi;
}
if (IsTransientInstallFailure (output)) {
return InstallResultKind.TransientTransport;
}

if (result.ExitCode != 0 ||
output.IndexOf ("Failure", StringComparison.OrdinalIgnoreCase) >= 0 ||
Expand All @@ -168,6 +216,18 @@ static InstallResultKind ClassifyInstallResult (AdbCommandResult result)
return InstallResultKind.Success;
}

internal static bool IsTransientInstallFailure (string output)
{
if (string.IsNullOrEmpty (output)) {
return false;
}
if (output.IndexOf ("device offline", StringComparison.OrdinalIgnoreCase) >= 0) {
return true;
}
Comment thread
simonrozsival marked this conversation as resolved.
Resolved
return output.IndexOf ("Failure calling service package", StringComparison.OrdinalIgnoreCase) >= 0 &&
output.IndexOf ("Broken pipe", StringComparison.OrdinalIgnoreCase) >= 0;
}

static string GetInstallErrorCode (InstallResultKind kind)
{
return kind switch {
Expand All @@ -179,6 +239,46 @@ static string GetInstallErrorCode (InstallResultKind kind)
_ => "ADB0010",
};
}

sealed class InstallRetryState
{
readonly List<string> diagnostics = new List<string> ();
string firstInstallOutput;
int installAttemptCount;

public bool TransportRetryUsed { get; set; }

public void AddInstallAttempt (AdbCommandResult result)
{
installAttemptCount++;
string output = result.Output ?? "";
if (installAttemptCount == 1) {
firstInstallOutput = output;
}
diagnostics.Add ($"Install attempt {installAttemptCount} (exit code {result.ExitCode}):{Environment.NewLine}{(output.Length > 0 ? output : "<no output>")}");
}

public void AddRecoveryDiagnostic (string diagnostic)
{
diagnostics.Add ($"ADB transport recovery:{Environment.NewLine}{diagnostic}");
}

public string GetFailureMessage ()
{
if (installAttemptCount == 1 && diagnostics.Count == 1) {
return firstInstallOutput ?? "";
}

var message = new StringBuilder ();
foreach (string diagnostic in diagnostics) {
if (message.Length > 0) {
message.AppendLine ();
}
message.AppendLine (diagnostic);
}
return message.ToString ().TrimEnd ();
}
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ static bool IsMissingDirectoryError (string output)
output.IndexOf ("No such file or directory", StringComparison.OrdinalIgnoreCase) >= 0;
}

struct AdbCommandResult
internal struct AdbCommandResult
{
public int ExitCode;
public string StandardOutput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Xamarin.Android.Build;
using Xamarin.Android.Tasks;
using Xamarin.ProjectTools;
Expand Down Expand Up @@ -177,6 +178,172 @@ public void FastDeploy2DoesNotResetForUnrelatedPushFailure ()
Assert.IsFalse (FastDeploy2.IsUnexpectedRemoteFilesystemError ("adb: error: device offline"));
}

[TestCase ("adb: error: device offline", true)]
[TestCase ("adb: failed to install app.apk: cmd: Failure calling service package: Broken pipe (32)", true)]
[TestCase ("adb: failed to install app.apk: Broken pipe (32)", false)]
[TestCase ("cmd: Failure calling service package: Security exception", false)]
[TestCase ("Failure [INSTALL_FAILED_INSUFFICIENT_STORAGE]", false)]
public void FastDeploy2ClassifiesOnlyKnownTransientInstallFailures (string output, bool expected)
{
Assert.AreEqual (expected, FastDeploy2.IsTransientInstallFailure (output));
}

[Test]
public async Task FastDeploy2RetriesTransientInstallOnce ()
Comment thread
simonrozsival marked this conversation as resolved.
Resolved
{
var task = new TestFastDeploy2 (
CreateAdbResult (1, "adb: failed to install app.apk: cmd: Failure calling service package: Broken pipe (32)"),
CreateAdbResult (0, "Success"));

await task.InstallApkWithRetry ("app.apk", reinstall: false, testOnly: false, user: "");

Assert.AreEqual (2, task.InstallAttempts);
Assert.AreEqual (1, task.RecoveryAttempts);
}

[Test]
public async Task FastDeploy2RetriesTransientInstallAfterUninstall ()
{
var task = new TestFastDeploy2 (
CreateAdbResult (1, "Failure [INSTALL_FAILED_ALREADY_EXISTS]"),
CreateAdbResult (1, "adb: failed to install app.apk: cmd: Failure calling service package: Broken pipe (32)"),
CreateAdbResult (0, "Success"));

await task.InstallApkWithRetry ("app.apk", reinstall: false, testOnly: false, user: "");

Assert.AreEqual (3, task.InstallAttempts);
Assert.AreEqual (1, task.UninstallAttempts);
Assert.AreEqual (1, task.RecoveryAttempts);
}

[Test]
public void FastDeploy2DoesNotRetryTransientInstallTwiceAcrossUninstall ()
{
var task = new TestFastDeploy2 (
CreateAdbResult (1, "first failure: device offline"),
CreateAdbResult (1, "Failure [INSTALL_FAILED_ALREADY_EXISTS]"),
CreateAdbResult (1, "third failure: cmd: Failure calling service package: Broken pipe (32)"));

var exception = Assert.ThrowsAsync<FastDeployInstallException> (
async () => await task.InstallApkWithRetry ("app.apk", reinstall: false, testOnly: false, user: ""));

Assert.IsNotNull (exception);
Assert.AreEqual ("ADB0010", exception.ErrorCode);
Assert.That (exception.Message, Does.Contain ("Install attempt 1"));
Assert.That (exception.Message, Does.Contain ("first failure"));
Assert.That (exception.Message, Does.Contain ("Install attempt 2"));
Assert.That (exception.Message, Does.Contain ("INSTALL_FAILED_ALREADY_EXISTS"));
Assert.That (exception.Message, Does.Contain ("Install attempt 3"));
Assert.That (exception.Message, Does.Contain ("third failure"));
Assert.That (exception.Message, Does.Not.Contain ("Install attempt 4"));
Assert.AreEqual (3, task.InstallAttempts);
Assert.AreEqual (1, task.UninstallAttempts);
Assert.AreEqual (1, task.RecoveryAttempts);
}

[Test]
public void FastDeploy2DoesNotRetrySemanticInstallFailure ()
{
var task = new TestFastDeploy2 (
CreateAdbResult (1, "Failure [INSTALL_FAILED_INSUFFICIENT_STORAGE]"));

var exception = Assert.ThrowsAsync<FastDeployInstallException> (
async () => await task.InstallApkWithRetry ("app.apk", reinstall: false, testOnly: false, user: ""));

Assert.IsNotNull (exception);
Assert.AreEqual ("ADB0060", exception.ErrorCode);
Assert.AreEqual (1, task.InstallAttempts);
Assert.AreEqual (0, task.RecoveryAttempts);
}

[Test]
public void FastDeploy2PreservesBothTransientInstallAttempts ()
{
var task = new TestFastDeploy2 (
CreateAdbResult (1, "first failure: cmd: Failure calling service package: Broken pipe (32)"),
CreateAdbResult (1, "second failure: device offline"));

var exception = Assert.ThrowsAsync<FastDeployInstallException> (
async () => await task.InstallApkWithRetry ("app.apk", reinstall: false, testOnly: false, user: ""));

Assert.IsNotNull (exception);
Assert.AreEqual ("ADB0010", exception.ErrorCode);
Assert.That (exception.Message, Does.Contain ("Install attempt 1"));
Assert.That (exception.Message, Does.Contain ("first failure"));
Assert.That (exception.Message, Does.Contain ("ADB transport recovery"));
Assert.That (exception.Message, Does.Contain ("Install attempt 2"));
Assert.That (exception.Message, Does.Contain ("second failure"));
Assert.AreEqual (2, task.InstallAttempts);
Assert.AreEqual (1, task.RecoveryAttempts);
}

[Test]
public void FastDeploy2PreservesOriginalFailureWhenRecoveryFails ()
{
var task = new TestFastDeploy2 (
new InvalidOperationException ("package manager still unavailable"),
CreateAdbResult (1, "cmd: Failure calling service package: Broken pipe (32)"));

var exception = Assert.ThrowsAsync<FastDeployInstallException> (
async () => await task.InstallApkWithRetry ("app.apk", reinstall: false, testOnly: false, user: ""));

Assert.IsNotNull (exception);
Assert.That (exception.Message, Does.Contain ("Failure calling service package: Broken pipe (32)"));
Assert.That (exception.Message, Does.Contain ("package manager still unavailable"));
Assert.AreEqual (1, task.InstallAttempts);
Assert.AreEqual (1, task.RecoveryAttempts);
}

static FastDeploy2.AdbCommandResult CreateAdbResult (int exitCode, string output)
{
return new FastDeploy2.AdbCommandResult {
ExitCode = exitCode,
StandardOutput = output,
StandardError = "",
};
}

sealed class TestFastDeploy2 : FastDeploy2
{
readonly Queue<AdbCommandResult> results;
readonly Exception recoveryException;

public int InstallAttempts { get; private set; }
public int UninstallAttempts { get; private set; }
public int RecoveryAttempts { get; private set; }

public TestFastDeploy2 (params AdbCommandResult [] results)
: this (recoveryException: null, results)
{
}

public TestFastDeploy2 (Exception recoveryException, params AdbCommandResult [] results)
{
this.results = new Queue<AdbCommandResult> (results);
this.recoveryException = recoveryException;
BuildEngine = new MockBuildEngine (TestContext.Out);
PackageName = "com.example.app";
}

internal override Task<AdbCommandResult> RunInstallCommand (string apkFile, bool reinstall, bool testOnly, string user)
{
InstallAttempts++;
return Task.FromResult (results.Dequeue ());
}

internal override Task UninstallPackage (string packageName, bool preserveData, string user)
{
UninstallAttempts++;
return Task.CompletedTask;
}

internal override Task WaitForInstallTransportRecovery ()
{
RecoveryAttempts++;
return recoveryException == null ? Task.CompletedTask : Task.FromException (recoveryException);
}
}

}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Xamarin.Android.Tools.AdbRunner.AdbRunner(string! adbPath, System.Collections.Ge
virtual Xamarin.Android.Tools.AdbRunner.ListDevicesAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyList<Xamarin.Android.Tools.AdbDeviceInfo!>!>!
Xamarin.Android.Tools.AdbRunner.StopEmulatorAsync(string! serial, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!
Xamarin.Android.Tools.AdbRunner.WaitForDeviceAsync(string? serial = null, System.TimeSpan? timeout = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!
Xamarin.Android.Tools.AdbRunner.WaitForPackageManagerAsync(string? serial = null, System.TimeSpan? timeout = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!
Xamarin.Android.Tools.ChecksumType
Xamarin.Android.Tools.ChecksumType.Sha1 = 0 -> Xamarin.Android.Tools.ChecksumType
Xamarin.Android.Tools.ChecksumType.Sha256 = 1 -> Xamarin.Android.Tools.ChecksumType
Expand Down
Loading
Loading