Skip to content

Stopping the daemon during a pending Android lease leaves an adb server on port 5038, so Android is unavailable after restart #1200

Closed

Description

@saibotma

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

  1. 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.
  2. While it is booting, run simlock daemon stop.
  3. 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

  1. added
    bug:new
    platform:android
    on Nov 5, 2026
  2. self-assigned this
    on Nov 5, 2026
  3. V3RON commented on Nov 5, 2026

    @V3RON
    Contributor

    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 stop does not wait for, or abort, a lease request that is still provisioning.

    • DaemonServer.#stop (src/daemon/server.ts:647) runs settle and then dispose. settle (src/daemon/main.ts:788) waits for leasing's reclaims and the physical watcher, core.drain (only the warm pool) and shutdownIdle. It does not wait for the acquisition coordinator's provision/boot step.
    • The emulator was still booting, so AndroidDriver.dispose (src/drivers/android/index.ts:520) found no attached emulator and reaped Simlock's adb server. Then state.db closed and the daemon logged Daemon stopped.
    • The boot wait kept polling adb with ANDROID_ADB_SERVER_PORT=5038 for about 54 s more. An adb client with no server running starts one itself, and that is exactly the adb -L tcp:5038 fork-server server you saw, started in the second the daemon stopped. The next start then refuses it as occupied (adb-server.ts:125), which is correct for a server Simlock has no record of.
    • The boot timeout's destroy and commit then failed with database is not open, so the device row stayed in provisioning. That explains the doctor findings and the empty cleanup --dry-run.

    Related:

    For a maintainer to decide:

    1. Should stop drain an in-flight provision (it can take up to the 180 s readiness timeout) or abort it?
    2. Should startup or cleanup handle a provisioning row whose folder is missing? Today nothing removes it.

    No more information is needed from the reporter.

    Written by an agent.

  4. V3RON commented on Nov 5, 2026

    @V3RON
    Contributor

    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".

    pnpm exec vitest run --project unit src/daemon/main.test.ts -t "#1200"
    
    AssertionError: expected [ 'boot started', 'dispose' ] to deeply equal [ 'boot started' ]
     ❯ src/daemon/main.test.ts:1802:26
    

    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's dispose (src/daemon/main.ts:804) disposes the drivers and then closes state.db while a boot is still running. The Android driver stops its adb server because no emulator is attached yet. The boot keeps polling adb -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, destroy hits a closed database ("database is not open"), so the record stays provisioning.

    Simplest fix

    Add leasing.settleAcquisitions(), which waits for every tracked workflow the way beginMaintenance does (lease-acquisition-coordinator.ts:428). Call it in dispose after components.close() (main.ts:802) and before the drivers are disposed. Tried locally: the repro and all src/daemon + src/leasing unit tests pass (1381). Update the Stop section of docs/internal/flows/startup.md.

    Alternatives rejected

    • Wait in settle, before components.close(): deadlocks a request waiting on a component install ("ends a running component install on shutdown…" times out).
    • Abort the boot through a signal: Driver.makeReady takes no signal. That is a contract change to every driver.
    • Stop adb's client from auto-starting a server: adb has no switch for that, and the record is still left behind.

    Risk

    • A stop now waits for a boot (Android: up to 180 s) or a seed making. Check this against the CLI's 300 s stop limit and launchd's exit timeout.
    • A queued waiter must not keep a tracked workflow pending, or the stop hangs.
    • A request that boots during the stop may be granted after daemon.stopping.

    Written by an agent.

  5. removed their assignment
    on Nov 5, 2026
  6. added
    bug:ready
    and removed
    bug:new
    on Nov 5, 2026
  7. self-assigned this
    on Nov 5, 2026
  8. V3RON commented on Nov 5, 2026

    @V3RON
    Contributor

    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.

  9. added a commit that references this issue on Nov 5, 2026
    cc39ec8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug:readyplatform:android

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions