[Bug]: device_open succeeds and registers a Device panel session for an Android emulator already claimed by another thread
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".
- In Thread A, call
device_open(hostId: "local", deviceId: "emulator-5554")and run the returned bound launcher to acquire theagent-deviceclaim:
Confirm that"<thread-A-launcher>" open com.android.settings --json "<thread-A-launcher>" session list --jsonopenexits with code 0 andsession listshows Thread A holdingemulator-5554. Leave Thread A open. - 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_openreturned:"<thread-B-launcher>" open com.android.settings - Call
device_list(hostId: "local")again after the launcher fails.
- Call
- In Thread A, call
device_close(hostId: "local", deviceId: "emulator-5554", shutdown: false). - In Thread B, run
"<thread-B-launcher>" open com.android.settingsagain.
Expected behavior
When another thread already holds an active agent-device claim on emulator-5554:
device_listreports that the emulator is already in use, ordevice_openrejects the request instead of returning a normal successquickStart.- Thread B does not keep a competing, unusable
emulator-5554entry 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
- Thread B's initial
device_listreturnsemulator-5554withbooted: trueandopen: [], with no busy or owner indicator. - Thread B's
device_open(hostId: "local", deviceId: "emulator-5554")succeeds withisError: false, returns a normalquickStart, binds a new launcher fort3-7f2f61de4e9de235f4cf95e4, and adds{"hostId":"local","deviceId":"emulator-5554"}to Thread B'sopenlist and Device panel. - 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. - After the refusal, Thread B's
device_liststill returnsopen: [{"hostId":"local","deviceId":"emulator-5554"}], leaving the unusable session open in Thread B's Device panel. - After Thread A calls
device_close(hostId: "local", deviceId: "emulator-5554", shutdown: false),DeviceService.closecloses Thread A'sagent-devicesession in 254 ms and Thread B's retry exits with code 0.
In main at 5596e749:
apps/server/src/mcp/toolkits/device/handlers.tslines 233 to 249 filterstate.sessionsto the callingthreadId, sodevice_listhides active sessions held by other threads.apps/server/src/mcp/toolkits/device/handlers.tslines 252 to 326 calldevices.open,devices.agentTarget, anddevices.agentLauncherwithout checking other threads' sessions oragent-devicedaemon claims.apps/server/src/device/DeviceService.tslines 850 to 881 append the new session tostate.sessionswhile deduplicating only the callingthreadId, andagentLauncheron lines 1198 to 1229 binds a new launcher for any matching session instate.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.