Skip to content

fix(android): release sessions after their ADB serial disappears - #5396

Open
thymikee wants to merge 4 commits into
mainfrom
fix/android-close-vanished-device
Open

thymikee wants to merge 4 commits into
mainfrom
fix/android-close-vanished-device

Conversation

@thymikee

@thymikee thymikee commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Summary

Plain Android close can end its session after the app-stop probe fails and other cleanup succeeds. A valid ADB inventory that omits the exact serial establishes absence, including a header-only list. Listed serials and inconclusive reads preserve the original error and claim.

Closes #5376.

Before / after

With the sole ADB transport disconnected, close at cd87454 exited 1 with android-process-probe-unavailable and kept the claim. At f0e08d0 it exited 0, logged the absent-device diagnostic, and left both lists empty. Reconnecting and reopening in the same workspace succeeded.

Validation

  • Compared branch-built CLIs on an API 37 emulator with a private ADB server and a sole TCP transport. The snapshot used android-helper 0.21.47-dev. The post-disconnect list contained only ADB's header.
  • The sole-device regression failed at cd87454. At f0e08d0, 23 focused tests, typecheck, Fallow, command-doc checks, and build passed.
  • The prior live Pixel run verified the nonempty inventory case at cd87454.

Prove it works
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

View guided diff Turn on auto-fix

@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.80 MB 6.80 MB +1.4 kB
Package (unpacked) 6.80 MB 6.80 MB +1.4 kB
Package (download) 2.07 MB 2.07 MB +477 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 18.2 ms 18.9 ms +0.7 ms
CLI --help 39.3 ms 44.4 ms +5.1 ms

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 9 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread website/docs/docs/commands.md Resolved
@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 3 new findings on a5af9f4. The verdict and the full list are in the summary comment.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

thymikee commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

Close does not yet release the session when the vanished serial was the last device adb listed, so #5376 is only partly fixed at cd87454. The CI checks that exercise the changed routes are not reported failing, and Body rules was cancelled, so it needs one re-run before merge. Body rules checks the PR description, which this diff does not touch, so the cancel looks unrelated. There are no conflicts.

adbInventoryOmitsSerial returns Boolean(serials?.length && !serials.includes(serial)), so an inventory with zero rows never counts as omitting the serial. Take the only Wi-Fi phone after a DHCP move, the only emulator after it is killed, or the only USB phone unplugged. Once adb drops it, adb devices prints only the header, the retire branch is skipped, and plain close rethrows android-process-probe-unavailable. The session and claim stay, open runs on the dead serial, open --serial <new> is refused as a selector conflict, and only daemon stop ends it. That is the issue symptom unchanged. The strict parse already proves adb answered, because the header must be line 1 (ime-lifecycle.ts:43), so an empty list is a real inventory. The rule the code must satisfy: plain close releases the session and its claim whenever a header-verified inventory omits the serial, including an empty one. If the adb-server-restart race matters, re-read once after a short delay when the first read is empty and require both reads to agree, in the same helper. Please drop the serials?.length condition, turn the retain test "unconfirmed by an empty inventory" into a retire case, and drop "at least one device" from the help and docs. If the guard stays, please narrow #5376 to the nonempty-inventory scope and change "Closes #5376" to "Refs #5376".

The changed path is device-facing, and the only live run in the PR body kept the serial listed, so close took the ordinary path and never reached isAndroidCloseSerialAbsent. The daemon tests mock the close steps and the adb inventory. By reading, the teardown steps stay silent against a missing serial, but no run shows that on a real adb, and this run is required before merge. Please run the issue repro on the branch build and post the output. Use an emulator as the second serial: adb connect 127.0.0.1:5555 against emulator-5554's adb port gives a droppable serial, so keep emulator-5554 or another device listed. Open an app with default flags, run snapshot -i so the snapshot helper and test IME are active, optionally set a Metro hint so the runtime-hints clear runs, then adb disconnect <serial> and run plain close. The output must show close ok, a warn diagnostic android_app_stop_skipped_device_absent with that serial in the daemon log, an empty agent-device sessions, the serial's claim gone from the claim list, and open --serial <other> working in the same workspace. After the empty-inventory fix, add the sole-device run and show close ok.

Could platform-android own this instead? When pidof is unavailable, stopAndroidPackage could call observeAndroidAdbDevices(). If a header-verified inventory omits device.id, it would emit the diagnostic and return as a no-op instead of throwing androidPidofUnavailableError, and otherwise throw as today. That is about 15 lines in app-lifecycle.ts. It needs no DaemonPlatformServices member, no root-composition lambda, no fixture entry, no request-signal plumbing, no daemon reason literal and no multi-flag guard, and the daemon's cleanupFailures gate and retire path stay as they are. The cost is that the platform cannot tell plain close from close <app> or open --relaunch, so those would also skip the stop on a serial adb no longer lists. The session is ending or the next open fails anyway, so that seems acceptable if the PR says it. If plain-only matters, one typed closeMode field on the platform input would do it without request flags in the daemon. The current shape is about 78 net production lines across 7 files and 3 layers. Either design can adopt the empty-inventory rule above.

