Skip to content

Commit b54f8cd

Browse files
jonpryorradekdoulik
authored andcommitted
[tests] Kill the emulator in ReleaseAndroidTarget
We've been noticing that emulator-based unit test execution is getting "finicky", e.g. [PR Build dotnet#939][0]: Target AcquireAndroidTarget: Task "CheckAdbTarget" Tool /Users/builder/android-toolchain/sdk/platform-tools/adb execution started with arguments: shell getprop ro.build.version.sdk Environment variables being passed to the tool: 21 Tool /Users/builder/android-toolchain/sdk/platform-tools/adb execution finished. [Output] AdbTarget: [Output] IsValidTarget: True Done executing task "CheckAdbTarget" This means that there is already an Android device attached (emulator or hardware...), as *something* is responding to `adb shell getprop`. However...that attached device is b0rked: Target UndeployUnitTestApks: Task "Adb" Task Adb Arguments: uninstall "Mono.Android_Tests" Tool /Users/builder/android-toolchain/sdk/platform-tools/adb execution started with arguments: uninstall "Mono.Android_Tests" Environment variables being passed to the tool: Error: Could not access the Package Manager. Is the system running? Tool /Users/builder/android-toolchain/sdk/platform-tools/adb execution finished. [Output] Output: Error: Could not access the Package Manager. Is the system running? Done executing task "Adb" *Every* attempt to "do something" with the device -- uninstall and install packages, run applications, etc. -- results in the "Could not access the Package Manager" error. The device is dead, yet still present. We want on-device unit test execution to be *reliable* (7450efc). It isn't, and that's very annoying. Manual investigation of the Jenkins build machine after these errors occur indicates that after `adb TARGET emu kill` executes (as part of `make run-apk-tests`), the `emulator` process *is still running*. Hopefully, that's the problem: the emulator process isn't dead. Fix the problem by *killing* the process, with not-quite-extreme prejudice, by invoking `Process.Kill()` on the `System.Diagnostics.Process` instance that represents `emulator`. (If that doesn't kill it, we'll need to escalate to extreme `kill -9` prejudice.) *For good measure*, run `adb kill-server` within `ReleaseAndroidTarget`, to keep the `adb` daemone from outlasting it's usefulness. [0]: https://jenkins.mono-project.com/view/Xamarin.Android/job/xamarin-android-pr-builder/939/
1 parent b6d2360 commit b54f8cd

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

build-tools/scripts/UnitTestApks.targets

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<UsingTask AssemblyFile="$(MSBuildThisFileDirectory)..\..\bin\Build$(Configuration)\Xamarin.Android.Tools.BootstrapTasks.dll" TaskName="Xamarin.Android.Tools.BootstrapTasks.CreateAndroidEmulator" />
66
<UsingTask AssemblyFile="$(MSBuildThisFileDirectory)..\..\bin\Build$(Configuration)\Xamarin.Android.Tools.BootstrapTasks.dll" TaskName="Xamarin.Android.Tools.BootstrapTasks.RunInstrumentationTests" />
77
<UsingTask AssemblyFile="$(MSBuildThisFileDirectory)..\..\bin\Build$(Configuration)\Xamarin.Android.Tools.BootstrapTasks.dll" TaskName="Xamarin.Android.Tools.BootstrapTasks.StartAndroidEmulator" />
8+
<UsingTask AssemblyFile="$(MSBuildThisFileDirectory)..\..\bin\Build$(Configuration)\Xamarin.Android.Tools.BootstrapTasks.dll" TaskName="Xamarin.Android.Tools.BootstrapTasks.KillProcess" />
89

910
<PropertyGroup>
1011
<_TestImageName>XamarinAndroidUnitTestRunner</_TestImageName>
@@ -38,7 +39,7 @@
3839
ToolPath="$(EmulatorToolPath)">
3940
<Output TaskParameter="AdbTarget" PropertyName="_AdbTarget" />
4041
<Output TaskParameter="AdbTarget" PropertyName="_EmuTarget" />
41-
<Output TaskParameter="AdbProcess" PropertyName="_EmuProcess" />
42+
<Output TaskParameter="EmulatorProcessId" PropertyName="_EmuPid" />
4243
</StartAndroidEmulator>
4344
<Exec
4445
Condition=" '$(_ValidAdbTarget)' != 'True' "
@@ -69,6 +70,15 @@
6970
ToolExe="$(AdbToolExe)"
7071
ToolPath="$(AdbToolPath)"
7172
/>
73+
<KillProcess
74+
Condition=" '$(_EmuTarget)' != '' "
75+
ProcessId="$(_EmuPid)"
76+
/>
77+
<Adb
78+
Arguments="kill-server"
79+
ToolExe="$(AdbToolExe)"
80+
ToolPath="$(AdbToolPath)"
81+
/>
7282
<Error
7383
Condition="'@(_FailedComponent)' != ''"
7484
Text="Execution of the following components did not complete successfully: @(_FailedComponent->'%(Identity)', ', ')"

src/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<Compile Include="Xamarin.Android.Tools.BootstrapTasks\StartAndroidEmulator.cs" />
5050
<Compile Include="Xamarin.Android.Tools.BootstrapTasks\UnzipDirectoryChildren.cs" />
5151
<Compile Include="Xamarin.Android.Tools.BootstrapTasks\Zip.cs" />
52+
<Compile Include="Xamarin.Android.Tools.BootstrapTasks\KillProcess.cs" />
5253
</ItemGroup>
5354
<ItemGroup>
5455
<ProjectReference Include="$(LibZipSharpSourceFullPath)\libZipSharp.csproj">
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using Microsoft.Build.Framework;
5+
using Microsoft.Build.Utilities;
6+
7+
using Xamarin.Android.BuildTools.PrepTasks;
8+
9+
namespace Xamarin.Android.Tools.BootstrapTasks
10+
{
11+
public class KillProcess : Task
12+
{
13+
[Required]
14+
public int ProcessId {get; set;}
15+
16+
public override bool Execute()
17+
{
18+
Log.LogMessage(MessageImportance.Low, $"Task {nameof(KillProcess)}");
19+
Log.LogMessage(MessageImportance.Low, $" {nameof (ProcessId)}: {ProcessId}");
20+
21+
using (var p = Process.GetProcessById (ProcessId)) {
22+
p.Kill ();
23+
}
24+
25+
return !Log.HasLoggedErrors;
26+
}
27+
}
28+
}

src/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks/StartAndroidEmulator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class StartAndroidEmulator : Task
1414
public string AdbTarget {get; set;}
1515

1616
[Output]
17-
public Process AdbProcess {get; set;}
17+
public int EmulatorProcessId {get; set;}
1818

1919
public string AndroidSdkHome {get; set;}
2020
public string Port {get; set;}
@@ -75,6 +75,7 @@ void Run (string emulator)
7575
StartInfo = psi,
7676
};
7777
p.Start ();
78+
EmulatorProcessId = p.Id;
7879
}
7980
}
8081
}

0 commit comments

Comments
 (0)