Skip to content

fix(android): start every emulator with a token gRPC endpoint at the console port + 3000 - #1041

Closed
Nabil372 wants to merge 11 commits into
callstackincubator:mainfrom
Nabil372:android-emulator-grpc
Closed

Nabil372 wants to merge 11 commits into
callstackincubator:mainfrom
Nabil372:android-emulator-grpc

Conversation

@Nabil372

@Nabil372 Nabil372 commented Oct 28, 2026 •

Copy link
Copy Markdown

An emulator started with -port and no -grpc serves no gRPC and writes no avd/running/pid_<pid>.ini. Tools that drive emulators over gRPC read that file, so they cannot find a leased emulator. Argent fails with "emulator not found among running emulators", while adb-only tools still work.

As decided on #639, every launch now passes -grpc <console port + 3000> -grpc-use-token. The allocator also probes the +3000 port, as it does the console and adb ports. The flags stay out of the clean-baseline hash, so existing baselines are kept.

This replaces the earlier android.emulator.grpc switch: the config key, its docs and the protocol bump are gone. With the discovery file written, Android Studio's Running Devices may also list Simlock's emulators (not checked), which ADR 0001 keeps out of its sight. I can record that in an ADR if you want one.

This implements the decision on #639, so a maintainer can close it. #1042 is a duplicate of #639.

  • The maintainer's repro test from bug/639-repro (5586 and 5588 give 8586 and 8588, each with a token), plus a console port skipped when its +3000 port is taken (src/drivers/android/index.test.ts)
  • The exact-argv tests carry the three flags; Boot section of the Android manual
  • gRPC listens on 127.0.0.1 only: lsof showed only 127.0.0.1:8590, and the LAN address refused a connection (emulator 37.2.12, API 37)
  • A baseline captured on main without -grpc loaded with it (by hand): a 2.3 s boot from simlock_clean_baseline, then a snapshot reclaim with no wipe
  • With 127.0.0.1:8590 taken, the lease got console 5592 and gRPC 8592, and booted
  • The slow lane checks both on a real emulator, as Android: emulators start without gRPC, so tools that need the emulator gRPC endpoint (Argent) cannot drive leased devices #639's decision asks (e2e/slow-android-smoke.test.ts, "a leased emulator's gRPC port, its console port plus 3000, listens on loopback only, ..."). lsof finds a listener on the port and none beyond 127.0.0.1 or ::1. After a release, the port closes, and the next boot keeps the kernel boot id the clean baseline captured. It passed in 37 s. It fails without the gRPC flags (nothing listens) and with snapshot loading forced off (the boot id changes). The lane's baseline is saved under -grpc; a baseline saved without it was checked by hand (the line above)

Review

Spec review: skipped. Code review: 0 blocking, 4 fixed, 6 notes. Claims review: skipped.
Rounds: 3. Tokens: n/a.

Summary by CodeRabbit

  • Improvements
    • Android emulators now use token-authenticated gRPC connections on a dedicated port. Port selection checks for conflicts on the gRPC port as well as the console and ADB ports, helping avoid launch failures.
  • Tests
    • Added coverage to verify emulator gRPC listeners stay on loopback and that a released emulator can be leased again with its existing boot state.

…ig file

Operators can set `android.emulator.grpc` to start this machine's
emulators with their gRPC endpoint on (`-grpc <console port + 3000>
-grpc-use-token`). The emulator then writes its discovery file
(`avd/running/pid_<pid>.ini`, with `grpc.port` and `grpc.token`), which
tools that show and drive an emulator through gRPC, such as Android
Studio's device manager and argent, read to find it. Off by default, so
launches are unchanged.

The port follows the console port, which the driver already reserves
free and unique (even, 5586-5682), so two emulators never share one. Like
the other `android.emulator` keys it is config-only and applies at a
device's next boot; it does not enter the clean-baseline hash, because it
changes no graphics state.

The key joins the config contract, so the daemon protocol moves to 65.
… the baseline

The tests that pin the protocol version and the config.get round trip,
and the contract and daemon manuals, still named 64 or the four-key
`android.emulator` block. A grpc row joins the baseline test, so the claim
that the setting keeps the clean baseline is checked rather than stated.

