From 526b93d6c8d47333d7f5b036c3a79fe320ad77e1 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 2 Oct 2026 19:00:28 +0200 Subject: [PATCH 1/4] =?UTF-8?q?test:=20reproduce=20#259=20=E2=80=94=20iOS?= =?UTF-8?q?=20removal=20fails=20while=20simctl=20lists=20the=20image=20as?= =?UTF-8?q?=20Deleting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/drivers/ios/index.test.ts | 93 ++++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 2 deletions(-) diff --git a/src/drivers/ios/index.test.ts b/src/drivers/ios/index.test.ts index 3e975b48c..f5a754db7 100644 --- a/src/drivers/ios/index.test.ts +++ b/src/drivers/ios/index.test.ts @@ -3179,7 +3179,7 @@ describe("IosSimctlDriver listComponents()", () => { }; } - function imagesListed(images: readonly Image[]) { + function imagesListed(images: readonly Image[], state = "Ready") { const stdout = JSON.stringify( Object.fromEntries( images.map((entry) => [ @@ -3190,7 +3190,7 @@ describe("IosSimctlDriver listComponents()", () => { platformIdentifier: entry.platform ?? "com.apple.platform.iphonesimulator", runtimeIdentifier: entry.runtime, sizeBytes: entry.sizeBytes ?? 1_000, - state: "Ready", + state, version: entry.version, }, ]), @@ -3731,6 +3731,43 @@ describe("IosSimctlDriver listComponents()", () => { ); }); + it("answers the removal with its size and residue when simctl runtime list shows the image as Deleting after simctl runtime delete returns and drops it seconds later (#259)", async () => { + // As seen on Xcode 26.4.1: `simctl runtime delete` exits while `simctl runtime list` + // still lists the image, in state `Deleting`, for about eight seconds. The runner answers + // by the fake clock rather than by call order, so it does not dictate how a fix waits. + const clock = new FakeClock(); + const deletingForMs = 8_000; + const runner = new ClockedSimctl259(clock, deletingForMs, { + defaultSet: defaultSetListed({ [target.runtime]: { unused: 2 } }), + deleted, + gone: imagesListed([]), + ready: imagesListed([target]), + deleting: imagesListed([target], "Deleting"), + }); + const driver = await createDriver(runner, clock, await assetStore([])); + + let settled = false; + const removal = driver + .removeComponent(receipt, { signal: signal() }) + .catch((caught: unknown) => caught) + .finally(() => { + settled = true; + }); + // Lets time pass in one-second steps, well inside the five-minute removal budget. + for (let second = 0; second < 30 && !settled; second += 1) { + await flushMicrotasks221(); + clock.advance(1_000); + } + + expect(await settledValue(removal)).toEqual({ + residue: + "2 never-used simulators in the default device set are now unavailable; Simlock does " + + "not delete simulators there -- `xcrun simctl delete unavailable` clears them", + sizeBytes: 7_000_000_000, + }); + expect(runner.deletes).toBe(1); + }); + it("ends simctl runtime delete after five minutes, SIGTERM then SIGKILL, and answers only once it has exited", async () => { // Ignores SIGTERM: only the SIGKILL ten seconds later ends it. const stubborn = { hangs: true, ignoresSigterm: true, match: deleteInvocation }; @@ -3788,6 +3825,58 @@ describe("IosSimctlDriver listComponents()", () => { }); }); +type ScriptedStep259 = ConstructorParameters[0][number]; + +/** + * A scripted `simctl` that answers by state rather than by call order (#259): `simctl runtime + * list -j` answers `ready` until `simctl runtime delete` has run, then `deleting` until + * `deletingForMs` of fake-clock time has passed since, then `gone`. The default set's listing + * always answers `defaultSet`. Every call is recorded in `calls`, as the scripted runner does. + */ +class ClockedSimctl259 extends ScriptedProcessRunner { + deletes = 0; + #deletedAt: number | undefined; + + constructor( + private readonly clock: FakeClock, + private readonly deletingForMs: number, + private readonly steps: { + readonly ready: ScriptedStep259; + readonly deleting: ScriptedStep259; + readonly gone: ScriptedStep259; + readonly deleted: ScriptedStep259; + readonly defaultSet: ScriptedStep259; + }, + ) { + super([]); + } + + override spawn(...call: Parameters) { + const [command, args] = call; + const argv = args.join(" "); + let step: ScriptedStep259; + if (argv.endsWith("runtime delete IMG-26.4-23E244")) { + this.deletes += 1; + this.#deletedAt = this.clock.now(); + step = this.steps.deleted; + } else if (argv.endsWith("runtime list -j")) { + step = + this.#deletedAt === undefined + ? this.steps.ready + : this.clock.now() - this.#deletedAt < this.deletingForMs + ? this.steps.deleting + : this.steps.gone; + } else if (argv === "simctl list -j devices") { + step = this.steps.defaultSet; + } else { + throw new Error(`Unexpected process invocation: ${command} ${argv}`); + } + const handle = new ScriptedProcessRunner([step]).spawn(...call); + this.calls.push({ args: [...args], command, options: call[2] ?? {} }); + return handle; + } +} + /** Lets queued continuations run, so a test can see that something has not happened yet. */ async function flushMicrotasks221(): Promise { for (let count = 0; count < 100; count += 1) await Promise.resolve(); From 6cdf43ba1f758f424419e483df60370a3f228cf3 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 2 Oct 2026 19:11:03 +0200 Subject: [PATCH 2/4] fix(ios): wait while simctl lists a removed runtime as Deleting before verifying the removal --- docs/CLI.md | 5 +- src/core/driver.ts | 6 ++- src/drivers/ios/index.test.ts | 92 ++++++++++++++++++++++++++++++++++- src/drivers/ios/index.ts | 75 +++++++++++++++++++++++++++- 4 files changed, 172 insertions(+), 6 deletions(-) diff --git a/docs/CLI.md b/docs/CLI.md index e6c130681..c1599688f 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -1451,7 +1451,10 @@ every one of them and removes nothing. waits for it, and no new Simlock device is created on the component being removed: a lease request for it fails with `RUNTIME_MISSING`. -An iOS runtime is removed with `simctl runtime delete`. Two things can stay +An iOS runtime is removed with `simctl runtime delete`. `simctl` can answer +while the runtime is still being deleted, so Simlock waits until it is gone: +at most five minutes from the start of the removal, after which the command +fails and Simlock keeps its record of the runtime. Two things can stay behind, and the result's `residue` names each one that did: - The never-booted simulators of that runtime in Xcode's default device set. diff --git a/src/core/driver.ts b/src/core/driver.ts index 6b5d44c83..4cc652785 100644 --- a/src/core/driver.ts +++ b/src/core/driver.ts @@ -553,7 +553,11 @@ export interface ComponentRemoval { readonly residue?: string; } -/** How long one platform removal (`simctl runtime delete`, `sdkmanager --uninstall`) may run. */ +/** + * How long one platform removal (`simctl runtime delete`, `sdkmanager --uninstall`) may run. On + * iOS it also covers the wait, after `simctl runtime delete` answers, for the image to stop + * being listed as deleting; the budget is measured from the start of the delete. + */ export const COMPONENT_REMOVAL_TIMEOUT_MS = 5 * 60_000; /** diff --git a/src/drivers/ios/index.test.ts b/src/drivers/ios/index.test.ts index f5a754db7..4401f6734 100644 --- a/src/drivers/ios/index.test.ts +++ b/src/drivers/ios/index.test.ts @@ -3478,6 +3478,8 @@ describe("IosSimctlDriver listComponents()", () => { const other = image("18.4", "22E238"); const receipt = { build: "23E244", image: "IMG-26.4-23E244" }; const deleteInvocation = simctl("runtime", "delete", "IMG-26.4-23E244"); + // After a delete the driver lists the images once more to see whether the image is still + // being deleted (#259), then the shared check lists images and the default set again. const deleted = { match: deleteInvocation, result: { code: 0, stderr: "", stdout: "" } }; const signal = () => new AbortController().signal; @@ -3498,6 +3500,7 @@ describe("IosSimctlDriver listComponents()", () => { defaultSetListed({ [other.runtime]: { unused: 4 } }), deleted, imagesListed([other]), + imagesListed([other]), defaultSetListed({ [other.runtime]: { unused: 4 } }), ]); // Other builds' downloads are in the store, one of them of the same version; this one's @@ -3520,6 +3523,7 @@ describe("IosSimctlDriver listComponents()", () => { defaultSetListed({}), deleted, imagesListed([]), + imagesListed([]), defaultSetListed({}), ]); const filesystem = await assetStore([["26.4", "23E244"]]); @@ -3542,6 +3546,7 @@ describe("IosSimctlDriver listComponents()", () => { defaultSetListed({ [target.runtime]: { unused: 11 } }), deleted, imagesListed([]), + imagesListed([]), defaultSetListed({ [target.runtime]: { unused: 11 } }), ]); const driver = await createDriver(runner, new FakeClock(), await assetStore([])); @@ -3558,6 +3563,7 @@ describe("IosSimctlDriver listComponents()", () => { defaultSetListed({ [target.runtime]: { unused: 3 } }), deleted, imagesListed([]), + imagesListed([]), defaultSetListed({ [target.runtime]: { unused: 3 } }), ]); const driver = await createDriver(runner, new FakeClock(), await assetStore([])); @@ -3576,6 +3582,7 @@ describe("IosSimctlDriver listComponents()", () => { defaultSetListed({ [target.runtime]: { unused: 1 } }), deleted, imagesListed([]), + imagesListed([]), defaultSetListed({ [target.runtime]: { unused: 1 } }), ]); const driver = await createDriver(runner, new FakeClock(), await assetStore([])); @@ -3593,6 +3600,7 @@ describe("IosSimctlDriver listComponents()", () => { defaultSetListed({ [target.runtime]: { unused: 2 } }), deleted, imagesListed([]), + imagesListed([]), defaultSetListed({ [target.runtime]: { unused: 2 } }), ]); const driver = await createDriver( @@ -3618,6 +3626,7 @@ describe("IosSimctlDriver listComponents()", () => { defaultSetListed({ [target.runtime]: { unused: 3 } }), deleted, imagesListed([sibling]), + imagesListed([sibling]), defaultSetListed({ [target.runtime]: { unused: 3 } }), ]); const driver = await createDriver(runner, new FakeClock(), await assetStore([])); @@ -3633,6 +3642,7 @@ describe("IosSimctlDriver listComponents()", () => { defaultSetListed({ [target.runtime]: { unused: 11 } }), deleted, imagesListed([]), + imagesListed([]), defaultSetListed({ [target.runtime]: { unused: 11 } }), ]); const driver = await createDriver(runner, new FakeClock(), await assetStore([])); @@ -3647,7 +3657,7 @@ describe("IosSimctlDriver listComponents()", () => { defaultSetInvocation.args, defaultSetInvocation.args, ]); - expect(runner.calls).toHaveLength(5); + expect(runner.calls).toHaveLength(6); }); it("reports the same foreign-device count in listComponents and in removeComponent's refusal for the same default set (#241)", async () => { @@ -3722,6 +3732,7 @@ describe("IosSimctlDriver listComponents()", () => { defaultSetListed({}), deleted, imagesListed([target]), + imagesListed([target]), defaultSetListed({}), ]); const driver = await createDriver(runner); @@ -3768,6 +3779,74 @@ describe("IosSimctlDriver listComponents()", () => { expect(runner.deletes).toBe(1); }); + it("fails with DriverCrashError five minutes after simctl runtime delete started, not after it answered, when simctl runtime list keeps showing the image as Deleting (#259)", async () => { + // The delete itself takes two minutes, so a budget restarted when it answers would end + // two minutes later than one measured from its start. + const clock = new FakeClock(); + const runner = new ClockedSimctl259( + clock, + Number.POSITIVE_INFINITY, + { + defaultSet: defaultSetListed({}), + deleted, + gone: imagesListed([]), + ready: imagesListed([target]), + deleting: imagesListed([target], "Deleting"), + }, + 120_000, + ); + const driver = await createDriver(runner, clock, await assetStore([])); + + let settledAt: number | undefined; + const removal = driver + .removeComponent(receipt, { signal: signal() }) + .catch((caught: unknown) => caught) + .finally(() => { + settledAt = clock.now(); + }); + for (let second = 0; second < 600 && settledAt === undefined; second += 1) { + await flushMicrotasks221(); + clock.advance(1_000); + } + + const error = await settledValue(removal); + expect(error).toBeInstanceOf(DriverCrashError); + expect(error).toMatchObject({ + message: "ios 26.4 was still being deleted 300000ms after its removal started", + }); + expect(settledAt).toBe((runner.deleteStartedAt ?? Number.NaN) + 300_000); + expect(clock.pendingTimerCount).toBe(0); + }); + + it("fails with DriverCrashError as soon as the signal fires while simctl runtime list shows the image as Deleting, and lists no more (#259)", async () => { + const clock = new FakeClock(); + const runner = new ClockedSimctl259(clock, Number.POSITIVE_INFINITY, { + defaultSet: defaultSetListed({}), + deleted, + gone: imagesListed([]), + ready: imagesListed([target]), + deleting: imagesListed([target], "Deleting"), + }); + const driver = await createDriver(runner, clock, await assetStore([])); + const controller = new AbortController(); + + const removal = driver + .removeComponent(receipt, { signal: controller.signal }) + .catch((caught: unknown) => caught); + await vi.waitFor(() => expect(runner.listsAfterDelete).toBe(1)); + await flushMicrotasks221(); + // The clock never moves: only the signal can end the wait. + controller.abort(); + + const error = await settledValue(removal); + expect(error).toBeInstanceOf(DriverCrashError); + expect(error).toMatchObject({ + message: "The removal of ios 26.4 was ended while it was still being deleted", + }); + expect(runner.listsAfterDelete).toBe(1); + expect(clock.pendingTimerCount).toBe(0); + }); + it("ends simctl runtime delete after five minutes, SIGTERM then SIGKILL, and answers only once it has exited", async () => { // Ignores SIGTERM: only the SIGKILL ten seconds later ends it. const stubborn = { hangs: true, ignoresSigterm: true, match: deleteInvocation }; @@ -3831,10 +3910,15 @@ type ScriptedStep259 = ConstructorParameters[0][nu * A scripted `simctl` that answers by state rather than by call order (#259): `simctl runtime * list -j` answers `ready` until `simctl runtime delete` has run, then `deleting` until * `deletingForMs` of fake-clock time has passed since, then `gone`. The default set's listing - * always answers `defaultSet`. Every call is recorded in `calls`, as the scripted runner does. + * always answers `defaultSet`. `deleteTakesMs` moves the fake clock on while the delete runs, + * as a slow `simctl runtime delete` would. Every call is recorded in `calls`, as the scripted + * runner does, and `deleteStartedAt` and `listsAfterDelete` say when the delete started and how + * often the images were listed after it. */ class ClockedSimctl259 extends ScriptedProcessRunner { deletes = 0; + deleteStartedAt: number | undefined; + listsAfterDelete = 0; #deletedAt: number | undefined; constructor( @@ -3847,6 +3931,7 @@ class ClockedSimctl259 extends ScriptedProcessRunner { readonly deleted: ScriptedStep259; readonly defaultSet: ScriptedStep259; }, + private readonly deleteTakesMs = 0, ) { super([]); } @@ -3857,9 +3942,12 @@ class ClockedSimctl259 extends ScriptedProcessRunner { let step: ScriptedStep259; if (argv.endsWith("runtime delete IMG-26.4-23E244")) { this.deletes += 1; + this.deleteStartedAt = this.clock.now(); + this.clock.advance(this.deleteTakesMs); this.#deletedAt = this.clock.now(); step = this.steps.deleted; } else if (argv.endsWith("runtime list -j")) { + if (this.#deletedAt !== undefined) this.listsAfterDelete += 1; step = this.#deletedAt === undefined ? this.steps.ready diff --git a/src/drivers/ios/index.ts b/src/drivers/ios/index.ts index 0d5fd313b..e7f4b49f9 100644 --- a/src/drivers/ios/index.ts +++ b/src/drivers/ios/index.ts @@ -50,6 +50,9 @@ const COMMAND_TIMEOUT_MS = 30_000; const XCODE_VERSION_TIMEOUT_MS = 15_000; const BOOTSTATUS_TIMEOUT_MS = 120_000; const PROVISION_ESTIMATE_MS = 500; +// How often a removal lists again while `simctl runtime list` shows the deleted image as still +// being deleted, which it does for a few seconds after `simctl runtime delete` answers (#259). +const DELETING_POLL_MS = 1_000; // This driver's word for "the newest runtime Apple offers": a bare `xcodebuild -downloadPlatform // iOS`. Not a version, so `findComponent` never finds it (ADR 0010 ยง2). const LATEST_COMPONENT = "latest"; @@ -557,6 +560,11 @@ export class IosSimctlDriver implements Driver { * unavailable and stay where they are, and `residue` says how many and how to clear them. When * the download is still in the macOS asset store afterwards (#79), `residue` says that too. * Nothing is written to the default set: Simlock never deletes a simulator or a file there. + * + * `simctl runtime delete` can answer while the image is still listed as being deleted (#259), + * so the removal waits for that state to end before it is verified (`#waitWhileDeleting`). + * The delete and the wait share one budget, `COMPONENT_REMOVAL_TIMEOUT_MS` from the start of + * the delete. */ async removeComponent( receipt: ComponentReceipt, @@ -567,7 +575,9 @@ export class IosSimctlDriver implements Driver { platform: this.platform, receipt, remove: async ({ image, unusedDevices, version }) => { + const deadline = this.#clock.now() + COMPONENT_REMOVAL_TIMEOUT_MS; await this.#deleteRuntimeImage(image.identifier, options.signal); + await this.#waitWhileDeleting(image.identifier, version, deadline, options.signal); const sentences = [ unavailableDevicesResidue(unusedDevices), (await this.#downloadResidue(image, version)).residue, @@ -606,6 +616,54 @@ export class IosSimctlDriver implements Driver { this.#assertSuccessful(args, outcome.result); } + /** + * Lists the runtime images every `DELETING_POLL_MS` while the image `identifier` is listed as + * being deleted (`isBeingDeleted`), and answers once it is gone or listed in any other state; + * `removeListedComponent`'s check then decides. Still being deleted at `deadline`, or when + * `signal` fires, is `DriverCrashError`, so the removal fails and the record is kept. + */ + async #waitWhileDeleting( + identifier: string, + version: string, + deadline: number, + signal: AbortSignal, + ): Promise { + for (;;) { + const listed = (await this.#loadRuntimeImages()).find( + (image) => image.identifier === identifier, + ); + if (listed === undefined || !isBeingDeleted(listed)) return; + const remainingMs = deadline - this.#clock.now(); + if (remainingMs <= 0) { + throw new DriverCrashError( + `${this.platform} ${version} was still being deleted ` + + `${String(COMPONENT_REMOVAL_TIMEOUT_MS)}ms after its removal started`, + ); + } + if (!(await this.#pause(Math.min(DELETING_POLL_MS, remainingMs), signal))) { + throw new DriverCrashError( + `The removal of ${this.platform} ${version} was ended while it was still being deleted`, + ); + } + } + } + + /** Answers `true` after `delayMs` on the clock, or `false` as soon as `signal` fires. */ + #pause(delayMs: number, signal: AbortSignal): Promise { + if (signal.aborted) return Promise.resolve(false); + return new Promise((resolve) => { + const onAbort = (): void => { + this.#clock.cancel(timer); + resolve(false); + }; + const timer = this.#clock.setTimer(delayMs, () => { + signal.removeEventListener("abort", onAbort); + resolve(true); + }); + signal.addEventListener("abort", onAbort, { once: true }); + }); + } + /** * What `removeComponent` reports when the removed image's download is still in the asset * store: matched by build, or by version for an image that names no build. @@ -1609,8 +1667,9 @@ export class IosSimctlDriver implements Driver { /** * The runtime images `simctl runtime list -j` reports, one per downloaded disk image. Read - * only for receipts: the image identifier is what changes when a runtime is deleted and - * downloaded again, even at the same version and build. + * for receipts -- the image identifier is what changes when a runtime is deleted and + * downloaded again, even at the same version and build -- and, after a removal, for whether + * the image is still being deleted. */ async #loadRuntimeImages(): Promise { const result = await this.#simctl(["runtime", "list", "-j"], COMMAND_TIMEOUT_MS); @@ -2022,6 +2081,17 @@ interface RuntimeImage { readonly platformIdentifier?: string; readonly version?: string; readonly sizeBytes?: number; + /** What the image is doing: `Ready` once installed, and a deleting state after a delete. */ + readonly state?: string; +} + +/** + * Whether `simctl runtime list -j` shows the image as still being deleted, which it does for a + * few seconds after `simctl runtime delete` answers (#259). The text listing prints + * `(Deleting)`; the JSON value has not been captured mid-delete, so it is compared ignoring case. + */ +function isBeingDeleted(image: RuntimeImage): boolean { + return image.state?.toLowerCase() === "deleting"; } /** The `platformIdentifier` `simctl runtime list -j` gives an iOS simulator runtime image. */ @@ -2043,6 +2113,7 @@ function parseRuntimeImages(value: unknown): readonly RuntimeImage[] { ...optionalString("build", entry.build), ...optionalString("platformIdentifier", entry.platformIdentifier), ...optionalString("version", entry.version), + ...optionalString("state", entry.state), ...(isByteCount(entry.sizeBytes) ? { sizeBytes: entry.sizeBytes } : {}), }, ] From e3b321dca7d8cb5ba909bbad4134d3be5d3679e9 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 2 Oct 2026 19:18:11 +0200 Subject: [PATCH 3/4] test(ios): pin the one-second poll, the capped last pause, abort cleanup and the case-insensitive state --- docs/CLI.md | 4 +- src/drivers/ios/index.test.ts | 114 ++++++++++++++++++++++++++++++++-- src/drivers/ios/index.ts | 3 +- 3 files changed, 113 insertions(+), 8 deletions(-) diff --git a/docs/CLI.md b/docs/CLI.md index c1599688f..af06b566f 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -1452,8 +1452,8 @@ every one of them and removes nothing. removed: a lease request for it fails with `RUNTIME_MISSING`. An iOS runtime is removed with `simctl runtime delete`. `simctl` can answer -while the runtime is still being deleted, so Simlock waits until it is gone: -at most five minutes from the start of the removal, after which the command +while the runtime is still being deleted, so Simlock waits until it is gone. +If it is not gone about five minutes after the removal started, the command fails and Simlock keeps its record of the runtime. Two things can stay behind, and the result's `residue` names each one that did: diff --git a/src/drivers/ios/index.test.ts b/src/drivers/ios/index.test.ts index 4401f6734..9e5329656 100644 --- a/src/drivers/ios/index.test.ts +++ b/src/drivers/ios/index.test.ts @@ -3793,7 +3793,8 @@ describe("IosSimctlDriver listComponents()", () => { ready: imagesListed([target]), deleting: imagesListed([target], "Deleting"), }, - 120_000, + // Half a second off the one-second polls, so the last pause must be cut short. + 120_500, ); const driver = await createDriver(runner, clock, await assetStore([])); @@ -3804,9 +3805,10 @@ describe("IosSimctlDriver listComponents()", () => { .finally(() => { settledAt = clock.now(); }); - for (let second = 0; second < 600 && settledAt === undefined; second += 1) { + // Half-second steps land on the deadline itself, so the time it fails at is exact. + for (let step = 0; step < 1_200 && settledAt === undefined; step += 1) { await flushMicrotasks221(); - clock.advance(1_000); + clock.advance(500); } const error = await settledValue(removal); @@ -3847,6 +3849,104 @@ describe("IosSimctlDriver listComponents()", () => { expect(clock.pendingTimerCount).toBe(0); }); + it("lists the images again every second while simctl runtime list shows the image as Deleting, and leaves no abort listener behind (#259)", async () => { + const clock = new FakeClock(); + const runner = new ClockedSimctl259(clock, 3_500, { + defaultSet: defaultSetListed({}), + deleted, + gone: imagesListed([]), + ready: imagesListed([target]), + deleting: imagesListed([target], "Deleting"), + }); + const driver = await createDriver(runner, clock, await assetStore([])); + const controller = new AbortController(); + const added = vi.spyOn(controller.signal, "addEventListener"); + const removed = vi.spyOn(controller.signal, "removeEventListener"); + + const removal = driver.removeComponent(receipt, { signal: controller.signal }); + await vi.waitFor(() => expect(runner.listsAfterDelete).toBe(1)); + for (const expected of [2, 3, 4]) { + await flushMicrotasks221(); + clock.advance(999); + await flushMicrotasks221(); + expect(runner.listsAfterDelete).toBe(expected - 1); + clock.advance(1); + await vi.waitFor(() => expect(runner.listsAfterDelete).toBe(expected)); + } + // Gone at 3.5 s: the listing at 4 s ends the wait. + await flushMicrotasks221(); + clock.advance(1_000); + await expect(removal).resolves.toEqual({ sizeBytes: 7_000_000_000 }); + expect(added.mock.calls.length).toBeGreaterThan(1); + expect(removed.mock.calls.map(([, listener]) => listener)).toEqual( + expect.arrayContaining(added.mock.calls.map(([, listener]) => listener)), + ); + }); + + it.each(["deleting", "DELETING"])( + "waits while simctl runtime list shows the image in state %s, the state compared ignoring case (#259)", + async (state) => { + const clock = new FakeClock(); + const runner = new ClockedSimctl259(clock, 2_000, { + defaultSet: defaultSetListed({}), + deleted, + gone: imagesListed([]), + ready: imagesListed([target]), + deleting: imagesListed([target], state), + }); + const driver = await createDriver(runner, clock, await assetStore([])); + + let settled = false; + const removal = driver + .removeComponent(receipt, { signal: signal() }) + .catch((caught: unknown) => caught) + .finally(() => { + settled = true; + }); + for (let second = 0; second < 30 && !settled; second += 1) { + await flushMicrotasks221(); + clock.advance(1_000); + } + + expect(await settledValue(removal)).toEqual({ sizeBytes: 7_000_000_000 }); + }, + ); + + it("fails with DriverCrashError when the signal fires while simctl runtime list is answering that the image is Deleting, and lists no more (#259)", async () => { + const clock = new FakeClock(); + const controller = new AbortController(); + const runner = new ClockedSimctl259( + clock, + Number.POSITIVE_INFINITY, + { + defaultSet: defaultSetListed({}), + deleted, + gone: imagesListed([]), + ready: imagesListed([target]), + deleting: imagesListed([target], "Deleting"), + }, + 0, + () => { + controller.abort(); + }, + ); + const driver = await createDriver(runner, clock, await assetStore([])); + + // The clock never moves: only the signal, already fired when the pause starts, ends it. + const error = await settledValue( + driver + .removeComponent(receipt, { signal: controller.signal }) + .catch((caught: unknown) => caught), + ); + + expect(error).toBeInstanceOf(DriverCrashError); + expect(error).toMatchObject({ + message: "The removal of ios 26.4 was ended while it was still being deleted", + }); + expect(runner.listsAfterDelete).toBe(1); + expect(clock.pendingTimerCount).toBe(0); + }); + it("ends simctl runtime delete after five minutes, SIGTERM then SIGKILL, and answers only once it has exited", async () => { // Ignores SIGTERM: only the SIGKILL ten seconds later ends it. const stubborn = { hangs: true, ignoresSigterm: true, match: deleteInvocation }; @@ -3913,7 +4013,7 @@ type ScriptedStep259 = ConstructorParameters[0][nu * always answers `defaultSet`. `deleteTakesMs` moves the fake clock on while the delete runs, * as a slow `simctl runtime delete` would. Every call is recorded in `calls`, as the scripted * runner does, and `deleteStartedAt` and `listsAfterDelete` say when the delete started and how - * often the images were listed after it. + * often the images were listed after it; `onListAfterDelete` runs at each of those listings. */ class ClockedSimctl259 extends ScriptedProcessRunner { deletes = 0; @@ -3932,6 +4032,7 @@ class ClockedSimctl259 extends ScriptedProcessRunner { readonly defaultSet: ScriptedStep259; }, private readonly deleteTakesMs = 0, + private readonly onListAfterDelete: () => void = () => undefined, ) { super([]); } @@ -3947,7 +4048,10 @@ class ClockedSimctl259 extends ScriptedProcessRunner { this.#deletedAt = this.clock.now(); step = this.steps.deleted; } else if (argv.endsWith("runtime list -j")) { - if (this.#deletedAt !== undefined) this.listsAfterDelete += 1; + if (this.#deletedAt !== undefined) { + this.listsAfterDelete += 1; + this.onListAfterDelete(); + } step = this.#deletedAt === undefined ? this.steps.ready diff --git a/src/drivers/ios/index.ts b/src/drivers/ios/index.ts index e7f4b49f9..ac6d2d2b1 100644 --- a/src/drivers/ios/index.ts +++ b/src/drivers/ios/index.ts @@ -620,7 +620,8 @@ export class IosSimctlDriver implements Driver { * Lists the runtime images every `DELETING_POLL_MS` while the image `identifier` is listed as * being deleted (`isBeingDeleted`), and answers once it is gone or listed in any other state; * `removeListedComponent`'s check then decides. Still being deleted at `deadline`, or when - * `signal` fires, is `DriverCrashError`, so the removal fails and the record is kept. + * `signal` fires, is `DriverCrashError`, so the removal fails and the record is kept. A listing + * already running is not cut short by either: it ends on its own `COMMAND_TIMEOUT_MS` first. */ async #waitWhileDeleting( identifier: string, From 5fa9d328eaeaf8067e13fbbfe993a77ef5516167 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Fri, 2 Oct 2026 19:28:11 +0200 Subject: [PATCH 4/4] test(ios): the still-listed removal fails without waiting; keep iOS detail out of the core constant --- src/core/driver.ts | 5 ++--- src/drivers/ios/index.test.ts | 9 +++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/core/driver.ts b/src/core/driver.ts index 4cc652785..4bea9d3e9 100644 --- a/src/core/driver.ts +++ b/src/core/driver.ts @@ -554,9 +554,8 @@ export interface ComponentRemoval { } /** - * How long one platform removal (`simctl runtime delete`, `sdkmanager --uninstall`) may run. On - * iOS it also covers the wait, after `simctl runtime delete` answers, for the image to stop - * being listed as deleting; the budget is measured from the start of the delete. + * How long one platform removal (`simctl runtime delete`, `sdkmanager --uninstall`) may run, + * measured from its start. A driver that waits after the tool answers spends the same budget. */ export const COMPONENT_REMOVAL_TIMEOUT_MS = 5 * 60_000; diff --git a/src/drivers/ios/index.test.ts b/src/drivers/ios/index.test.ts index 9e5329656..9d28aacec 100644 --- a/src/drivers/ios/index.test.ts +++ b/src/drivers/ios/index.test.ts @@ -3737,9 +3737,14 @@ describe("IosSimctlDriver listComponents()", () => { ]); const driver = await createDriver(runner); - await expect(driver.removeComponent(receipt, { signal: signal() })).rejects.toThrow( - "ios 26.4 is still installed after it was removed", + // The clock never moves, so a removal that waited would never settle. + const error = await settledValue( + driver.removeComponent(receipt, { signal: signal() }).catch((caught: unknown) => caught), ); + + expect(error).toBeInstanceOf(DriverCrashError); + expect(error).toMatchObject({ message: "ios 26.4 is still installed after it was removed" }); + expect(runner.calls).toHaveLength(6); }); it("answers the removal with its size and residue when simctl runtime list shows the image as Deleting after simctl runtime delete returns and drops it seconds later (#259)", async () => {