From 1f4d73552e8082ade28f2f664c421e742683286f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 3 Dec 2026 20:28:25 +0000 Subject: [PATCH 1/2] test(devices): close with shutdown after a close without it leaves the device on (#48424) Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/DeviceService.test.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/apps/server/src/device/DeviceService.test.ts b/apps/server/src/device/DeviceService.test.ts index 39a76ab16451e..c87e3d80eb429 100644 --- a/apps/server/src/device/DeviceService.test.ts +++ b/apps/server/src/device/DeviceService.test.ts @@ -1278,6 +1278,24 @@ it.effect("keeps shutdown successful when subsequent discovery fails", () => }), ); +it.effect("shuts a device down when a close with shutdown follows a close without it", () => + Effect.gen(function* () { + const { service, requests } = yield* fixture(Effect.void, undefined, true); + yield* service.configure({ enabled: true }); + const threadId = ThreadId.make("close-then-shutdown"); + const session = yield* service.open({ + threadId, + deviceId: "Pixel_API_35", + platform: "android", + }); + yield* service.close({ threadId, deviceId: session.deviceId, shutdown: false }); + yield* service.close({ threadId, deviceId: session.deviceId, shutdown: true }); + expect(requests.some((url) => url.endsWith("/shutdown"))).toBe(true); + const state = yield* service.state; + expect(state.devices.find((device) => device.id === session.deviceId)?.booted).toBe(false); + }), +); + it.effect.each(["shutdown", "close"] as const)( "%s releases iOS capture and the session's launcher so reopening uses a fresh session", (operation) => From d275eff654ac2e2ca40b9f7e476215a354c89080 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 3 Dec 2026 20:37:28 +0000 Subject: [PATCH 2/2] fix(server): device_close with shutdown after a plain close turns the device off A close without shutdown retires the thread's session, so a later close with shutdown matched nothing and reported success while the device stayed booted and kept its Simlock lease. Close now shuts down the booted device it names when the thread has no session left, and fails when another thread still has that device open. The MCP device_close tool finds the device's host the same way. Fixes #48424 Co-Authored-By: Claude Opus 5.5 --- apps/server/src/device/DeviceService.test.ts | 21 +++++++++++++++ apps/server/src/device/DeviceService.ts | 26 +++++++++++++++++-- .../src/mcp/toolkits/device/handlers.ts | 10 ++++++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/apps/server/src/device/DeviceService.test.ts b/apps/server/src/device/DeviceService.test.ts index c87e3d80eb429..09c9f29c84b39 100644 --- a/apps/server/src/device/DeviceService.test.ts +++ b/apps/server/src/device/DeviceService.test.ts @@ -1296,6 +1296,27 @@ it.effect("shuts a device down when a close with shutdown follows a close withou }), ); +it.effect("refuses to shut down a closed device that another thread still has open", () => + Effect.gen(function* () { + const { service, requests } = yield* fixture(Effect.void, undefined, true); + yield* service.configure({ enabled: true }); + const threadId = ThreadId.make("closed-thread"); + const other = ThreadId.make("other-thread"); + const session = yield* service.open({ + threadId, + deviceId: "Pixel_API_35", + platform: "android", + }); + yield* service.open({ threadId: other, deviceId: session.deviceId, platform: "android" }); + yield* service.close({ threadId, deviceId: session.deviceId }); + const error = yield* Effect.flip( + service.close({ threadId, deviceId: session.deviceId, shutdown: true }), + ); + expect(error._tag).toBe("DeviceHostUnavailableError"); + expect(requests.some((url) => url.endsWith("/shutdown"))).toBe(false); + }), +); + it.effect.each(["shutdown", "close"] as const)( "%s releases iOS capture and the session's launcher so reopening uses a fresh session", (operation) => diff --git a/apps/server/src/device/DeviceService.ts b/apps/server/src/device/DeviceService.ts index 0c508b0118d65..57662b8907ab2 100644 --- a/apps/server/src/device/DeviceService.ts +++ b/apps/server/src/device/DeviceService.ts @@ -978,8 +978,30 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function* } } if (input.shutdown) yield* releaseLaunchers(ending); + // A close without shutdown already retired this thread's session, so turn off the device it names. + const hostId = input.hostId ?? LOCAL_DEVICE_HOST_ID; + const unclaimed = + input.shutdown && input.deviceId !== undefined && closing.length === 0 + ? state.devices + .filter( + (device) => + device.hostId === hostId && device.id === input.deviceId && device.booted, + ) + .map((device) => ({ hostId, deviceId: device.id, platform: device.platform })) + : []; + if ( + unclaimed.length > 0 && + state.sessions.some( + (session) => session.hostId === hostId && session.deviceId === input.deviceId, + ) + ) + return yield* new DeviceHostUnavailableError({ + hostId, + reason: `Device ${input.deviceId} is open in another thread. Close it there first.`, + }); + const shuttingDown = [...closing, ...unclaimed]; if (input.shutdown) { - for (const session of closing) { + for (const session of shuttingDown) { const ready = yield* currentReadiness(session.hostId); if (!ready) return yield* new DeviceHostUnavailableError({ @@ -993,7 +1015,7 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function* ...current, sessions: current.sessions.filter((session) => !ending.includes(session)), })); - return closing; + return shuttingDown; }), ); if (input.shutdown) diff --git a/apps/server/src/mcp/toolkits/device/handlers.ts b/apps/server/src/mcp/toolkits/device/handlers.ts index 0d7dc00f20caa..1c6bc308e459b 100644 --- a/apps/server/src/mcp/toolkits/device/handlers.ts +++ b/apps/server/src/mcp/toolkits/device/handlers.ts @@ -440,7 +440,15 @@ const handlers = { : yield* devices .locateSession({ threadId: scope.thread.threadId, deviceId, hostId: inputHostId }) .pipe(mapLocateErrors); - const hostId = Option.isSome(open) ? open.value.hostId : inputHostId; + const located = + Option.isNone(open) && input.shutdown && deviceId !== undefined + ? yield* devices.locateDevice({ deviceId, hostId: inputHostId }).pipe(mapLocateErrors) + : Option.none(); + const hostId = Option.isSome(open) + ? open.value.hostId + : Option.isSome(located) + ? located.value.hostId + : inputHostId; yield* devices.close({ threadId: scope.thread.threadId, ...(hostId === undefined ? {} : { hostId }),