Not blocking, take or leave: import the reason constant from the platform contract instead of re-typing it at session-close.ts:531; skip the header line in both modes of readAndroidAdbDevices and let allowPartial decide only about a stateless row (ime-lifecycle.ts:42); add one platform-android test over canned adb devices output with offline and unauthorized rows (both returned) and one with only a header (returns []); emit a debug diagnostic when the inventory is undefined or empty instead of swallowing it at session-close.ts:544, and document android_app_stop_skipped_device_absent; keep the platform-services.ts:12 doc-comment edit only if it is deliberate; and add a clause to commands.md:243 saying that if the serial is still listed as offline, run adb disconnect <serial> and then close again.

Of the earlier inline threads, the one on the empty inventory (#5396 (comment)) still applies through the sole-device case above. These are fixed at this head and can be resolved: the docs retention sentence (#5396 (comment)), the quick listing with partial rows (#5396 (comment)), and the redundant requireCurrent (#5396 (comment)).

Not verified here: a screen recording still active on the vanished serial, whether any shipped adb prints a daemon-start banner on stdout, and how long a real Wi-Fi serial stays offline after a DHCP move. No tests, build, emulator or adb were run in this review, and the unit counts in the PR body are the author's.

Before merge, please post the vanished-serial run, decide the empty-inventory rule, and answer the platform-android ownership question.

@AlexErrant

Copy link
Copy Markdown

Ran the #5376 repro on a physical Pixel 9a over Wi-Fi adb (adb 36.0.0), branch build at cd87454:

  • Serial gone, another device listed: plain close ok in 0.27 s, android_app_stop_skipped_device_absent in the request log, session and claim gone, open --serial <current> then works. Same for close --session <address> from another directory. A healthy session on the real serial in the same daemon is untouched.
  • Serial gone, no other device listed: close still fails with android-process-probe-unavailable and keeps the session, as noted in the review.
  • Serial offline (forwarder killed, no disconnect): session kept; adb dropped the offline serial on its own after ~55 s, and close then succeeded. Frozen forwarder (still device): session kept.
  • adb 36 prints the daemon-start banner on stderr, so stdout starts with the header.
  • open with no selector on the gone serial still reports APP_NOT_INSTALLED with a hint to run apps. The issue mentions that separately, so this PR doesn't fully close it. Feel free to disregard that detail or address it as you see fit.

@thymikee

thymikee commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

Thanks for the run on a real Pixel over Wi-Fi adb. It covers the live evidence the review asked for on the route where another device stays listed: plain close succeeds, the skip diagnostic appears, the session and claim are released, and a new --serial open works. It also settles the stdout-banner question for adb 36.

The run also confirms the one remaining blocker at cd87454: when the vanished serial was the only device, close still fails with android-process-probe-unavailable and keeps the session. The fix is the rule from the review: a header-verified empty inventory counts as omitting the serial. After that change, the same sole-device step should show close ok.

The APP_NOT_INSTALLED result for a selector-less open on the gone serial is separate from this PR and can be tracked under #5376.

thymikee commented Dec 6, 2026

Copy link
Copy Markdown
Member Author

The sole-device case is fixed in f0e08d0. I compared the prior and patched CLI builds on an Android 17 emulator using one TCP transport on a private ADB server. After disconnect, adb devices returned the header and zero rows. The prior head exited 1 with android-process-probe-unavailable and kept a live claim. The patched head exited 0, emitted android_app_stop_skipped_device_absent, and left session list and device status empty. Reconnecting and open --serial in the same workspace worked. The snapshot used android-helper 0.21.47-dev. A malformed or failed inventory still keeps the claim. Both focused suites passed (23 tests), along with typecheck, Fallow, and command-doc checks.

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of f0e08d0

Since the review of cd87454: 1 new · 0 still open · 0 no longer raised

1 finding(s): 1 minor.

🟡 Minor (1)

Where Finding
packages/daemon/src/sessions/session-lifecycle/internal/session-close.ts:555 🆕 Empty adb devices read mid adb-server restart still retires the session and frees its claim

Reviewed with this repository's own Sentinel configuration (202d13506363).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

@thymikee

thymikee commented Dec 6, 2026

Copy link
Copy Markdown
Member Author

This PR is ready at f0e08d0. The earlier findings from cd87454 are fixed, and all 24 checks pass at this commit.

Not blocking, and fine to take or leave: the earlier question about why the daemon owns the absent-serial check on plain close (instead of stopAndroidPackage in platform-android) has no reply yet. One sentence in the PR body would settle it: https://github.com/callstack/agent-device/blob/f0e08d0/packages/daemon/src/sessions/session-lifecycle/internal/session-close.ts#L515

Open threads: the one on header-only adb reads (#5396 (comment)) is benign, because the retire rule is the approved invariant. These threads are fixed at this head and can be resolved: retention wording in app.ts and commands.md (#5396 (comment)), stateless rows in ime-lifecycle.ts (#5396 (comment)), and the redundant requireCurrent in session-close.ts (#5396 (comment)).

Not verified here: the adb-server restart window with a transient header-only list was not run live, and the emulator and Pixel results come from the author and AlexErrant reports, not from a rerun. No tests, build, emulator or adb were run for this review. No platform-android test pins that a header-only adb devices output parses to an empty list.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Dec 6, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Android: a session whose device left adb can't be closed; only daemon stop ends it

2 participants