From 92f671a2db5b86d2e5b52c7eefeb39259ebbd5d8 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 2 Oct 2026 17:51:23 +0200 Subject: [PATCH 1/3] =?UTF-8?q?test:=20reproduce=20#237=20=E2=80=94=20the?= =?UTF-8?q?=20Android=20driver=20leaves=20a=20timer=20armed=20after=20a=20?= =?UTF-8?q?shutdown=20sees=20its=20emulator=20exit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/drivers/android/index.test.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/drivers/android/index.test.ts b/src/drivers/android/index.test.ts index 1f34ecfd4..980448dcf 100644 --- a/src/drivers/android/index.test.ts +++ b/src/drivers/android/index.test.ts @@ -456,6 +456,31 @@ describe("AndroidDriver", () => { ); }); + /** + * #237: a timer the driver leaves armed holds the event loop open, so a daemon that logged + * "Daemon stopped" stays alive until it fires. Both shutdowns here -- the one `makeReady` + * does to restart from the new baseline, and the one the test asks for -- see the emulator + * exit at once, so neither has anything left to wait for. + */ + it("leaves no timer armed once each shutdown has seen its emulator exit", async () => { + const harness = await provisionedHarness({ + afterwards: [processResult(binaries.adb, ["-s", "emulator-5586", "emu", "kill"])], + }); + await harness.driver.makeReady(harness.device); + const launch = harness.runner.calls.findLastIndex( + (call) => call.command === binaries.emulator && call.args.includes("-avd"), + ); + const running = harness.runner.handles[launch]; + expect(running, "no running emulator").toBeDefined(); + + const shutdown = harness.driver.shutdown(harness.device); + // The emulator answers `emu kill` by exiting. + running?.kill("SIGTERM"); + await shutdown; + + expect(harness.clock.pendingTimerCount, "timers still armed after shutdown").toBe(0); + }); + it("validates a new clean baseline by restarting from it before becoming ready", async () => { const harness = await provisionedHarness(); @@ -3170,6 +3195,8 @@ async function provisionedHarness( readonly forReclaim?: boolean; readonly initialAdbFailures?: number; readonly readinessTimeoutMs?: number; + /** Invocations the test makes after the harness's own, in order. */ + readonly afterwards?: readonly ScriptedProcessExpectation[]; } = {}, ) { const filesystem = await androidFilesystem({ config: "hw.ramSize=2048\n" }); @@ -3245,6 +3272,7 @@ async function provisionedHarness( ); } + expectations.push(...(options.afterwards ?? [])); const runner = new ScriptedProcessRunner(expectations); const driver = await createDriver(filesystem, runner, { clock, From 61d4c69957eb4a00f95f2a4c93c80718e38a0fb0 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 2 Oct 2026 17:58:25 +0200 Subject: [PATCH 2/3] fix(android): cancel the shutdown wait's timer once the emulator exits The timeout branch of #waitForExit armed a second timer that was never cancelled, so a stopped daemon stayed alive until it fired (up to readinessTimeoutMs, 180 s by default). Keep one timer and cancel it after the race. --- src/drivers/android/index.test.ts | 25 +++++++++++++++++++++++++ src/drivers/android/index.ts | 17 +++++++++-------- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/drivers/android/index.test.ts b/src/drivers/android/index.test.ts index 980448dcf..903dd745a 100644 --- a/src/drivers/android/index.test.ts +++ b/src/drivers/android/index.test.ts @@ -481,6 +481,31 @@ describe("AndroidDriver", () => { expect(harness.clock.pendingTimerCount, "timers still armed after shutdown").toBe(0); }); + it("kills an emulator that has not exited within the readiness timeout of a shutdown", async () => { + const harness = await provisionedHarness({ + afterwards: [processResult(binaries.adb, ["-s", "emulator-5586", "emu", "kill"])], + readinessTimeoutMs: 2_000, + }); + await harness.driver.makeReady(harness.device); + const launch = harness.runner.calls.findLastIndex( + (call) => call.command === binaries.emulator && call.args.includes("-avd"), + ); + const running = harness.runner.handles[launch]; + expect(running, "no running emulator").toBeDefined(); + if (running === undefined) return; + const kill = vi.spyOn(running, "kill"); + + // The emulator ignores `emu kill`: only the timeout can end the wait. + const shutdown = harness.driver.shutdown(harness.device); + await vi.waitFor(() => expect(harness.clock.pendingTimerCount).toBe(1)); + expect(kill).not.toHaveBeenCalled(); + harness.clock.advance(2_000); + await shutdown; + + expect(kill).toHaveBeenCalledWith("SIGKILL"); + expect(harness.clock.pendingTimerCount, "timers still armed after shutdown").toBe(0); + }); + it("validates a new clean baseline by restarting from it before becoming ready", async () => { const harness = await provisionedHarness(); diff --git a/src/drivers/android/index.ts b/src/drivers/android/index.ts index 0cf8acf33..f3ec48610 100644 --- a/src/drivers/android/index.ts +++ b/src/drivers/android/index.ts @@ -47,6 +47,7 @@ import { type ProcessRunner, type ProcessSupervisor, type TcpProbe, + type TimerHandle, } from "../../ports/index.js"; import { AdbRegistrar } from "./adb-registrar.js"; import { AdbServerSupervisor, AdbServerUnavailableError } from "./adb-server.js"; @@ -1858,18 +1859,18 @@ export class AndroidDriver implements Driver { } async #waitForExit(handle: ProcessHandle, timeoutMs: number): Promise { - let timerFired = false; - const timer = this.#clock.setTimer(timeoutMs, () => { - timerFired = true; - }); - const result = await Promise.race([ + let timer: TimerHandle | undefined; + const exited = await Promise.race([ handle.wait().then(() => true), new Promise((resolve) => { - this.#clock.setTimer(timeoutMs, () => resolve(false)); + timer = this.#clock.setTimer(timeoutMs, () => resolve(false)); }), ]); - this.#clock.cancel(timer); - return timerFired ? false : result; + // An armed timer holds the event loop open, so a stopped daemon would outlive it (#237). + if (timer !== undefined) { + this.#clock.cancel(timer); + } + return exited; } async #runOrThrow( From c4b5afa4abb998601fa62bd451c87646c80e26a3 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 2 Oct 2026 18:12:24 +0200 Subject: [PATCH 3/3] fix(android): cancel the shutdown wait's timer when the wait fails too --- src/drivers/android/index.test.ts | 24 +++++++++++++++++++++--- src/drivers/android/index.ts | 22 ++++++++++++---------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/drivers/android/index.test.ts b/src/drivers/android/index.test.ts index 903dd745a..3d7a98fca 100644 --- a/src/drivers/android/index.test.ts +++ b/src/drivers/android/index.test.ts @@ -491,13 +491,13 @@ describe("AndroidDriver", () => { (call) => call.command === binaries.emulator && call.args.includes("-avd"), ); const running = harness.runner.handles[launch]; - expect(running, "no running emulator").toBeDefined(); - if (running === undefined) return; + if (running === undefined) throw new Error("no running emulator"); const kill = vi.spyOn(running, "kill"); // The emulator ignores `emu kill`: only the timeout can end the wait. + const armedBefore = harness.clock.pendingTimerCount; const shutdown = harness.driver.shutdown(harness.device); - await vi.waitFor(() => expect(harness.clock.pendingTimerCount).toBe(1)); + await vi.waitFor(() => expect(harness.clock.pendingTimerCount).toBeGreaterThan(armedBefore)); expect(kill).not.toHaveBeenCalled(); harness.clock.advance(2_000); await shutdown; @@ -506,6 +506,24 @@ describe("AndroidDriver", () => { expect(harness.clock.pendingTimerCount, "timers still armed after shutdown").toBe(0); }); + it("leaves no timer armed when waiting for the emulator to exit fails", async () => { + const harness = await provisionedHarness({ + afterwards: [processResult(binaries.adb, ["-s", "emulator-5586", "emu", "kill"])], + }); + await harness.driver.makeReady(harness.device); + const launch = harness.runner.calls.findLastIndex( + (call) => call.command === binaries.emulator && call.args.includes("-avd"), + ); + const running = harness.runner.handles[launch]; + if (running === undefined) throw new Error("no running emulator"); + // A child that emits `error` rejects its wait instead of resolving it. + vi.spyOn(running, "wait").mockRejectedValue(new Error("spawn error")); + + await expect(harness.driver.shutdown(harness.device)).rejects.toThrow("spawn error"); + + expect(harness.clock.pendingTimerCount, "timers still armed after shutdown").toBe(0); + }); + it("validates a new clean baseline by restarting from it before becoming ready", async () => { const harness = await provisionedHarness(); diff --git a/src/drivers/android/index.ts b/src/drivers/android/index.ts index f3ec48610..4a707846d 100644 --- a/src/drivers/android/index.ts +++ b/src/drivers/android/index.ts @@ -1860,17 +1860,19 @@ export class AndroidDriver implements Driver { async #waitForExit(handle: ProcessHandle, timeoutMs: number): Promise { let timer: TimerHandle | undefined; - const exited = await Promise.race([ - handle.wait().then(() => true), - new Promise((resolve) => { - timer = this.#clock.setTimer(timeoutMs, () => resolve(false)); - }), - ]); - // An armed timer holds the event loop open, so a stopped daemon would outlive it (#237). - if (timer !== undefined) { - this.#clock.cancel(timer); + try { + return await Promise.race([ + handle.wait().then(() => true), + new Promise((resolve) => { + timer = this.#clock.setTimer(timeoutMs, () => resolve(false)); + }), + ]); + } finally { + // An armed timer holds the event loop open, so a stopped daemon would outlive it (#237). + if (timer !== undefined) { + this.#clock.cancel(timer); + } } - return exited; } async #runOrThrow(