The gRPC port is as unique as the console port it follows, and no more:
the allocator probes the console port and the one above it, not +3000.
The configuration reference now says that with the setting on, Android
Studio lists Simlock's emulators, which ADR 0001 otherwise keeps out of
its sight.
Nabil372 and others added 5 commits October 29, 2026 12:17
# Conflicts:
#	src/contract/AGENTS.md
#	src/contract/protocol.ts
#	src/daemon-protocol/index.test.ts
#	src/daemon/AGENTS.md
…console port + 3000

Without -grpc, an emulator launched with -port serves no gRPC and writes no
avd/running/pid_<pid>.ini, so tools that drive emulators over gRPC (argent,
Android Studio's Running Devices) cannot find a leased one (callstackincubator#639).

Every launch now passes -grpc <console port + 3000> -grpc-use-token, and the
console-port allocator also probes the +3000 port. The gRPC flags stay out of
the clean-baseline hash. This replaces the opt-in android.emulator.grpc key:
the config key, its docs and the protocol 65 bump are gone, per the maintainer
decision on callstackincubator#639.
@Nabil372 Nabil372 changed the title feat(config,android): serve an emulator's gRPC endpoint from the config file fix(android): start every emulator with a token gRPC endpoint at the console port + 3000 Oct 29, 2026
…nd the clean baseline loads with it

The callstackincubator#639 decision asks the slow lane to confirm both on a real emulator.
The leased emulator's gRPC port, its console port plus 3000, accepts on
127.0.0.1 and refuses on every other IPv4 address of the host. After a
release and a shutdown, the next lease boots from the clean baseline:
the kernel boot id is the one the snapshot captured.
A connect that fails for any reason passed the old check, and IPv6
addresses on tunnels time out rather than refuse. lsof lists every
listener on the port, on either family, so the test now asserts there is
one and that none is bound beyond 127.0.0.1 or ::1. The title says
"listens", which is what the test proves.
…napshot boot

A matching boot id also passed if the emulator never stopped. The test
now waits for the gRPC port to close after the shutdown, and an lsof
failure other than "nothing listens" is thrown, not read as a closed
port.
@Nabil372

Copy link
Copy Markdown
Author

Thanks for the decision on #639, and for the repro test. It made the fix straightforward.

One question on the slow-lane check "an existing snapshot still loads with -grpc". The lane runs only the PR's build, and that build always passes -grpc. So the lane test I added (e2e/slow-android-smoke.test.ts) captures the clean baseline under -grpc and shows a later boot loads it: the kernel boot id stays the same.

The upgrade case from the triage's Risk is a baseline captured without -grpc. I checked it by hand: main at 7ac7f18 captured the baseline, then this branch booted from it in 2.3 s with a snapshot reclaim and no wipe. The lane could only make such a baseline by starting the emulator outside Simlock.

Is the lane test enough, or would you like the lane to cover the no--grpc baseline too?

@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Walkthrough

Android emulator launches now use an authenticated gRPC port 3000 above the console port, and port allocation checks that port. Tests cover launch arguments and verify loopback listeners and emulator reuse across lease release and reacquisition.

Changes: Android gRPC launch and validation

Layer / File(s) Summary
Configure gRPC startup and reserve its port
src/drivers/android/index.ts, src/drivers/android/AGENTS.md
Emulator launches add -grpc with a port 3000 above the console port and -grpc-use-token. Port allocation checks the gRPC port, and the boot documentation describes the new arguments and port check.
Validate launch arguments and lease reuse
src/drivers/android/index.test.ts, src/daemon/main.test.ts, src/drivers/android/physical-handler.test.ts, e2e/slow-android-smoke.test.ts
Tests cover gRPC port selection and launch arguments across boot paths. The real-emulator test checks loopback-only listeners, shutdown and port closure after release, and reuse of the same AVD, serial, and boot ID.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant E2ESmokeTest
  participant SimlockDaemon
  participant SimlockAdbServer
  participant lsof
  E2ESmokeTest->>SimlockDaemon: Lease Pixel 9 with warm pool disabled
  SimlockDaemon-->>E2ESmokeTest: Return AVD name and lease ID
  E2ESmokeTest->>SimlockAdbServer: Find serial matching AVD
  SimlockAdbServer-->>E2ESmokeTest: Return emulator serial
  E2ESmokeTest->>lsof: Inspect derived gRPC port listeners
  lsof-->>E2ESmokeTest: Return listener addresses
  E2ESmokeTest->>SimlockDaemon: Release lease
  E2ESmokeTest->>E2ESmokeTest: Wait for shutdown and port closure
  E2ESmokeTest->>SimlockDaemon: Lease Pixel 9 again
  SimlockDaemon-->>E2ESmokeTest: Return AVD name and lease ID
  E2ESmokeTest->>SimlockAdbServer: Find serial and read boot ID
  SimlockAdbServer-->>E2ESmokeTest: Return serial and boot ID
  E2ESmokeTest->>lsof: Inspect derived gRPC port listeners again
  lsof-->>E2ESmokeTest: Return listener addresses
Loading

Possibly related issues

  • Issue 639 concerns Android emulator launches without -grpc and failed gRPC discovery and tooling. This PR adds gRPC launch arguments and tests the listener behavior.

Suggested labels: javascript

Suggested reviewers: v3ron

Merge Risk: Minimal · up to 80dbd

Android emulators now start with a token-authenticated gRPC port 3000 above the console port, and port allocation skips console ports whose gRPC port is already listening. The changes look consistent and are covered by launch-argument tests and a slow real-emulator test. A rare port-allocation collision with an IPv6-only listener has not been shown to occur.

Security architecture risk: Moderate · up to 80dbd

The new control endpoint is enabled on every Android emulator launch. Token authentication is requested, and loopback-only operation is supported by the reported manual check and new regression coverage. However, binding, authentication, and credential-file guarantees across emulator versions remain unverified. No exploitable security defect was established.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The new attack surface consists of the gRPC endpoints of emulators launched through AndroidDriver. A failure in their binding or authentication contract could expose the corresponding emulator-control surface. The evidence does not establish remote access, host compromise, shared credential authority, or cross-tenant reachability.

Security Findings and Attack Paths

  • observed — The supplied security assessment retains no findings. Its external-exposure candidate is deferred pending upstream bind-behavior evidence. The reported localhost-only listener and refused LAN connection are counterevidence to remote reachability for the tested emulator version; they do not resolve the contract across other versions.

Trust Boundaries and Controls

  • observed — The new endpoint requests token authentication. Existing AVD-directory checks prevent readiness or destructive adb operations from trusting a serial solely because it answers. Those checks do not establish gRPC token enforcement, listener ownership, or discovery-file credential permissions.

Resilience and Maintainability Implications

  • inferred — Initial allocation avoids an already-listening derived gRPC port, but recovery and retained-port reuse can launch without repeating that check. Because readiness remains adb-based, the evidence does not resolve whether a gRPC-only bind failure aborts startup, leaves discovery unavailable, or otherwise affects consumers. No resulting credential leak or endpoint substitution was demonstrated.

Hardening Proposals

  • proposed — Establish a version-specific runtime contract for loopback binding, rejection of missing or invalid credentials, and discovery-file access permissions. Include gRPC-only port collisions during recovery and reuse, and document the intentional change to accidental local-tool visibility.
Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: Android emulator launches now pass a token-protected gRPC endpoint at console port + 3000. The changeset adds -grpc <port+3000> -grpc-use-token to every boot, update…
Docstring Coverage Passed Docstring coverage is 50.00% which is sufficient. The required threshold is 50.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentati…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: Android emulator launches now pass a token-protected gRPC endpoint at console port + 3000. The changeset adds -grpc &lt;port+3000&gt; -grpc-use-token to every boot, updates port allocation to probe the gRPC port, and adds tests and docs for it. The title uses the conventional fix(android): prefix and fits the scope. It is 88 characters, which is slightly long but still readable.

Full details: Docstring Coverage

Explanation

Docstring coverage is 50.00% which is sufficient. The required threshold is 50.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 5 files. (1 skipped: 1 unsupported.)

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@V3RON

V3RON commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Already done! 👌

@V3RON V3RON closed this Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants