Repository navigation
fix(server): wait for Android boot and stream readiness before opening - #48277
Conversation
Cold-booting an Android emulator in the Device panel could fail with "No video received from the device. Reconnect to try again." because expo-device-hub's /api/devices/boot returns as soon as adb comes online (before sys.boot_completed=1), and serve-emu's first capture startup can outlast the client's 15-second first-frame timer. After booting an Android emulator in DeviceHubOperations.boot, wait for sys.boot_completed=1 (up to 2 minutes) and warm serve-emu via /vendor/serve-emu/health?device=<serial> (up to 60 seconds, retrying through serve-emu's 5-second retry cooldown and one transient startup failure) before returning the booted serial. Slow boot completion or stream warm-up errors log a warning and degrade cleanly so agent-only flows and Reconnect still work, while a confirmed emulator exit (two adb devices omissions separated by a poll interval, bounded inside each stage's deadline) fails boot with DeviceBootError(launch_failed). Model: Gemini 4 Neon (high); harness: Antigravity in T3 Code
ApprovabilityVerdict: Approved at Macroscope's review found this PR approvable — Android emulator launches now wait for boot and stream readiness before opening, reducing premature video failures. The added waits are bounded, with fallback behavior for slow startup and stream errors; physical devices and iOS are unaffected. You can add or adjust custom eligibility rules. Learn more. |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Problem
Cold-booting an Android emulator from the Device panel reliably fails with
"No video received from the device. Reconnect to try again."on the first open:expo-device-hub'sPOST /api/devices/bootreturns as soon asadbcomes online, beforesys.boot_completed=1and SurfaceFlinger are ready.openpublishes the session,serve-emu's first capture startup (scrcpy-server, ffmpeg probe, and H.264 startup gate) can exceed the client's 15 s first-frame timer (FIRST_FRAME_TIMEOUT_MS), so the panel gives up before the first frame arrives even though the emulator is still starting normally.Expected behavior:
bootfinishes booting and warming the stream (while the panel shows"Starting…"), then opens the session ready to stream frames within the 15 s client budget.Change
In
apps/server/src/device/DeviceHubOperations.ts, mirror what iOSbootalready does (attachIosStream) for cold-booted Android emulators (skipping physical phones and missing/non-emulator serials):/api/devices/bootreturns anemulator-<port>serial, polladb -s <serial> shell getprop sys.boot_completedfor up to 2 minutes (every 1.5 s) until it returns1.GET /vendor/serve-emu/health?device=<serial>(up to 60 s). In[email protected],/healthawaitsserve-emu's sharedensure()startup, returns200 { ok: true, status: "streaming" }once capture is streaming (and keeps capture warm with 0 clients), and returns503during a failed start or its 5 sSPAWN_RETRY_COOLDOWN_MS. Warm-up retries through cooldown503s and up to one transient startup failure.adb devicesomissions separated by 1.5 s (so a single transientadbblip does not abort boot, and exit confirmation never overshoots the stage budget). A confirmed exit fails boot withDeviceBootError(launch_failed)preserving the underlyingcause; slow boots or video/HTTP failures log a warning and degrade cleanly so agent-only flows and Reconnect still work.packages/client-runtime/src/device/stream.ts,DeviceHubOperations.attach,LocalDeviceHost, orDeviceService.ts.Scope
Focused server bug fix in
DeviceHubOperations.boot(2 files:DeviceHubOperations.tsandDeviceService.test.ts).Context:
#18598described the same cold-start timeout symptom and was closed asNot plannedbecause the 15 s client timer is working as designed; this fix leaves that timer untouched and waits for readiness during server-sidebootinstead.Verification
b6a615c798, identical 2-file patch before clean rebase onto4b8199987c→4d4a4e2041) on Windows 11 Pro (10.0.26200), Android Emulator 37.2.13.0 (Medium_Phone_API_37.0, Android 17.0), isolated[email protected],ffmpeg9.0.2, running the realmakeDeviceHubOperations:-no-snapshot):POST /api/devices/bootreturnedemulator-5554in 12.95 s whilesys.boot_completedwas still empty; polling waited 6.6 s (5 polls) untilsys.boot_completed=1;GET /vendor/serve-emu/health?device=emulator-5554blocked 1.52 s and returned200 { ok: true, status: "streaming" }(libx264,grpc-screenshot); totalboot()took 21.1 s. Opening/vendor/serve-emu/ws?device=emulator-5554&frame-meta=1immediately after received the first binarySEMUvideo frame in 166 ms with 0 reconnects, followed by a valid 443 KB PNG screenshot and clean shutdown.POST /api/devices/boottook 3.44 s (sys.boot_completed=1on poll oxc stack #1),/healthreturned200 streamingin 2.28 s (totalboot()5.97 s), first WS video frame arrived in 165 ms with 0 reconnects, followed by a valid 715 KB PNG screenshot and clean shutdown.[email protected]vendor/serve-emu/dist/middleware.jsbehavior (/healthawaitsensure(), returns200 { ok: true, status: "streaming" }only when streaming, and returns503with"cooling down after a failure"during its 5 sSPAWN_RETRY_COOLDOWN_MS).vp run check:changedon rebased4d4a4e2041):format,lint(0 warnings, 0 errors, 206 rules),knip,graphql,typecheck, andtest(DeviceService.test.ts: 87 passed including 8 newTestClocktests for boot-completion wait, restarted cooldown retries, missing-serial skip, timeout/warm-up degradation, transientadbomission, and confirmed exit within the 60 s budget;AgentContextBudget.test.ts: 2 passed).serve-emu503/cooldown (covered byTestClockunit tests), and the live stream check measured binarySEMUframes arriving on the WebSocket rather than full browser decoding in the Device panel UI.Model: Gemini 4 Neon (high); harness: Antigravity in T3 Code.