Skip to content

fix(android): start serve-emu only after Android finishes booting - #57

Merged
hannojg merged 3 commits into
callstackincubator:mainfrom
jiunshinn:fix/android-wait-for-boot
Oct 30, 2026
Merged

hannojg merged 3 commits into
callstackincubator:mainfrom
jiunshinn:fix/android-wait-for-boot

Conversation

@jiunshinn

@jiunshinn jiunshinn commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #44.

Problem

adb lists a device as device as soon as adbd runs. Android starts its system services several seconds later. packages/core/src/serve-emu.ts maps the adb state device to Booted, and ensureStarted spawns serve-emu at once. A scrcpy server that starts in that window crashes:

ERROR: Attempt to invoke virtual method 'android.view.InputDevice android.hardware.input.InputManagerGlobal.getInputDevice(int)' on a null object reference
    at android.view.KeyCharacterMap.load(KeyCharacterMap.java:377)
    at com.genymobile.scrcpy.control.Controller.<init>(Controller.java:96)

InputManagerGlobal.getInstance() returns null while the input service is not registered yet (adb shell service check input prints not found). Earlier in the same window, the server also exits right after it sends the device name (scrcpy stream ended mid-header (needed 81, had 65)). Each crash reaches the user as serve-emu exited before it became ready.

The emulator image is not the cause. The same Android 17 image (sdk_gphone16k_arm64) streams normally a few seconds later. An upgrade of scrcpy will not fix it either: scrcpy's Controller needs the input service on every Android version. Genymobile/scrcpy#6306 reports the same stack on Android 14, and that issue is still open.

Fix

Before ensureStarted spawns serve-emu, it now polls adb shell getprop sys.boot_completed every 500 ms until the value is 1.

  • The wait has a 120 s limit, the same as bootAndroidEmulator. Because it ends before the spawn, the 30 s readiness window still measures only the backend.
  • If adb reports that the device is gone (not found, offline, unauthorized), the start fails at once. If the plugin server closes during the wait, the start fails with "The plugin server has closed".
  • android.backend.boot_wait.samples and .duration record only the waits that happened, with outcome booted or timeout. The new metrics are documented in docs/telemetry.md. android.backend.startup.duration still excludes device boot.
  • The version goes to 0.1.168, as CONTRIBUTING.md requires. Drop that commit hunk if you bump versions in a separate step.

Checks

On macOS (Apple Silicon) with Node 22.23.2, as in CI, after npm ci, vendor:agent-device and vendor:serve-emu:

  • npm test: 842 passed, 1 failed. The failure is a relocated Baguette loads the selected Xcode library after its build-toolchain rpath is removed. It also fails on main (fb3ab34) on this machine and depends on the local Xcode, not on Android.
  • npm run typecheck:telemetry: passed.
  • New tests in packages/core/test/serve-emu-boot-wait.test.ts, each with a fake adb:
    • serve-emu spawns only after the third probe reports 1.
    • A device that disappears during the wait fails at once, and serve-emu does not start.
    • dispose() ends the wait, and serve-emu does not start.
  • serve-emu-startup.test.ts's fake adb now answers the boot probe.
  • I did not run test:hosts, test:visual (Linux Docker) or test:package.

Evidence

Setup:

  • An Android 17 AVD (sdk_gphone16k_arm64) does a cold boot with -no-snapshot-load.
  • The real Mobile Dev panel runs in the repo's stand-in MCP Apps host (tooling/hosts, Codex profile), served from npm run build:hosts. The serve-emu runtime from buildServeEmu is beside server.mjs.
  • The panel opens with mobile_open_simulator {"platform":"android","kind":"emulator","deviceId":"emulator-5580"} at the moment adb lists the emulator.

Layout of the screenshots and videos:

  • Left: the panel.
  • Right: the MCP server's stderr, each mobile_android_stream_session call the panel makes with its result, and the live sys.boot_completed and input-service state.

Before (main, fb3ab34): the panel shows "Connecting…" while 3 starts fail in a row. The third is the NPE from this issue. On this machine, the panel's retry connected on the 4th start. Every failed start is one of the errors in Sentry.

Before: the panel says Connecting… while three serve-emu starts crash during boot, the third with the InputManagerGlobal NPE

callstack-before.mp4

After (this branch): the one mobile_android_stream_session call waits for boot and returns Stream ready after 11.1 s. No start fails.

After: one stream call waits for boot, then the panel streams the Android 17 home screen with no failed start

callstack-after.mp4

I maintain upstream serve-emu. The same race exists there and in expo-device-hub, and both get the same rule: jiunshinn/serve-emu#181 and expo/expo-device-hub#501.

Authorship

Claude Code (Claude Opus 5.5) wrote this change. It also reproduced the crash and checked the fix on the cold-booted emulator. The PR stays a draft until I review it.

jiunshinn and others added 2 commits October 30, 2026 08:51
adb lists a device as soon as adbd runs, seconds before Android starts its
system services. The plugin counted that adb state as Booted and spawned
serve-emu at once, so the bundled scrcpy server crashed in Controller.<init>:
KeyCharacterMap.load found no input service (NullPointerException in
InputManagerGlobal.getInputDevice) and the panel showed "serve-emu exited
before it became ready". Upgrading scrcpy does not help: the server needs
the input service on every Android version (scrcpy issue #6306 is open).

ensureStarted now polls getprop sys.boot_completed every 500 ms, for up to
120 s like an AVD boot, before it spawns serve-emu, so the 30 s readiness
window still measures only the backend. A device that adb cannot reach fails
at once. android.backend.boot_wait.* records the waits that happened.

Fixes callstackincubator#44

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hannojg
hannojg force-pushed the fix/android-wait-for-boot branch from ec5629c to c394be0 Compare October 30, 2026 07:54
@hannojg

hannojg commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator

Thanks @jiunshinn for the fix and the detailed reproduction! I reproduced the same startup NPE on Android 16 and confirmed the boot gate fixes it.

I’ve rebased onto main, updated the version to 0.1.172, and added the Android panel timeout adjustment (180 s) plus a regression test for a 155 s opening. I also updated the missing-runtime test’s SDK fixture for the new boot probe.

The local suite (926 passed, 1 skipped), telemetry typecheck, host regressions, build, package smoke test, and Codex installation check all pass. I’ll merge this once the final CI run is green.

@hannojg
hannojg merged commit e065172 into callstackincubator:main Oct 30, 2026
6 checks passed
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.

fix(android): serve-emu crashes at startup on Android 16/17 emulators (scrcpy NPE in KeyCharacterMap)

2 participants