Skip to content
This repository was archived by the owner on Jul 13, 2026. It is now read-only.
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
25 changes: 23 additions & 2 deletions src/Xamarin.Android.Tools.AndroidSdk/Runners/EmulatorRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -57,7 +58,22 @@ public Process LaunchEmulator (string avdName, bool coldBoot = false, List<strin
if (additionalArgs != null)
args.AddRange (additionalArgs);

var psi = ProcessUtils.CreateProcessStartInfo (emulatorPath, args.ToArray ());
ProcessStartInfo psi;
if (OS.IsWindows) {
psi = ProcessUtils.CreateProcessStartInfo (emulatorPath, args.ToArray ());
} else {
// On Unix, launch through a shell that ignores SIGINT before exec'ing
// the emulator. This prevents Ctrl+C in the parent terminal from killing
// the emulator process. 'trap "" INT' sets SIGINT to SIG_IGN, which POSIX
// guarantees is preserved across exec.
var shellCmd = new StringBuilder ("trap '' INT; exec ");
shellCmd.Append (ShellQuote (emulatorPath));
foreach (var arg in args) {
shellCmd.Append (' ');
shellCmd.Append (ShellQuote (arg));
}
psi = ProcessUtils.CreateProcessStartInfo ("/bin/sh", "-c", shellCmd.ToString ());
Comment thread
jonathanpeppers marked this conversation as resolved.
}

if (environmentVariables != null) {
foreach (var kvp in environmentVariables)
Expand Down Expand Up @@ -218,11 +234,12 @@ public async Task<EmulatorBootResult> BootEmulatorAsync (
// Detect early process exit for fast failure
if (emulatorProcess.HasExited && !processExitedWithZero) {
if (emulatorProcess.ExitCode != 0) {
int exitCode = emulatorProcess.ExitCode;
Comment thread
jonathanpeppers marked this conversation as resolved.
Outdated
emulatorProcess.Dispose ();
return new EmulatorBootResult {
Success = false,
ErrorKind = EmulatorBootErrorKind.LaunchFailed,
ErrorMessage = $"Emulator process for '{deviceOrAvdName}' exited with code {emulatorProcess.ExitCode} before becoming available.",
ErrorMessage = $"Emulator process for '{deviceOrAvdName}' exited with code {exitCode} before becoming available.",
};
}
// Exit code 0: emulator likely forked (common on macOS).
Expand Down Expand Up @@ -308,5 +325,9 @@ async Task<EmulatorBootResult> WaitForFullBootAsync (
cancellationToken.ThrowIfCancellationRequested ();
return new EmulatorBootResult { Success = false, ErrorKind = EmulatorBootErrorKind.Cancelled, ErrorMessage = "Boot cancelled." };
}

/// Quotes a string for safe use in a POSIX shell command.
/// Wraps in single quotes and escapes embedded single quotes.
static string ShellQuote (string arg) => "'" + arg.Replace ("'", "'\\''") + "'";
}

Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public async Task LaunchFailure_ReturnsError ()
var result = await runner.BootEmulatorAsync ("Pixel_7_API_35", mockAdb, options);

Assert.IsFalse (result.Success);
Assert.That (result.ErrorMessage, Does.Contain ("Failed to launch"));
Assert.AreEqual (EmulatorBootErrorKind.LaunchFailed, result.ErrorKind);
}

[Test]
Expand Down Expand Up @@ -482,6 +482,68 @@ public void BootEmulatorAsync_EmptyDeviceName_Throws ()

// --- Helpers ---

[Test]
[Platform ("Linux,MacOsX")]
public void LaunchEmulator_SurvivesSigint ()
Comment thread
jonathanpeppers marked this conversation as resolved.
Comment on lines +479 to +481

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test passes on macOS:

Image

{
Comment thread
jonathanpeppers marked this conversation as resolved.
Outdated
// Verify that the emulator process launched by LaunchEmulator ignores
// SIGINT (Ctrl+C) so it is not killed when the parent receives the signal.
var (tempDir, emuPath) = CreateFakeEmulatorSdk ();
try {
var runner = new EmulatorRunner (emuPath);
using var process = runner.LaunchEmulator ("TestAVD");

Assert.IsFalse (process.HasExited, "Process should be running after launch");

// Send SIGINT to the emulator process
var killPsi = ProcessUtils.CreateProcessStartInfo ("kill", "-INT", process.Id.ToString ());
using var kill = new Process { StartInfo = killPsi };
kill.Start ();
kill.WaitForExit (5000);
Comment thread
jonathanpeppers marked this conversation as resolved.
Outdated

// Give the signal a moment to be delivered
Thread.Sleep (500);

Assert.IsFalse (process.HasExited, "Emulator process should survive SIGINT");

process.Kill ();
process.WaitForExit (5000);
} finally {
Directory.Delete (tempDir, true);
}
}

[Test]
[Platform ("Linux,MacOsX")]
public void ShellQuote_EscapesSingleQuotes ()
{
// Verify that paths with special characters are handled correctly
// by launching a fake emulator with a path containing a single quote.
var tempDir = Path.Combine (Path.GetTempPath (), $"emu-quote-test-{Path.GetRandomFileName ()}");
var emulatorDir = Path.Combine (tempDir, "emu'dir");
Directory.CreateDirectory (emulatorDir);

var emuPath = Path.Combine (emulatorDir, "emulator");
File.WriteAllText (emuPath, "#!/bin/sh\nsleep 60\n");
var psi = ProcessUtils.CreateProcessStartInfo ("chmod", "+x", emuPath);
using (var chmod = new Process { StartInfo = psi }) {
chmod.Start ();
chmod.WaitForExit ();
Comment thread
jonathanpeppers marked this conversation as resolved.
Outdated
}

try {
var runner = new EmulatorRunner (emuPath);
using var process = runner.LaunchEmulator ("TestAVD");

Assert.IsFalse (process.HasExited, "Process should start even with single-quote in path");

process.Kill ();
process.WaitForExit (5000);
} finally {
Directory.Delete (tempDir, true);
}
}

static (string tempDir, string emulatorPath) CreateFakeEmulatorSdk ()
{
var tempDir = Path.Combine (Path.GetTempPath (), $"emu-boot-test-{Path.GetRandomFileName ()}");
Expand Down