Skip to content

fix(server): wait for Android boot and stream readiness before opening - #48277

Merged
Yash-Singh1 merged 1 commit into
pingdotgg:mainfrom
EliBlauBalto:fix/android-boot-stream-readiness
Dec 3, 2026
Merged

Yash-Singh1 merged 1 commit into
pingdotgg:mainfrom
EliBlauBalto:fix/android-boot-stream-readiness

Conversation

@EliBlauBalto

@EliBlauBalto EliBlauBalto commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

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:

  1. expo-device-hub's POST /api/devices/boot returns as soon as adb comes online, before sys.boot_completed=1 and SurfaceFlinger are ready.
  2. Once open publishes 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: boot finishes 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 iOS boot already does (attachIosStream) for cold-booted Android emulators (skipping physical phones and missing/non-emulator serials):

  • After /api/devices/boot returns an emulator-<port> serial, poll adb -s <serial> shell getprop sys.boot_completed for up to 2 minutes (every 1.5 s) until it returns 1.
  • Once boot completes, call GET /vendor/serve-emu/health?device=<serial> (up to 60 s). In [email protected], /health awaits serve-emu's shared ensure() startup, returns 200 { ok: true, status: "streaming" } once capture is streaming (and keeps capture warm with 0 clients), and returns 503 during a failed start or its 5 s SPAWN_RETRY_COOLDOWN_MS. Warm-up retries through cooldown 503s and up to one transient startup failure.
  • Both stages reserve 5.5 s inside their deadlines to confirm an emulator exit via two adb devices omissions separated by 1.5 s (so a single transient adb blip does not abort boot, and exit confirmation never overshoots the stage budget). A confirmed exit fails boot with DeviceBootError(launch_failed) preserving the underlying cause; slow boots or video/HTTP failures log a warning and degrade cleanly so agent-only flows and Reconnect still work.
  • Does not touch the client's 15 s first-frame timer in packages/client-runtime/src/device/stream.ts, DeviceHubOperations.attach, LocalDeviceHost, or DeviceService.ts.

Scope

Focused server bug fix in DeviceHubOperations.boot (2 files: DeviceHubOperations.ts and DeviceService.test.ts).

Context: #18598 described the same cold-start timeout symptom and was closed as Not planned because the 15 s client timer is working as designed; this fix leaves that timer untouched and waits for readiness during server-side boot instead.

