t3code icon indicating copy to clipboard operation
t3code copied to clipboard

[Bug]: device_open succeeds and registers a Device panel session for an Android emulator already claimed by another thread

Open davissears opened this issue 5 days ago • 0 comments

Before submitting

  • [x] I searched existing issues and did not find a duplicate.
  • [x] I included enough detail to reproduce or investigate the problem.

Checked #31303, #32511, #38603, closed #38588, and Discussion #43241. #31303 covers a refused thread running snapshot and press after agent-device open fails. #32511 covers completed or settled threads retaining device sessions. #38603 covers a single session rejecting itself across two working directories. Discussion #43241 was an earlier Ideas question about reserving emulators; this report narrows the scope to the reproducible defect in device_list and device_open during active cross-thread contention.

Drafted by Gemini 4 Neon and filed by GPT-6.2-Sol in T3 Code on behalf of @davissears.

Area

apps/server

Steps to reproduce

Prerequisites: local device host enabled with Agent device access and one booted local Android emulator at hostId: "local", deviceId: "emulator-5554".

  1. In Thread A, call device_open(hostId: "local", deviceId: "emulator-5554") and run the returned bound launcher to acquire the agent-device claim:
    "<thread-A-launcher>" open com.android.settings --json
    "<thread-A-launcher>" session list --json
    
    Confirm that open exits with code 0 and session list shows Thread A holding emulator-5554. Leave Thread A open.
  2. In Thread B while Thread A still holds the claim, run these calls in order:
    • Call device_list(hostId: "local").
    • Call device_open(hostId: "local", deviceId: "emulator-5554").
    • Call device_list(hostId: "local").
    • Run the bound launcher that device_open returned:
      "<thread-B-launcher>" open com.android.settings
      
    • Call device_list(hostId: "local") again after the launcher fails.
  3. In Thread A, call device_close(hostId: "local", deviceId: "emulator-5554", shutdown: false).
  4. In Thread B, run "<thread-B-launcher>" open com.android.settings again.

Expected behavior

When another thread already holds an active agent-device claim on emulator-5554:

  • device_list reports that the emulator is already in use, or device_open rejects the request instead of returning a normal success quickStart.
  • Thread B does not keep a competing, unusable emulator-5554 entry in its Device panel after contention fails.
  • A coordinator or agent learns that the emulator is busy before spending build and launch steps on it.

Actual behavior

  1. Thread B's initial device_list returns emulator-5554 with booted: true and open: [], with no busy or owner indicator.
  2. Thread B's device_open(hostId: "local", deviceId: "emulator-5554") succeeds with isError: false, returns a normal quickStart, binds a new launcher for t3-7f2f61de4e9de235f4cf95e4, and adds {"hostId":"local","deviceId":"emulator-5554"} to Thread B's open list and Device panel.
  3. Thread B's bound launcher immediately fails with exit code 1 and Error (DEVICE_IN_USE): Device is already in use by session "t3-21e0019f12cbf3c977f5be92". Not dispatched.
  4. After the refusal, Thread B's device_list still returns open: [{"hostId":"local","deviceId":"emulator-5554"}], leaving the unusable session open in Thread B's Device panel.
  5. After Thread A calls device_close(hostId: "local", deviceId: "emulator-5554", shutdown: false), DeviceService.close closes Thread A's agent-device session in 254 ms and Thread B's retry exits with code 0.

In main at 5596e749:

  • apps/server/src/mcp/toolkits/device/handlers.ts lines 233 to 249 filter state.sessions to the calling threadId, so device_list hides active sessions held by other threads.
  • apps/server/src/mcp/toolkits/device/handlers.ts lines 252 to 326 call devices.open, devices.agentTarget, and devices.agentLauncher without checking other threads' sessions or agent-device daemon claims.
  • apps/server/src/device/DeviceService.ts lines 850 to 881 append the new session to state.sessions while deduplicating only the calling threadId, and agentLauncher on lines 1198 to 1229 binds a new launcher for any matching session in state.sessions.

Impact

Major degradation or frequent failure

Version or commit

0.1.13-nightly.20261126.5305 (source verified on main at 5596e749)

Environment

macOS 27.2.1, build 26B131, arm64, T3 Code Nightly Desktop 0.1.13-nightly.20261126.5305, local device host, expo-device-hub 0.26.1, agent-device 0.21.41, Android Emulator Android 16.0 on emulator-5554

Logs or stack traces

# Step 1: Thread A (t3-21e0019f12cbf3c977f5be92) acquires the claim
$ "<stateDir>/device/targets/t3-21e0019f12cbf3c977f5be92/agent-device" open com.android.settings --json
{"success":true,"data":{"session":"t3-21e0019f12cbf3c977f5be92","appBundleId":"com.android.settings","id":"emulator-5554","message":"Opened: com.android.settings"}}
# exit code: 0

# Step 2: Thread B calls device_open(hostId: "local", deviceId: "emulator-5554")
# Result: isError=false, bound launcher for t3-7f2f61de4e9de235f4cf95e4
# Thread B device_list(hostId: "local") returns open: [{"hostId":"local","deviceId":"emulator-5554"}]

# Thread B runs its returned launcher
$ "<stateDir>/device/targets/t3-7f2f61de4e9de235f4cf95e4/agent-device" open com.android.settings
Error (DEVICE_IN_USE): Device is already in use by session "t3-21e0019f12cbf3c977f5be92". Not dispatched. Next: The holding session may belong to another agent or user. Run agent-device devices and open another device with a device selector. To wait for the device to free up, run the same open with --wait <ms>. Only if that session is yours: reuse --session t3-21e0019f12cbf3c977f5be92, or run agent-device close --session t3-21e0019f12cbf3c977f5be92 first.
Log: <home>/.agent-device/remote-diagnostics/t3-7f2f61de4e9de235f4cf95e4/requests/974e91244b600563.ndjson
# exit code: 1

# Thread B device_list(hostId: "local") after refusal still returns open: [{"hostId":"local","deviceId":"emulator-5554"}]

# Step 3: Thread A calls device_close(hostId: "local", deviceId: "emulator-5554", shutdown: false)
# DeviceService.close releases t3-21e0019f12cbf3c977f5be92 in agent-device (254 ms, status ok)

# Step 4: Thread B retries its bound launcher
$ "<stateDir>/device/targets/t3-7f2f61de4e9de235f4cf95e4/agent-device" open com.android.settings
Opened: com.android.settings
Session state: <stateDir>/device/agent-device/sessions/t3-7f2f61de4e9de235f4cf95e4
# exit code: 0

Screenshots, recordings, or supporting files

No response

Workaround

Locate the thread that owns the holding t3-<hash> session and call device_close(shutdown: false) from that thread, then retry in Thread B. If abandoning Thread B after DEVICE_IN_USE, call device_close(shutdown: false) in Thread B so the unused Device panel entry is removed.

davissears avatar Nov 26 '26 22:11 davissears