Repository navigation
Description
What happened
I stopped the daemon while an Android lease request was still waiting for its emulator to boot. After starting the daemon again, Android was unavailable:
$ simlock doctor
Unavailable [android] occupied: Refusing to run the android driver: something is already listening on port 5038 and Simlock has no record of starting it, so it will not attach to it. ...
The process on port 5038 was an ordinary adb server, started the second the daemon stopped:
$ lsof -nP -iTCP:5038 -sTCP:LISTEN
adb 74712 <me> 8u IPv4 ... 0t0 TCP 127.0.0.1:5038 (LISTEN)
$ ps -o pid,ppid,lstart,command -p 74712
74712 1 Mon Nov 2 12:17:02 2026 adb -L tcp:5038 fork-server server --reply-fd 4
Nothing else on this Mac uses port 5038, so it looks like Simlock itself started it after its own adb server was already shut down. Android worked again only after I killed that process and restarted the daemon.
The failed device from that lease also stayed behind. Afterwards, simlock doctor keeps reporting registry-device-missing and stalled-transition for it (state provisioning), simlock status counts it as one of three managed Android devices, and simlock cleanup --dry-run returns [].
What you expected
Stopping the daemon during a pending Android lease to leave nothing running on Simlock's adb port, so Android works after the next start, and the failed device not to stay in the registry.
How to reproduce
- Start an Android lease that takes a while to boot:
simlock lease --platform android --device "Pixel 9" --detach --ttl 10m --timeout 4m. Here the boot never finished, because of an emulator that doesn't start (see Android leases time out with no reason when the emulator rejects -crash-report-mode (Emulator 36.1.9) #1199), but a slow first boot may do the same. - While it is booting, run
simlock daemon stop. - Start the daemon again and run
simlock doctor.
It happened once; I haven't tried to reproduce it again.
Environment
- Simlock 0.5.0, Android, worker installed as a launchd service
- macOS 26.6.1 (arm64), Android Emulator 36.1.9, platform-tools 36.0.0
Logs and evidence
Daemon log around the stop. The daemon stopped at 12:17:02.8; the pending lease failed about a minute later:
{"timestamp":1793618222792,"level":"info","module":"daemon.server","message":"Daemon stopped","fields":{"reason":"requested"}}
{"timestamp":1793618276697,"level":"warn","module":"daemon.device-provisioner","message":"new device failed to become ready","fields":{"deviceId":"dev_27b91b96-c407-478e-b236-cb8b79df6c33","step":"boot","error":"BootTimeoutError: Timed out waiting for device to boot: simlock_42c32b76-ae8a-439e-9cf3-f4bd27f44d8f"}}
{"timestamp":1793618279248,"level":"warn","module":"daemon.device-provisioner","message":"destroying a device that failed to boot failed","fields":{"deviceId":"dev_27b91b96-c407-478e-b236-cb8b79df6c33","step":"destroy","error":"Error: database is not open"}}
{"timestamp":1793618279248,"level":"error","module":"daemon.server.dispatch","message":"operation","fields":{"operation":"lease.request","role":"admin","durationMs":183371,"code":"INTERNAL","message":"Timed out waiting for device to boot: dev_27b91b96-c407-478e-b236-cb8b79df6c33"}}
The simlock daemon stop command itself printed Daemon stopping and then {"error":{"code":"DAEMON_CONNECTION_LOST","message":"Daemon connection closed"}}, and a simlock daemon start right after it failed with DAEMON_STARTUP_FAILED because the old daemon was still running.
simlock doctor afterwards:
{"findings":[{"kind":"registry-device-missing","deviceId":"dev_27b91b96-c407-478e-b236-cb8b79df6c33","platform":"android"},{"kind":"stalled-transition","deviceId":"dev_27b91b96-c407-478e-b236-cb8b79df6c33","platform":"android","state":"provisioning","enteredAt":1793618096683,"ageMs":930891,"thresholdMs":213000}]}
Activity
V3RON commentedon Nov 5, 2026
Automated triage (first pass). Not a duplicate. Labels kept as they are (
bug:new,platform:android). There is no reproduction test yet, so the full rule 8 report is still to do.Likely cause (read from the code at
f4e1c29, not reproduced):daemon stopdoes not wait for, or abort, a lease request that is still provisioning.DaemonServer.#stop(src/daemon/server.ts:647) runssettleand thendispose.settle(src/daemon/main.ts:788) waits for leasing's reclaims and the physical watcher,core.drain(only the warm pool) andshutdownIdle. It does not wait for the acquisition coordinator's provision/boot step.AndroidDriver.dispose(src/drivers/android/index.ts:520) found no attached emulator and reaped Simlock's adb server. Thenstate.dbclosed and the daemon loggedDaemon stopped.adbwithANDROID_ADB_SERVER_PORT=5038for about 54 s more. An adb client with no server running starts one itself, and that is exactly theadb -L tcp:5038 fork-server serveryou saw, started in the second the daemon stopped. The next start then refuses it asoccupied(adb-server.ts:125), which is correct for a server Simlock has no record of.database is not open, so the device row stayed inprovisioning. That explains the doctor findings and the emptycleanup --dry-run.Related:
database is not openpattern for the warm pool. It was fixed by draining the warm pool before close. This bug is the same gap for lease provisioning.daemon stopnow waits until the daemon has exited, which covers theDAEMON_CONNECTION_LOST/DAEMON_STARTUP_FAILEDpart.For a maintainer to decide:
cleanuphandle aprovisioningrow whose folder is missing? Today nothing removes it.No more information is needed from the reporter.
Written by an agent.
V3RON commentedon Nov 5, 2026
Reproduction
Branch
bug/1200-repro,src/daemon/main.test.ts: "disposes the driver only after a lease request's boot has ended, and deletes the device that failed to boot".Root cause
Defect. The stop never waits for the lease requests the acquisition coordinator is still running (
#activeWorkflows,src/leasing/lease-acquisition-coordinator.ts:233).leasing.settle(src/leasing/create-leasing.ts:275) waits only for the physical watcher and background reclaims. The daemon'sdispose(src/daemon/main.ts:804) disposes the drivers and then closesstate.dbwhile a boot is still running. The Android driver stops its adb server because no emulator is attached yet. The boot keeps pollingadb -s <serial> shell getprop sys.boot_completed(src/drivers/android/emulator.ts:210) on port 5038, and the adb client starts its own server there:adb -L tcp:5038 fork-server server, the process in the report. When the boot fails,destroyhits a closed database ("database is not open"), so the record staysprovisioning.Simplest fix
Add
leasing.settleAcquisitions(), which waits for every tracked workflow the waybeginMaintenancedoes (lease-acquisition-coordinator.ts:428). Call it indisposeaftercomponents.close()(main.ts:802) and before the drivers are disposed. Tried locally: the repro and allsrc/daemon+src/leasingunit tests pass (1381). Update the Stop section ofdocs/internal/flows/startup.md.Alternatives rejected
settle, beforecomponents.close(): deadlocks a request waiting on a component install ("ends a running component install on shutdown…" times out).Driver.makeReadytakes no signal. That is a contract change to every driver.Risk
daemon.stopping.Written by an agent.
refactor(leasing): kill reuses settleAcquisitions (#1200)
fix(leasing): a stop starts no new acquisition while it waits for run…
fix(leasing): move the stop check out of #nextPlan; raise three line …
test: raise main.test.ts's shrink-only limit after merging main (#1200)
refactor(leasing): split the ready-grant branch out of #decide to low…
test: raise lease-acquisition-coordinator.ts's shrink-only limit afte…
V3RON commentedon Nov 5, 2026
Delivery run
Merged: #1317 (#1200)
Parked: none
Waiting: none
Decided: #1200 — what a queued request does when a daemon stop begins → the queue starts no new acquisition; the request ends with the daemon, as before
Written by an agent.
fix(daemon): stop waits for lease requests still booting before it di…