Repository navigation
Description
Part of #1189.
Scope
After this PR, a second Simlock worker with real drivers for the same OS user, in another folder, refuses to start and names the worker that holds the machine; it never starts an adb server or sweeps emulator ports. A worker's stop frees the lock, and a crashed worker's lock is taken over. The slow e2e lane refuses to run (exit 69, "machine not fit") while a worker outside the lane holds the lock, and says to stop it. Workers with substituted drivers and gateways are unchanged. The website stops offering a second worker through SIMLOCK_HOME.
All of ADR 0059 in one PR: the lock, its release and the slow lane's check share one file format and one path, and split apart they have no user-visible slice.
In short
A worker with real drivers takes the worker lock, a folder holding owner.json, before it looks for drivers: /tmp/simlock-<uid>/worker.lock/ on macOS and Linux (<uid> from process.getuid()), <os.tmpdir()>\simlock\worker.lock\ on Windows. A second one of the same user in another folder then stops with: another Simlock worker runs for this user (pid <pid>, folder <folder>, since <startedAt>); stop it first: SIMLOCK_HOME=<folder> simlock daemon stop, which the CLI prints as The daemon could not start: <that message>, exit 1. <pid>, <folder> and <startedAt> are the holder's owner.json fields.
flowchart LR
A[startDaemon, worker] --> L["take worker lock (new)"]
L -- held by another folder --> X["fail start (new)"]
L -- ok --> D[discoverDrivers: adb server, sweep]
D --> S[socket claim, converge]
S --> T["stop: remove lock if ours (new)"]
Before and after: see #1189, How it works for the user.
| Lock state at start | Today | After |
|---|---|---|
| none | starts | starts, lock written |
| live pid, other folder | starts, sweeps the other's emulators | refused, exit 1 |
| live pid, same folder | "already running" | "already running" |
| pid gone | starts | starts, lock taken over |
owner.json missing or not JSON |
starts | starts, lock taken over |
If this goes wrong, a user sees a worker refuse to start while no other worker runs.
Technical spec
Facts
Commit 019ba5c3d.
| # | Fact | Source |
|---|---|---|
| F1 | The worker branch calls discoverDrivers only when options.drivers is undefined |
src/daemon/main.ts:421-423 |
| F2 | discoverDrivers loads the drivers module and nothing else when SIMLOCK_DRIVERS_MODULE is set |
src/daemon/main.ts:1305-1311 |
| F3 | A gateway returns through startGatewayDaemon before driver discovery |
src/daemon/main.ts:376-377 |
| F4 | The Android driver starts or adopts its adb server and runs the sweep while it is created, inside discovery | src/drivers/android/index.ts:736-747 |
| F5 | The sweep announces every console port 5586–5682 | src/drivers/android/index.ts:967-975, :104-105 |
| F6 | A start failure is logged as Daemon failed to start with fields.message, exit 1 |
src/daemon/main.ts:1704-1712 |
| F7 | The CLI turns the last such line into The daemon could not start: <message> |
src/ports/daemon-launcher.ts:19-24, :47-59 |
| F8 | An existing e2e test proves a start error reaches DAEMON_STARTUP_FAILED with that prefix |
e2e/start-refusal.test.ts:2-18 |
| F9 | A second daemon on one folder is refused at the socket claim with DaemonAlreadyRunningError |
src/daemon/connection-host.ts:18-42 |
| F10 | The fake-driver e2e helper sets SIMLOCK_DRIVERS_MODULE; driver: "real" does not |
e2e/helpers/env.ts:199-206 |
| F11 | The e2e project runs files in parallel, 4 workers; the slow script runs --no-file-parallelism |
vitest.config.ts:44-48; package.json:56 |
| F12 | Each e2e test's daemons are torn down in one afterEach |
e2e/helpers/env.ts:54-58 |
| F13 | slow-e2e.sh exits 75 when another run holds its lock; its lock is a folder holding a pid, and a dead pid is stale |
scripts/slow-e2e.sh:15-20, :35, :52-61 |
| F14 | src/slow-e2e.test.ts drives the script with override env vars and checks exit 75 |
src/slow-e2e.test.ts:41-48, :187 |
| F15 | Liveness of a pid is process.kill(pid, 0); ProcessSupervisor.isAlive wraps it |
src/ports/parent-watch.ts:64-66; src/ports/process-supervisor.ts:12 |
| F16 | Code reads the uid with process.getuid?.() and checks a folder's owner and 0700 mode |
src/agent-device/host/agent-device-supervisor.ts:535-545 |
| F17 | Test-only startDaemon options are declared with a why: test-only comment |
src/daemon/main.ts:169-203 |
| F18 | The website offers "a separate install on the same account" with SIMLOCK_HOME |
website/src/content/docs/docs/worker/running-the-daemon.mdx:152 |
| F19 | e2e/gateway-fleet.test.ts runs two workers at once, with fake drivers |
e2e/gateway-fleet.test.ts:140-146; F10 |
| F20 | startDaemon takes test-only processRunner and filesystem options |
src/daemon/main.ts:180, :188 |
| F21 | slow-e2e.sh takes the lane lock, then runs helper check (which first runs clean, killing the lane's leftover processes under /tmp/simlock-slow); a failed check frees the lane lock and exits 69 |
scripts/slow-e2e.sh:220-236, :79-97 |
| F22 | The hardware verifier reports unavailable at once when the script's check fails, and waits up to 30 minutes on exit 75 |
.claude/agents/hardware-verifier.md:21-34 |
| F23 | #stop runs stopAuxiliary first, then settle, then dispose (which reaps the drivers' adb server), then the host stop |
src/daemon/server.ts:643-665 |
Modules touched
Searches: discoverDrivers, startDaemon(, stopAuxiliary, SIMLOCK_HOME under website/, SIMLOCK_SLOW_ in scripts/ and src/slow-e2e.test.ts.
flowchart LR
main["daemon/main.ts (changed)"] --> lock["ports/worker-lock.ts (new)"]
main --> disc[discoverDrivers]
lock --> fs[Filesystem]
lock --> ps[ProcessSupervisor]
slow["scripts/slow-e2e.sh (changed)"]
web["website running-the-daemon.mdx (changed)"]
src/ports/worker-lock.ts(new):WorkerLockport andNodeWorkerLockadapter overFilesystemandProcessSupervisor, plusdefaultWorkerLockPath(platform, uid, tmpdir); exported fromsrc/ports/index.ts; a row insrc/ports/AGENTS.md. No fake: the tests use the adapter on a temp folder.src/daemon/main.ts: the worker branch takes the lock beforediscoverDriversexactly when F1 and F2 both say drivers are discovered (nooptions.drivers, noSIMLOCK_DRIVERS_MODULE). It releases it afterdisposeand before the host stops (F23), so no other worker sweeps while this one still acts on devices; and on a failed start, only when this start wroteowner.json(not by comparing pids: two in-process daemons shareprocess.pid). Test-only optionworkerLockPath(F17) changes only where the lock lives, never whether it is taken.src/daemon/AGENTS.md: the composition line and Rules.scripts/slow-e2e.sh: insidehelper check, afterclean(F21), readsowner.jsonat${SIMLOCK_SLOW_WORKER_LOCK:-/tmp/simlock-$(id -u)/worker.lock}. A live pid fails the check, so the lane lock is freed and the script exits 69, printingnot fit: a Simlock worker holds this machine (pid <pid>, folder <folder>); stop it first: SIMLOCK_HOME=<folder> simlock daemon stop. A lane worker left by an aborted run is killed bycleanbefore this check, so it never blocks.website/src/content/docs/docs/worker/running-the-daemon.mdx: line 152 replaced.
Contract and event changes
None: the refusal is a start failure (F6, F7); no shape, code or event is added. owner.json fields read back: pid a positive integer, folder a string, startedAt a string. Any other content is unreadable, and an unreadable lock is stale.
Other code on the same state
Searched worker.lock, owner.json, /tmp/simlock- in src/, scripts/, e2e/: 0 hits today. The slow lane's own lock (/tmp/simlock-slow-e2e.lock, F13) is a different path; both may be held at once, by the lane runner and by the lane's worker. The socket claim (F9) runs after the lock: same folder alive → the lock goes on, the claim refuses as today.
Failure modes
| Call | Outcome | What happens | Limit, counted from |
|---|---|---|---|
mkdir of the lock folder |
made | write owner.json, go on |
none: one local call |
mkdir of the lock folder |
exists, holder alive, other folder | start fails with the refusal message | none |
mkdir of the lock folder |
exists, holder alive, same folder | go on; the claim decides (F9) | none |
mkdir of the lock folder |
exists, holder gone or owner.json unreadable |
rewrite owner.json with this pid, go on |
none |
parent /tmp/simlock-<uid> (macOS, Linux; also the parent of workerLockPath) |
cannot be made, not ours, or not 0700 and cannot be set |
start fails: <path> cannot be used as the worker lock folder: <reason> |
none |
parent <os.tmpdir()>\simlock (Windows) |
cannot be made | start fails with the same message; no owner or mode check | none |
| release at stop | owner.json names another pid |
leave it | none |
A restart in the middle: a crash after mkdir and before owner.json leaves an unreadable lock, taken over at the next start (Tests line 1). Two workers starting at once: both may see a stale lock and take it over; the later write wins. Not tested (realistic edge cases only).
Rules in play
- ADR 0059 §1–4: all of it, as written.
- ADR 0034 §1, "One worker per OS user": the lock enforces it for workers with real drivers.
- ADR 0034 §4, "
SIMLOCK_HOMEis for tests only": fake-driver workers never take the lock (F2, F10). - Ports rules, "Application code never imports
fs": the adapter lives insrc/ports. - Documentation rule 9: the website page changes in this PR.
Tests
Seam: startDaemon in src/daemon/main.test.ts with workerLockPath set to a temp folder; the adapter in a new src/ports/worker-lock.test.ts; the script lines in src/slow-e2e.test.ts.
- A worker with real drivers takes the worker lock before driver discovery, writing its pid, folder and start time: no drivers passed and no
SIMLOCK_DRIVERS_MODULE→ taken before any command (startDaemonwith afilesystemholding an Android SDK and a scriptedprocessRunner(F20), plushostInfoandnpmfakes so only driver commands could reach the runner, against a lock held in another folder: refused, no adb, emulator or xcrun command); drivers passed or module set → the held lock is not read and the start goes on; no lock, stale lock (pid gone), unreadableowner.json→ written (adapter); parent owned by another user →<path> cannot be used as the worker lock folder: <reason>. - A worker whose lock is held by a live pid in another folder gets the refusal message; held by a live pid in the same folder, it goes on (adapter), and the daemon is refused at the socket claim as today; a worker that wins the socket claim then makes sure it holds the lock: missing → it makes it again; naming its own folder and another pid → it rewrites
owner.jsonwith its own pid; naming a live pid in another folder → its start fails with the refusal message; so whichever same-folder start wins holds the lock, whatever order the loser fails in (decided in delivery, pending the maintainer). - A gateway never takes the lock, even when it is held.
- The daemon and
slow-e2e.shagree on the default worker lock path:defaultWorkerLockPathanswers/tmp/simlock-501/worker.lockfor darwin and linux with uid 501, and<tmpdir>\simlock\worker.lockfor win32; and the defaultslow-e2e.shreads (the text afterSIMLOCK_SLOW_WORKER_LOCK:-) matches it for the current uid. - A worker's stop removes the lock only when
owner.jsonis still the one it wrote; a start that fails after taking the lock removes it: still its own, taken over by another meanwhile, failed start. scripts/slow-e2e.sh checkand a run exit 69 with thenot fitmessage, freeing the lane lock, while a live pid holds the worker lock; with a stale holder they go on.
The CLI's prefix is already proven by e2e/start-refusal.test.ts (F8); fake-driver workers side by side by the e2e lane itself (F11, F19).
Done when
pnpm exec vitest run --project unit src/daemon/main.test.ts src/ports/worker-lock.test.ts src/slow-e2e.test.tspasses, with the lines above.- Slow lane, real hardware:
e2e/slow-android-smoke.test.tschecks that/tmp/simlock-<uid>/worker.lock/owner.jsonnames its worker's pid and folder while it runs, and is gone aftersimlock daemon stop. pnpm test:e2epasses with its files in parallel (F11).running-the-daemon.mdxno longer offers a second worker throughSIMLOCK_HOME, and says a second worker refuses to start and names the first.- ADR 0059 is set to Accepted on PR docs(adr): 0059 a worker with real drivers holds a per-user lock (#1189) #1190 when this merges.
Out of scope
- Two OS users on one Mac.
- Changing the Android sweep.
- Removing other
SIMLOCK_HOMEmentions from the website.
Depends on
Written by an agent.
Activity
test: the worker lock is written over no lock, a stale lock and an un…
feat(ports): an unreadable owner.json is a stale worker lock (#1191)
test: a parent owned by another user is refused with the worker lock …
feat(ports): the worker lock's parent folder is the user's own and 07…
test: a worker with real drivers takes the worker lock, one with subs…
test: a worker whose lock is held by a live pid in the same folder go…
feat(ports): a live holder in the same folder keeps its lock (#1191)
test: a worker that wins the socket claim rewrites owner.json with it…
feat(daemon): the worker that wins the socket claim owns the worker l…
test: a gateway never takes the worker lock (#1191)
9 remaining items
feat(scripts): slow-e2e.sh check refuses a machine a live worker holds (
docs: the worker lock in the ports and daemon manuals, the website an…
refactor(ports): keep the worker lock's refusal error and port type i…
test: a worker lock owner.json with pid 0 is unreadable, not a live h…
fix(ports): the worker lock's release removes only the lock it rename…
test: the worker lock is first, and held until the drivers are dispos…
docs: the worker lock in the startup flow (#1191)
docs: fit the startup flow and daemon manual in the 150-line budget (#…
fix(ports): a worker lock claim re-makes the lock until owner.json sh…
test(e2e): the slow smoke waits for the worker lock to go after daemo…
docs(adr): 0059 to Accepted (#1191)
docs(adr): accept 0034, its feature #700 is closed (#1191)
docs(adr): restore the 0032 row, only 0034 changes (#1191)
feat(daemon): a worker with real drivers holds a per-user lock (#1193)