Verification

  • Live Windows Android emulator test (b6a615c798, identical 2-file patch before clean rebase onto 4b8199987c → 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], ffmpeg 9.0.2, running the real makeDeviceHubOperations:
    • True cold boot (-no-snapshot): POST /api/devices/boot returned emulator-5554 in 12.95 s while sys.boot_completed was still empty; polling waited 6.6 s (5 polls) until sys.boot_completed=1; GET /vendor/serve-emu/health?device=emulator-5554 blocked 1.52 s and returned 200 { ok: true, status: "streaming" } (libx264, grpc-screenshot); total boot() took 21.1 s. Opening /vendor/serve-emu/ws?device=emulator-5554&frame-meta=1 immediately after received the first binary SEMU video frame in 166 ms with 0 reconnects, followed by a valid 443 KB PNG screenshot and clean shutdown.
    • Quick Boot resume: POST /api/devices/boot took 3.44 s (sys.boot_completed=1 on poll oxc stack #1), /health returned 200 streaming in 2.28 s (total boot() 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.
  • Hub source check: Verified [email protected] vendor/serve-emu/dist/middleware.js behavior (/health awaits ensure(), returns 200 { ok: true, status: "streaming" } only when streaming, and returns 503 with "cooling down after a failure" during its 5 s SPAWN_RETRY_COOLDOWN_MS).
  • Focused automated checks (vp run check:changed on rebased 4d4a4e2041): format, lint (0 warnings, 0 errors, 206 rules), knip, graphql, typecheck, and test (DeviceService.test.ts: 87 passed including 8 new TestClock tests for boot-completion wait, restarted cooldown retries, missing-serial skip, timeout/warm-up degradation, transient adb omission, and confirmed exit within the 60 s budget; AgentContextBudget.test.ts: 2 passed).
  • Not verified live: Neither live run triggered a serve-emu 503/cooldown (covered by TestClock unit tests), and the live stream check measured binary SEMU frames arriving on the WebSocket rather than full browser decoding in the Device panel UI.

Model: Gemini 4 Neon (high); harness: Antigravity in T3 Code.

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
@github-actions github-actions Bot added vouch:unvouched Author is not yet trusted in the VOUCHED list. size:L 100-499 changed lines (additions + deletions). labels Dec 3, 2026
@macroscopeapp

macroscopeapp Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at 4d4a4e2

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.

@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Android virtual-device boot now polls sys.boot_completed and then checks the serve-emu health endpoint before returning. Timeouts and stream startup failures log warnings and retain the serial; confirmed emulator exits raise DeviceBootError. iOS stream attachment remains unchanged.

Changes: Android emulator boot readiness

Layer / File(s) Summary
Boot probes and exit confirmation
apps/server/src/device/DeviceHubOperations.ts
Adds bounded boot-completion polling and exit confirmation. Two successful adb devices omissions confirm an exit; failed probes do not.
Serve-emu stream warm-up
apps/server/src/device/DeviceHubOperations.ts
Checks the serve-emu health endpoint after boot completion. Retries eligible HTTP 503 responses, handles cooling-down responses, and stops on other failures or deadline expiry.
Boot flow and validation
apps/server/src/device/DeviceHubOperations.ts, apps/server/src/device/DeviceService.test.ts
Runs Android readiness checks when the hub returns an emulator serial. Tests cover retries, cooldowns, timeouts, missing serials, and confirmed exits. iOS stream attachment behavior is unchanged.

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant DeviceHubOperations
  participant Hub
  participant Adb as adb
  participant ServeEmu as serve-emu health endpoint
  DeviceHubOperations->>Hub: Request Android device boot
  Hub-->>DeviceHubOperations: Return emulator serial
  loop Until boot completion or deadline
    DeviceHubOperations->>Adb: Probe sys.boot_completed
    Adb-->>DeviceHubOperations: Return boot state
  end
  loop Until stream ready or deadline
    DeviceHubOperations->>ServeEmu: Request health status
    ServeEmu-->>DeviceHubOperations: Return health response
  end
Loading

Suggested reviewers: juliusmarminge

Merge Risk: Minimal · up to 4d4a4

This change makes Android emulator boot wait for boot completion and stream readiness before returning. It degrades cleanly on slow boots and fails only when an emulator exit is confirmed. No concrete merge-blocking risk was established. The worst-case boot is longer, which matters only if some caller enforces a short timeout, and none was found in the server code.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "fix(server): wait for Android boot and stream readiness before opening" matches the main change: DeviceHubOperations.boot now waits for sys.boot_completed=1 and a serve-emu health response …
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.
Description check Passed The description is thorough and follows the template. It has Problem, Change, Scope, and Verification sections. The Problem section states the cold-boot failure, the cause, and the expected behavior. …
Full details: Title check

Explanation

The title "fix(server): wait for Android boot and stream readiness before opening" matches the main change: DeviceHubOperations.boot now waits for sys.boot_completed=1 and a serve-emu health response before the session opens. It follows the conventional commit format and is specific enough for a teammate scanning history. The title does not mention the confirmed-exit failure path, but that is a secondary detail.

Full details: Description check

Explanation

The description is thorough and follows the template. It has Problem, Change, Scope, and Verification sections. The Problem section states the cold-boot failure, the cause, and the expected behavior. The Change section lists the server-side polling, warm-up, and exit-check behavior and names the files left untouched. The Scope section names the two changed files and links context from a closed issue. The Verification section reports live Windows emulator runs with timings, the automated checks run, and what was not verified live. The template asks for a conventional commit title in the PR title, but the title is not part of the provided description, so it cannot be checked here. The model and harness line is present at the end. Issue #18598 is referenced in Scope but not linked as a URL, which is minor.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 4b81999 and 4d4a4e2.

Files selected for processing: 2

  • apps/server/src/device/DeviceHubOperations.ts
  • apps/server/src/device/DeviceService.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@Yash-Singh1
Yash-Singh1 merged commit 32a917c into pingdotgg:main Dec 3, 2026
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:unvouched Author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants