diff --git a/src/drivers/android/index.test.ts b/src/drivers/android/index.test.ts index bab4b462f..8aac9a316 100644 --- a/src/drivers/android/index.test.ts +++ b/src/drivers/android/index.test.ts @@ -413,6 +413,42 @@ describe("AndroidDriver", () => { ]); }); + // Another Simlock instance's emulator is announced only to that instance's adb server, so + // its own `adb devices` lists nothing here; only the machine's TCP ports show it (#257). + it.each([ + ["its console port", 5586], + ["the adb port above its console port", 5587], + ])( + "skips a console port when something listens on %s that its own adb server does not list", + async (_, listeningPort) => { + const runner = new ScriptedProcessRunner([ + processResult(binaries.avdmanager, ["list", "device"], pixelDevices), + processResult(binaries.avdmanager, [ + "create", + "avd", + "-n", + "simlock_one", + "-k", + /.+/, + "-d", + "pixel_8", + ]), + processResult(binaries.emulator, ["-version"], "Android emulator version 36.1.9"), + processResult(binaries.adb, ["devices"], "List of devices attached\n"), + ]); + const driver = await createDriver(await androidFilesystem(), runner, { + ids: ["one"], + tcpProbe: new FakeTcpProbe([adbServerPort, listeningPort]), + }); + const spec = { model: "Pixel 8", osVersion: "34", platform: "android" } as const; + await driver.resolveSpec(spec); + + const device = await driver.provision(spec); + + expect(device.driverData).toMatchObject({ avdName: "simlock_one", port: 5588 }); + }, + ); + it("cold boots without loading or automatically saving snapshots", async () => { const harness = await provisionedHarness(); diff --git a/src/drivers/android/index.ts b/src/drivers/android/index.ts index 4a707846d..931e69ac3 100644 --- a/src/drivers/android/index.ts +++ b/src/drivers/android/index.ts @@ -441,6 +441,7 @@ export class AndroidDriver implements Driver { readonly #registrar: AdbRegistrar; readonly #rootOptions: EnsureOwnedRootOptions; readonly #sdk: AndroidSdkPaths; + readonly #tcpProbe: TcpProbe; readonly componentFootprint: { readonly path: string; readonly bytes: number }; private constructor( @@ -468,6 +469,7 @@ export class AndroidDriver implements Driver { this.#registrar = new AdbRegistrar({ serverPort: adbServerPort, tcp: options.tcpProbe }); this.#rootOptions = rootOptions; this.#sdk = sdk; + this.#tcpProbe = options.tcpProbe; // System images land under the SDK root, so that is the volume an install is reserved on. this.componentFootprint = { bytes: ANDROID_SYSTEM_IMAGE_MIN_FREE_BYTES, path: sdk.root }; // Where an AVD Simlock made before it owned a root still sits: the AVD home the user @@ -695,9 +697,7 @@ export class AndroidDriver implements Driver { * has no scanner to rediscover anything -- so every emulator that survived the restart * (which is by design: releasing a lease hands a device to the warm pool) would be * invisible forever. Invisible is worse than gone: `listManaged` reports no process, so - * `doctor` can never call it an orphan and several gigabytes of RSS leak permanently; the - * port allocator derives occupancy from `adb devices` and hands out a console port that is - * already in use, whose emulator then cannot bind and is quarantined for it. + * `doctor` can never call it an orphan and several gigabytes of RSS leak permanently. * * `connect_emulator` is idempotent (adb keys transports by port), a port with nothing on * it is a cheap failed connect, and the range is bounded and Simlock's own -- so this is @@ -803,7 +803,7 @@ export class AndroidDriver implements Driver { } const configHash = await this.#configHash(avdName, image); - const port = await this.#portAllocator.allocate(this.#env()); + const port = await this.#portAllocator.allocate(this.#env(), this.#tcpProbe); const driverData: AndroidDriverData = { avdName, configHash, @@ -1966,12 +1966,25 @@ class PortAllocator { ) {} /** - * The scoped environment is passed per call rather than held, because one allocator is - * shared by every driver on a runner (see `portAllocatorFor`) while the environment - * belongs to one driver instance -- a stored one would go stale the moment a second - * driver appeared and would silently poll the wrong adb server. + * The scoped environment and the probe are passed per call rather than held, because one + * allocator is shared by every driver on a runner (see `portAllocatorFor`) while both + * belong to one driver instance -- a stored environment would go stale the moment a + * second driver appeared and would silently poll the wrong adb server. + * + * `adb devices` answers only for emulators announced to this instance's own server, so a + * port it leaves free is then probed: an emulator another Simlock instance started is in + * neither this process's reservations nor this server's list, but it holds its console + * port and the adb port above it on the machine, and an emulator told to take either + * exits at once. Each probe is one loopback connect, refused at once on a free port and + * bounded by the probe's own timeout otherwise; the walk stops at the first free port, so + * the wait is at most one probe pair per console port in the range. + * + * The probe sees only an emulator that is running now. A port handed out but not booted on + * -- by another instance, or by this one before a restart emptied `#reserved` -- still + * reads free here when its device sits between `provision` and `makeReady` or is shut + * down, and two devices can then reach the same port at boot. */ - async allocate(env: NodeJS.ProcessEnv): Promise { + async allocate(env: NodeJS.ProcessEnv, tcpProbe: TcpProbe): Promise { const previous = this.#lock; let release!: () => void; this.#lock = new Promise((resolve) => { @@ -1985,7 +1998,7 @@ class PortAllocator { } const unavailable = new Set([...this.#reserved, ...portsFromAdbDevices(result.stdout)]); for (let port = PORT_MIN; port <= PORT_MAX; port += 2) { - if (!unavailable.has(port)) { + if (!unavailable.has(port) && !(await consolePortsInUse(tcpProbe, port))) { this.#reserved.add(port); return port; } @@ -2001,6 +2014,15 @@ class PortAllocator { } } +/** True when anything on the machine listens on `consolePort` or on the adb port above it. */ +async function consolePortsInUse(tcpProbe: TcpProbe, consolePort: number): Promise { + const listening = await Promise.all([ + tcpProbe.isListening(consolePort), + tcpProbe.isListening(consolePort + 1), + ]); + return listening.includes(true); +} + class SequentialIdGenerator implements IdGenerator { #next = 1; diff --git a/src/drivers/android/two-instances.test.ts b/src/drivers/android/two-instances.test.ts new file mode 100644 index 000000000..5bf8bef9a --- /dev/null +++ b/src/drivers/android/two-instances.test.ts @@ -0,0 +1,320 @@ +import { describe, expect, it } from "vitest"; + +import { OWNED_ROOT_MARKER_FILE } from "../../core/index.js"; +import { + FakeClock, + FakeProcessSupervisor, + MemoryFilesystem, + type ProcessHandle, + type ProcessResult, + type ProcessRunner, + type ProcessRunOptions, + type TcpProbe, +} from "../../ports/index.js"; +import { AndroidDriver } from "./index.js"; + +// Two Simlock instances on one machine, set up the way CLI.md and CONFIGURATION.md say to: +// a distinct `SIMLOCK_HOME` each, and a distinct `drivers.android.adbServerPort` each. +// They share the SDK, the system images, and the machine's TCP ports. + +const sdk = "/android-sdk"; +const home = "/home/simlock"; +const binaries = { + adb: `${sdk}/platform-tools/adb`, + avdmanager: `${sdk}/cmdline-tools/latest/bin/avdmanager`, + emulator: `${sdk}/emulator/emulator`, + sdkmanager: `${sdk}/cmdline-tools/latest/bin/sdkmanager`, +} as const; +const pixelDevices = `Available devices:\nid: 0 or "pixel_8"\n Name: Pixel 8\n OEM : Google\n`; +const spec = { model: "Pixel 8", osVersion: "34", platform: "android" } as const; + +interface Instance { + readonly adbServerPid: number; + readonly adbServerPort: number; + /** The id of the first AVD this instance creates; its name is `simlock_`. */ + readonly avdId: string; + readonly instanceId: string; + readonly simlockHome: string; +} + +const first: Instance = { + adbServerPid: 4242, + adbServerPort: 5038, + avdId: "first", + instanceId: "instance-first", + simlockHome: `${home}/.simlock-first`, +}; +const second: Instance = { + adbServerPid: 4343, + adbServerPort: 5039, + avdId: "second", + instanceId: "instance-second", + simlockHome: `${home}/.simlock-second`, +}; + +describe("AndroidDriver with two Simlock instances on one machine (#257)", () => { + it("provisions a device on a console port no other instance's emulator is listening on", async () => { + const machine = await Machine.with([first, second]); + + // The second instance is already running when the first boots a device, so nothing has + // announced that emulator to the second instance's server. + const secondDriver = await machine.start(second); + const firstDriver = await machine.start(first); + await firstDriver.resolveSpec(spec); + await firstDriver.makeReady(await firstDriver.provision(spec)); + + await secondDriver.resolveSpec(spec); + const provisioned = await secondDriver.provision(spec); + + expect(provisioned.address).toMatch(/^emulator-\d+$/); + expect( + machine.avdListeningAt(provisioned.address), + `the emulator already listening on ${provisioned.address}, the address the second instance gave its new device`, + ).toBeUndefined(); + }); +}); + +/** + * One machine: the SDK, every emulator running on it, and the adb servers Simlock instances + * run. As much of `avdmanager`, `emulator` and `adb` as provisioning, a first boot and a + * baseline capture use. + * + * What it models is adb's, not Simlock's. Each server holds a transport only for an emulator + * that was announced to it -- the emulator announces itself to the server its + * `ANDROID_ADB_SERVER_PORT` names, and `host:emulator:` announces one to whichever + * server receives it -- because every Simlock server runs with its scanner off (`ADB_EMU=0`). + * `adb devices` and `adb -s` see only that server's transports. An emulator told to take a + * console port another one holds exits at once (`emulator -help`, 36.1.9: "if any of these + * ports is already used, the emulator will fail to start"). A console port and the adb port + * above it are TCP ports on the machine, so a probe sees them whichever instance owns them. + */ +class Machine { + /** Console port -> the AVD whose emulator is listening on it. */ + readonly #listening = new Map(); + /** Console port -> the adb servers holding a transport to the emulator on it. */ + readonly #attached = new Map>(); + readonly #exits = new Map void>(); + #nextPid = 1; + + private constructor( + private readonly filesystem: MemoryFilesystem, + private readonly instances: readonly Instance[], + ) {} + + static async with(instances: readonly Instance[]): Promise { + const filesystem = new MemoryFilesystem(); + for (const binary of Object.values(binaries)) { + await filesystem.mkdirp(binary.slice(0, binary.lastIndexOf("/"))); + await filesystem.writeFileAtomic(binary, "binary"); + } + await filesystem.mkdirp(`${sdk}/system-images/android-34/google_apis/arm64-v8a`); + for (const instance of instances) { + // Each instance's own root, created and marked by an earlier run of that instance. + const root = deviceRootOf(instance); + await filesystem.mkdirp(root); + await filesystem.writeFileAtomic( + `${root}/${OWNED_ROOT_MARKER_FILE}`, + JSON.stringify({ + instanceId: instance.instanceId, + owner: "simlock", + platform: "android", + schemaVersion: 1, + }), + ); + // Each instance's own adb server, already running, which the driver adopts. + await filesystem.writeFileAtomic( + `${instance.simlockHome}/adb-server.json`, + JSON.stringify({ pid: instance.adbServerPid, port: instance.adbServerPort, startedAt: 1 }), + ); + } + return new Machine(filesystem, instances); + } + + /** Starts one instance's Android driver: a daemon process of its own, so a runner of its own. */ + async start(instance: Instance): Promise { + let nextId = 0; + return AndroidDriver.create({ + clock: new FakeClock(), + driverConfig: { adbServerPort: instance.adbServerPort }, + env: { ANDROID_HOME: sdk }, + filesystem: this.filesystem, + homeDirectory: home, + hostAbi: "arm64-v8a", + idGenerator: { + generate: () => (nextId++ === 0 ? instance.avdId : `${instance.avdId}-${nextId}`), + }, + instanceId: instance.instanceId, + processRunner: this.#runner(), + processSupervisor: new FakeProcessSupervisor([instance.adbServerPid]), + simlockHome: instance.simlockHome, + tcpProbe: this.#tcpProbe(), + }); + } + + /** The AVD whose emulator is listening on `serial`'s console port, or `undefined`. */ + avdListeningAt(serial: string | undefined): string | undefined { + return this.#listening.get(consolePortOf(serial)); + } + + #tcpProbe(): TcpProbe { + return { + isListening: async (port) => + this.instances.some((instance) => instance.adbServerPort === port) || + this.#listening.has(port) || + this.#listening.has(port - 1), + send: async (serverPort, payload) => { + const adbPort = Number(/host:emulator:(\d+)$/.exec(payload)?.[1]); + // adb answers by connecting out to the emulator's adb port; nothing there, no transport. + if (this.#listening.has(adbPort - 1)) this.#attach(adbPort - 1, serverPort); + return ""; + }, + }; + } + + #runner(): ProcessRunner { + return { + run: (command, args, options) => this.#run(command, args, options), + spawn: (command, args, options) => this.#spawn(command, args, options), + spawnStreaming: (command, args) => { + throw new Error(`Unexpected streaming invocation: ${command} ${args.join(" ")}`); + }, + }; + } + + async #run( + command: string, + args: readonly string[], + options: ProcessRunOptions | undefined, + ): Promise { + const result = + command === binaries.avdmanager + ? await this.#avdmanager(args, options) + : command === binaries.adb + ? this.#adb(args, serverPortOf(options)) + : command === binaries.emulator && args[0] === "-version" + ? ok("Android emulator version 36.1.9") + : undefined; + if (result === undefined) { + throw new Error(`Unexpected process invocation: ${command} ${args.join(" ")}`); + } + return result; + } + + async #avdmanager( + args: readonly string[], + options: ProcessRunOptions | undefined, + ): Promise { + if (args[0] === "list") return ok(pixelDevices); + if (args[0] !== "create") return undefined; + const avdHome = options?.env?.["ANDROID_AVD_HOME"]; + if (avdHome === undefined) throw new Error("avdmanager ran without ANDROID_AVD_HOME"); + const avdName = args[args.indexOf("-n") + 1] ?? ""; + await this.filesystem.mkdirp(`${avdHome}/${avdName}.avd`); + await this.filesystem.writeFileAtomic( + `${avdHome}/${avdName}.avd/config.ini`, + "hw.ramSize=2048\n", + ); + return ok(); + } + + #adb(args: readonly string[], serverPort: number): ProcessResult | undefined { + if (args[0] === "devices") { + const lines = [...this.#listening.keys()] + .filter((port) => this.#attached.get(port)?.has(serverPort) === true) + .map((port) => `emulator-${port}\tdevice\n`); + return ok(`List of devices attached\n${lines.join("")}`); + } + if (args[0] !== "-s") return undefined; + const port = consolePortOf(args[1]); + if (this.#attached.get(port)?.has(serverPort) !== true) { + return { code: 1, stderr: `adb: device '${args[1]}' not found`, stdout: "" }; + } + const rest = args.slice(2).join(" "); + if (rest === "emu kill") { + this.#exit(port); + return ok("OK: killing emulator, bye bye\n"); + } + if (rest.startsWith("shell echo ")) return ok(); + const answer = ADB_ANSWERS.get(rest); + return answer === undefined ? undefined : ok(answer); + } + + #spawn( + command: string, + args: readonly string[], + options: ProcessRunOptions | undefined, + ): ProcessHandle { + if (command !== binaries.emulator || args[0] !== "-avd") { + throw new Error(`Unexpected spawn: ${command} ${args.join(" ")}`); + } + const avdName = args[1] ?? ""; + const port = Number(args[args.indexOf("-port") + 1]); + let exit!: (result: ProcessResult) => void; + const exited = new Promise((resolve) => { + exit = resolve; + }); + const bound = !this.#listening.has(port); + if (bound) { + this.#listening.set(port, avdName); + this.#exits.set(port, () => exit(ok())); + // An emulator announces itself to the server its environment names. + this.#attach(port, serverPortOf(options)); + } else { + exit({ code: 1, stderr: `console port ${port} is already used`, stdout: "" }); + } + return { + // Killing an emulator that never bound must not stop the one that did. + kill: () => { + if (bound && this.#listening.get(port) === avdName) this.#exit(port); + }, + pid: this.#nextPid++, + stderr: emptyLines(), + stdout: emptyLines(), + unref: () => undefined, + wait: () => exited, + }; + } + + #attach(consolePort: number, serverPort: number): void { + const servers = this.#attached.get(consolePort) ?? new Set(); + servers.add(serverPort); + this.#attached.set(consolePort, servers); + } + + #exit(port: number): void { + this.#listening.delete(port); + this.#attached.delete(port); + this.#exits.get(port)?.(); + this.#exits.delete(port); + } +} + +/** What a booted emulator answers to the `adb -s ` commands a first boot and a baseline capture send. */ +const ADB_ANSWERS = new Map([ + ["shell getprop sys.boot_completed", "1\n"], + ["shell getprop init.svc.bootanim", "stopped\n"], + ["emu avd snapshot save simlock_clean_baseline", "OK\n"], + ["emu avd snapshot list", "simlock_clean_baseline\n"], +]); + +function deviceRootOf(instance: Instance): string { + return `${instance.simlockHome}/devices/android`; +} + +function serverPortOf(options: ProcessRunOptions | undefined): number { + const port = Number(options?.env?.["ANDROID_ADB_SERVER_PORT"]); + if (!Number.isInteger(port)) throw new Error("adb ran without ANDROID_ADB_SERVER_PORT"); + return port; +} + +function consolePortOf(serial: string | undefined): number { + return Number(/^emulator-(\d+)$/.exec(serial ?? "")?.[1]); +} + +function ok(stdout = ""): ProcessResult { + return { code: 0, stderr: "", stdout }; +} + +async function* emptyLines(): AsyncIterable { + // An emulator Simlock spawns is never read from. +}