Repository navigation
fix(android): start serve-emu only after Android finishes booting - #57
Merged
hannojg merged 3 commits intoOct 30, 2026
Merged
Conversation
jiunshinn
marked this pull request as ready for review
October 28, 2026 09:42
hannojg
self-requested a review
October 29, 2026 22:17
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
force-pushed
the
fix/android-wait-for-boot
branch
from
October 30, 2026 07:54
ec5629c to
c394be0
Compare
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #44.
Problem
adb lists a device as
deviceas soon as adbd runs. Android starts its system services several seconds later.packages/core/src/serve-emu.tsmaps the adb statedevicetoBooted, andensureStartedspawns serve-emu at once. A scrcpy server that starts in that window crashes:InputManagerGlobal.getInstance()returns null while theinputservice is not registered yet (adb shell service check inputprintsnot 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 asserve-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'sControllerneeds 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
ensureStartedspawns serve-emu, it now pollsadb shell getprop sys.boot_completedevery 500 ms until the value is1.bootAndroidEmulator. Because it ends before the spawn, the 30 s readiness window still measures only the backend.android.backend.boot_wait.samplesand.durationrecord only the waits that happened, withoutcomebootedortimeout. The new metrics are documented indocs/telemetry.md.android.backend.startup.durationstill excludes device boot.Checks
On macOS (Apple Silicon) with Node 22.23.2, as in CI, after
npm ci,vendor:agent-deviceandvendor:serve-emu:npm test: 842 passed, 1 failed. The failure isa relocated Baguette loads the selected Xcode library after its build-toolchain rpath is removed. It also fails onmain(fb3ab34) on this machine and depends on the local Xcode, not on Android.npm run typecheck:telemetry: passed.packages/core/test/serve-emu-boot-wait.test.ts, each with a fake adb:1.dispose()ends the wait, and serve-emu does not start.serve-emu-startup.test.ts's fake adb now answers the boot probe.test:hosts,test:visual(Linux Docker) ortest:package.Evidence
Setup:
sdk_gphone16k_arm64) does a cold boot with-no-snapshot-load.tooling/hosts, Codex profile), served fromnpm run build:hosts. The serve-emu runtime frombuildServeEmuis besideserver.mjs.mobile_open_simulator {"platform":"android","kind":"emulator","deviceId":"emulator-5580"}at the moment adb lists the emulator.Layout of the screenshots and videos:
mobile_android_stream_sessioncall the panel makes with its result, and the livesys.boot_completedand 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.callstack-before.mp4
After (this branch): the one
mobile_android_stream_sessioncall waits for boot and returnsStream readyafter 11.1 s. No start fails.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.