Skip to content

fix(android): check ownership before a failed boot stops its emulator - #4991

Merged
thymikee merged 6 commits into
0.22from
fix/4867-readiness-stop-ownership
Nov 23, 2026
Merged

thymikee merged 6 commits into
0.22from
fix/4867-readiness-stop-ownership

Conversation

@thymikee

@thymikee thymikee commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Readiness stops the emulator a failed cold boot launched (#4863), gated only by device-shutdown policy. A second open that binds to the booting emulator-NNNN becomes its owner; the first open's later failure stopped it from under that owner. The failed-open cleanup (#4842) already refuses that; readiness had no such answer. Fixes #4867.

EnsureReadyInput gains isAndroidEmulatorHeldByOther, answered from the session and claim state the cleanup reads, under the device's claim lock, so a claim in flight counts too. A held device is never signaled; an unreachable answer also stops nothing, recorded by android_emulator_launch_ownership_unanswered. Ownership is recorded under the serial, so only a boot that resolved one asks; an earlier failure stops its launch as base did. Other routes keep asking nothing (#4993). Keeping a launch is now the boot wait's own knowledge; ADR 0029 reworded; a test pins the policy gate.

Before / after

open canceled after a second session claimed the booting emulator-5560
base: androidEmulator.terminate(4242) -> the other session's emulator stops
head: no terminate call                  -> it keeps running

Validation

Evidence at 7f2da869bc; head ebce477210 adds only a typed diagnostic reason. Reverting readiness sources to base fails 3 of that suite's 35 tests; all 35 pass at head. Green: typecheck, format, daemon, readiness, host, doc-guard suites.

Live, node bin/agent-device.mjs at this head, isolated --state-dir, AVD focus-order-probe-api37, device status before: no claims:

A open canceled 15s after emulator-5560 reported device, no other owner
  -> process gone, no ownership_unanswered diagnostic
B same cancel, live foreign claim on emulator-5560 (claim store, own state dir)
  -> process kept running, nothing terminated
C open canceled at t+4s, before any serial appeared
  -> process gone, no ownership_unanswered diagnostic

Cleanup: sessions closed, scratch claims and state dirs removed, nothing left running.

🤖 Generated with Claude Code

#4863 stops the emulator a cold boot launched when that boot fails, and the
stop is gated only by the `device-shutdown` policy. A second `open` that binds
to the booting `emulator-NNNN` becomes that emulator's owner, and the first
open's later failure stopped it out from under that owner. The daemon's
failed-open cleanup (#4842) already refuses to stop a launch somebody else
holds; the readiness stop had no such answer.

Readiness now asks whoever owns the boot before its stop signals the launched
pid. The daemon answers from the same session and claim state its cleanup
reads, under that device's claim lock, so a claim being written right now
answers as ownership too. A held device is never signaled, whichever reason
failed the boot, and an answer that cannot be reached keeps the launch. A route
that opens no session, such as `boot`, has no ownership to ask about and keeps
following the boot's own rule.

Keeping a launch after its own failure is now the boot wait's own knowledge
rather than an undocumented `bootBudgetSpent` field on the error's details.

Taking the optional notes from #4863's final review: ADR 0029's amendment now
says the emulator host gates its own `terminate` and the launched-process stop
uses the request-level flag, and a composition test pins the shutdown gate into
the composed emulator host's `terminate`.
@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 5.10 MB 5.10 MB +1.2 kB
Package (unpacked) 5.10 MB 5.10 MB +1.2 kB
Package (download) 1.55 MB 1.55 MB +265 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 19.2 ms 22.3 ms +3.1 ms
CLI --help 36.6 ms 45.1 ms +8.5 ms

The readiness stop already keeps a launch when nobody can say who owns the
emulator, but it swallows the reason. It now emits
android_emulator_launch_ownership_unanswered, in the same style as the
terminate refusal beside it, and the open's help, the command docs and ADR 0029
say that an unreachable answer stops nothing.

The failed-open cleanup asks the session half of the same question through
holdsDeviceInAnotherSession, so both stops read ownership one way.
@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: 1 new finding on a403ceb. The verdict and the full list are in the summary comment.

@thymikee

Copy link
Copy Markdown
Member Author

The ownership check in a403ceb covers only boots that start from open. A failed or canceled boot, install, push, apps, doctor or appstate request can still stop an emulator that another session owns.

Only open passes isAndroidEmulatorHeldByOther (session-open.ts:438). Every other route that readies a stopped AVD uses the same ensureEmulatorReady launch and terminate path with no callback, and launchHeldByOtherOwner returns false there (runtime.ts:126). Example: boot --device Pixel_9 launches the AVD, a second client's open binds emulator-5554, claims it and publishes, then the boot request is canceled or fails its probe and terminate(pid) stops the emulator the open session owns. That is the race #4867 describes, so the rule should be: every daemon route whose readiness can terminate a launch asks whether any session other than the request's own, or a foreign claim, holds the device before it signals. The daemon's readiness composition enumerates those routes, so the answer should come from there and not from each route. If #4867 is meant to cover open-launched boots only, a follow-up for the other routes would be enough, and the docs sentence below needs to change.

The new sentence in commands.md:256 sits in a paragraph that ends with "boot and app deployment follow it the same way". Those routes pass no callback, so they still stop a held emulator, and ADR 0029 line 31 says the same. Fixing the finding above makes the sentence true. Otherwise, scope the sentence to open.

This change alters whether a launched emulator process is terminated after a failed or canceled cold boot, which is a device-facing path, and the PR lists device-free validation only. Live evidence is required before merge. Run node bin/agent-device.mjs on an isolated --state-dir and do two runs. First, cold open on a stopped AVD, canceled after emulator-NNNN appears and before boot completes, with no other owner: the emulator process should be gone and no android_emulator_launch_ownership_unanswered diagnostic should appear. Second, the same run with another session or claim holding emulator-NNNN: the emulator should stay running with no terminate. Record the commands, device, SHA and device status before and after each run.

Could the check live where the #4863 policy gate already lives? The daemon-composed emulator host's terminate could take the device beside the pid and ask, under that device's claim lock, whether another session or a foreign claim holds it. It could reuse findSessionHoldingDevice and claimHeldByAnotherOwner, and keep the lock across the bounded stop. Readiness would then need no new EnsureReadyInput field, every daemon route would be covered by construction, and the non-daemon CLI path would keep its current rule. The host terminate signature would need to carry the device and the request's session name, and ADR 0029 line 31 ("a route that opens no session has no ownership to ask about") would need an amendment. A smaller option within the current shape is to build the callback once in the daemon's readiness composition and have it run the stop, so the lock spans the signal.

Not blocking, and these can be taken or left: the answer is read and the claim lock released before terminateLaunch runs (runtime.ts:108), while stopUnownedLaunch re-asks and stops inside the lock to close that window; the test at runtime.test.ts:715 stubs a true answer for an AVD id that production cannot give, and no test drives open, readiness failure and no terminate through the daemon route; the BootWaitProgress refactor and the throwaway { budgetSpentWithBootRunning: false } object are #4863 follow-ups that this fix did not need; and the PR body says the #4863 policy gate "still decides first", while at this commit ownership is asked first and the gate runs inside the host terminate.

One lower-priority open inline thread, on the AVD-keyed question before discovery returns, still applies.

The code needs the changes above. The 5 non-passing checks are cancelled Body rules runs that look superseded by PR-body edits, and the other 17 checks pass. Not verified here: that the latest Body rules run succeeded, that any tests were run, that the six base-failing tests fail at base, and that the finding above reproduces live. There are no conflicts. Before merge, cover every daemon route that can terminate a failed boot's launch (or rescope #4867 and fix the boot and deployment sentence in commands.md), then attach the live evidence for the failed cold-open stop and keep paths.

Sessions and device claims are keyed under the emulator's serial, so an AVD
name is not an address anything can be held under. Asking ownership about a
boot that never resolved a serial could therefore only answer "not held", which
is a guess at somebody else's emulator: the guard was vacuous exactly when the
boot failed before discovery named the serial, and the stop killed the
emulator a concurrent open had already taken.

Readiness now treats that answer as unknown and keeps the launch, the same way
it treats an unreachable one, and records
android_emulator_launch_ownership_unanswered with the reason. No re-resolution
is added: whoever resolves the serial is the open that binds to it.

The daemon test pinned the vacuous false answer for an AVD; it asks about a
serial nobody holds instead, which is the other real answer.
…boot

Only `open` reads the session and claim state that answers ownership, and only
an `open` publishes the session a held device would belong to. `boot`, app
deployment, `apps`, `appstate`, `push` and `doctor` keep the boot's own rule and
stop their launch; covering every route from the daemon's readiness composition
is filed as a follow-up (#4867 comment r4660185881 finding).

The commands doc sentence and ADR 0029 said the whole boot-time rule, including
`boot` and deployment, stops nothing when the device is held. They now state the
scope: only `open` asks who holds the emulator.
@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

The pre-serial pick is fixed at 793a34b, but the new keep path orphans an emulator on an early-canceled open. CI is green: 16 checks, 0 not passing at 793a34b. No conflicts. Before merge, the stop must be restored for that case and live evidence attached.

runtime.ts:134 now answers "held" whenever bootDevice has no emulator-NNNN id. That id stays the AVD name from launch until discovery lists the emulator (runtime.ts:73-85). Any failure in that window now keeps the launch. A cold open canceled in its first seconds, or a discovery error, leaves the emulator running. The failed-open cleanup never sees it, because onAndroidEmulatorLaunch fires only on success (runtime.ts:91-98). Base 0.22 stopped this launch on any non-timeout failure, which is the "no emulator left running with no session to own it" rule from #4863. The race this guards against needs another client to claim the serial within one poll. The rule to satisfy: before keeping or stopping a pre-serial launch, resolve the address that ownership is recorded under. On failure with no serial, run one bounded discover for the selected AVD that does not use the aborted request signal. If no running emulator-NNNN for that AVD is listed, nobody can hold it, so stop. If one is listed, ask isAndroidEmulatorHeldByOther about that serial. Keep the launch only when the discover or the answer fails. Reverting to asking nothing before a serial, as on base, also meets the rule. A runtime test for cancel-before-discovery should assert that terminate is called.

The PR changes whether a launched emulator process is terminated after a failed or canceled cold boot. That is a device-facing lifecycle path, and the PR body still lists only device-free validation at a403ceb. Required before merge: on an isolated --state-dir with node bin/agent-device.mjs at this head, attach three runs with commands, AVD, SHA and device status before and after. (1) A cold open on a stopped AVD, canceled after emulator-NNNN appears and before boot completes, with no other owner. The emulator process must be gone and no android_emulator_launch_ownership_unanswered may appear. (2) The same run with another session or claim holding emulator-NNNN. The emulator must stay running and nothing may be terminated. (3) A cold open canceled before emulator-NNNN appears, once the first finding is settled, showing the chosen outcome.

The thread at #4991 (comment) (pre-serial AVD stop) is fixed at this head, in runtime.ts rather than the cited file, and its cost is the first finding.

Not blocking, and fine to take or leave: ADR 0029 line 31 has a stray "—." and says twice that boot and app deployment stop their launch, and it names a follow-up for the other routes without an issue, so dropping the duplicate clause and filing and citing that issue would tidy it.

Not verified here: tests were not run locally, the length of the pre-serial window on a real host was not measured, and the "6 fail at base / 73 pass" count refers to a403ceb and was not re-checked for this head. The earlier findings are at #4991 (comment).

Ownership is recorded under the emulator's serial, so a boot that failed
before discovery named one has nothing to ask about. Settling that address
would take one more inventory pass, and every inventory probe runs under the
request's own signal, which a canceled request has already aborted: the pass
could only fail. Ask ownership only of a boot that resolved a serial, and let
the earlier window stop its launch as base did, so no emulator is left running
with no session to own it (#4863).

Issue #4993 tracks giving that window, and every other daemon route that can
stop a failed boot's launch, an ownership answer from state that needs no
inventory.

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>
@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: 1 new finding on 7f2da86. The verdict and the full list are in the summary comment.

@thymikee

Copy link
Copy Markdown
Member Author

Fixed at 7f2da86 with the second option you allowed: the pre-serial launch asks nothing and stops, as on base (runtime.ts:137). a cancel that arrives before discovery names a serial stops the launch asserts terminate, and a failure before a serial is resolved stops the launch without asking covers the discovery-error case.

I did not take the bounded discover. Every inventory probe runs its command under the request's own scope signal, which a canceled request has already aborted, and watchCommandAbort settles an aborted signal as a failed command at spawn (exec-trace.ts:181). A fresh AbortSignal.timeout on the discover call cannot reach those probes: listLocalDeviceInventory reads the scope from the request's AsyncLocalStorage context. So that pass answers "failed" for exactly the canceled case it was meant to settle, and keeps the launch. open asks only where the serial is already resolved. Getting an answer for that window, and for the routes that ask nothing, is #4993, which now covers both.

ADR 0029 line 31: duplicate clause dropped, the stray —. is gone, and it cites #4993 (0029-daemon-policy.md:31). help open and website/docs/docs/commands.md state the same rule.

Live evidence at this head is in the PR body: three open runs on AVD focus-order-probe-api37 under an isolated --state-dir, node bin/agent-device.mjs, device status clean before and after. Canceled after the serial reported device with no other owner, the emulator is gone with no ownership_unanswered; the same cancel with a live claim for that serial on the host keeps it running; canceled at t+4s, before any serial appeared, it is gone with no diagnostic. Scratch claims and state dirs are removed and no emulator is left.

android_emulator_launch_ownership_unanswered recorded the ownership caller's
error message as its `reason`. Report the typed reason code
`ownership-query-failed` there and carry the message as `error`, so a reader of
the diagnostic log groups unanswered ownership questions by why, not by the
text each caller happened to throw.

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>
@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of ebce477

Since the review of 7f2da86: 0 new · 0 still open · 1 no longer raised (1 marked fixed)

No open findings.

Not in the reviewers' prompt (1 file)
  • packages/command-surface/src/management/app.ts: its very long or mostly numeric lines were cut; reviewers could still search them

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

💬 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

Copy link
Copy Markdown
Member Author

Both problems from the earlier review (793a34b, #4991 (comment)) are fixed at ebce477, and the code looks good. A launch that has no serial yet now stops as on base, with a cancel-before-discovery test that asserts the terminate. The PR also adds live stop, keep and pre-serial evidence. The ownership-query failure now reports reason 'ownership-query-failed' with the thrown message, and runtime.test.ts asserts the full payload, so the thread at #4991 (comment) can be resolved. The thread at #4991 (comment) does not apply any more: the pre-serial window is accepted as meeting #4867 and tracked in #4993, so it can be resolved too. All 17 checks pass at this commit, and no conflicts were reported. Not verified here: the tests were not run locally, and the '3 of 35 fail at base' count was not reproduced. The live runs were made at 7f2da86, and only the diagnostic payload changed since. The PR body summarizes those runs, so the full command lines and device status output were not reproduced. No further change is needed before merge.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 23, 2026
@thymikee
thymikee merged commit 2f0f17b into 0.22 Nov 23, 2026
17 checks passed
@thymikee
thymikee deleted the fix/4867-readiness-stop-ownership branch November 23, 2026 07:18
thymikee added a commit that referenced this pull request Nov 23, 2026
…#4991)

* fix(android): check ownership before a failed boot stops its emulator

stop is gated only by the `device-shutdown` policy. A second `open` that binds
to the booting `emulator-NNNN` becomes that emulator's owner, and the first
open's later failure stopped it out from under that owner. The daemon's
failed-open cleanup (#4842) already refuses to stop a launch somebody else
holds; the readiness stop had no such answer.

Readiness now asks whoever owns the boot before its stop signals the launched
pid. The daemon answers from the same session and claim state its cleanup
reads, under that device's claim lock, so a claim being written right now
answers as ownership too. A held device is never signaled, whichever reason
failed the boot, and an answer that cannot be reached keeps the launch. A route
that opens no session, such as `boot`, has no ownership to ask about and keeps
following the boot's own rule.

Keeping a launch after its own failure is now the boot wait's own knowledge
rather than an undocumented `bootBudgetSpent` field on the error's details.

Taking the optional notes from #4863's final review: ADR 0029's amendment now
says the emulator host gates its own `terminate` and the launched-process stop
uses the request-level flag, and a composition test pins the shutdown gate into
the composed emulator host's `terminate`.

* fix(android): record an unanswered ownership question

The readiness stop already keeps a launch when nobody can say who owns the
emulator, but it swallows the reason. It now emits
android_emulator_launch_ownership_unanswered, in the same style as the
terminate refusal beside it, and the open's help, the command docs and ADR 0029
say that an unreachable answer stops nothing.

The failed-open cleanup asks the session half of the same question through
holdsDeviceInAnotherSession, so both stops read ownership one way.

* fix(android): keep a launch whose boot never resolved a serial

Sessions and device claims are keyed under the emulator's serial, so an AVD
name is not an address anything can be held under. Asking ownership about a
boot that never resolved a serial could therefore only answer "not held", which
is a guess at somebody else's emulator: the guard was vacuous exactly when the
boot failed before discovery named the serial, and the stop killed the
emulator a concurrent open had already taken.

Readiness now treats that answer as unknown and keeps the launch, the same way
it treats an unreachable one, and records
android_emulator_launch_ownership_unanswered with the reason. No re-resolution
is added: whoever resolves the serial is the open that binds to it.

The daemon test pinned the vacuous false answer for an AVD; it asks about a
serial nobody holds instead, which is the other real answer.

* docs(4867): scope the ownership answer to the open that launched the boot

Only `open` reads the session and claim state that answers ownership, and only
an `open` publishes the session a held device would belong to. `boot`, app
deployment, `apps`, `appstate`, `push` and `doctor` keep the boot's own rule and
stop their launch; covering every route from the daemon's readiness composition
is filed as a follow-up (#4867 comment r4660185881 finding).

The commands doc sentence and ADR 0029 said the whole boot-time rule, including
`boot` and deployment, stops nothing when the device is held. They now state the
scope: only `open` asks who holds the emulator.

* fix(android): stop a launch whose boot failed before a serial exists

Ownership is recorded under the emulator's serial, so a boot that failed
before discovery named one has nothing to ask about. Settling that address
would take one more inventory pass, and every inventory probe runs under the
request's own signal, which a canceled request has already aborted: the pass
could only fail. Ask ownership only of a boot that resolved a serial, and let
the earlier window stop its launch as base did, so no emulator is left running
with no session to own it (#4863).

Issue #4993 tracks giving that window, and every other daemon route that can
stop a failed boot's launch, an ownership answer from state that needs no
inventory.

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>

* fix(android): keep the ownership diagnostic's reason a code

android_emulator_launch_ownership_unanswered recorded the ownership caller's
error message as its `reason`. Report the typed reason code
`ownership-query-failed` there and carry the message as `error`, so a reader of
the diagnostic log groups unanswered ownership questions by why, not by the
text each caller happened to throw.

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>

---------

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>
hannojg pushed a commit to hannojg/agent-device that referenced this pull request Nov 23, 2026
…callstack#4991)

* fix(android): check ownership before a failed boot stops its emulator

stop is gated only by the `device-shutdown` policy. A second `open` that binds
to the booting `emulator-NNNN` becomes that emulator's owner, and the first
open's later failure stopped it out from under that owner. The daemon's
failed-open cleanup (callstack#4842) already refuses to stop a launch somebody else
holds; the readiness stop had no such answer.

Readiness now asks whoever owns the boot before its stop signals the launched
pid. The daemon answers from the same session and claim state its cleanup
reads, under that device's claim lock, so a claim being written right now
answers as ownership too. A held device is never signaled, whichever reason
failed the boot, and an answer that cannot be reached keeps the launch. A route
that opens no session, such as `boot`, has no ownership to ask about and keeps
following the boot's own rule.

Keeping a launch after its own failure is now the boot wait's own knowledge
rather than an undocumented `bootBudgetSpent` field on the error's details.

Taking the optional notes from callstack#4863's final review: ADR 0029's amendment now
says the emulator host gates its own `terminate` and the launched-process stop
uses the request-level flag, and a composition test pins the shutdown gate into
the composed emulator host's `terminate`.

* fix(android): record an unanswered ownership question

The readiness stop already keeps a launch when nobody can say who owns the
emulator, but it swallows the reason. It now emits
android_emulator_launch_ownership_unanswered, in the same style as the
terminate refusal beside it, and the open's help, the command docs and ADR 0029
say that an unreachable answer stops nothing.

The failed-open cleanup asks the session half of the same question through
holdsDeviceInAnotherSession, so both stops read ownership one way.

* fix(android): keep a launch whose boot never resolved a serial

Sessions and device claims are keyed under the emulator's serial, so an AVD
name is not an address anything can be held under. Asking ownership about a
boot that never resolved a serial could therefore only answer "not held", which
is a guess at somebody else's emulator: the guard was vacuous exactly when the
boot failed before discovery named the serial, and the stop killed the
emulator a concurrent open had already taken.

Readiness now treats that answer as unknown and keeps the launch, the same way
it treats an unreachable one, and records
android_emulator_launch_ownership_unanswered with the reason. No re-resolution
is added: whoever resolves the serial is the open that binds to it.

The daemon test pinned the vacuous false answer for an AVD; it asks about a
serial nobody holds instead, which is the other real answer.

* docs(4867): scope the ownership answer to the open that launched the boot

Only `open` reads the session and claim state that answers ownership, and only
an `open` publishes the session a held device would belong to. `boot`, app
deployment, `apps`, `appstate`, `push` and `doctor` keep the boot's own rule and
stop their launch; covering every route from the daemon's readiness composition
is filed as a follow-up (callstack#4867 comment r4660185881 finding).

The commands doc sentence and ADR 0029 said the whole boot-time rule, including
`boot` and deployment, stops nothing when the device is held. They now state the
scope: only `open` asks who holds the emulator.

* fix(android): stop a launch whose boot failed before a serial exists

Ownership is recorded under the emulator's serial, so a boot that failed
before discovery named one has nothing to ask about. Settling that address
would take one more inventory pass, and every inventory probe runs under the
request's own signal, which a canceled request has already aborted: the pass
could only fail. Ask ownership only of a boot that resolved a serial, and let
the earlier window stop its launch as base did, so no emulator is left running
with no session to own it (callstack#4863).

Issue callstack#4993 tracks giving that window, and every other daemon route that can
stop a failed boot's launch, an ownership answer from state that needs no
inventory.

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>

* fix(android): keep the ownership diagnostic's reason a code

android_emulator_launch_ownership_unanswered recorded the ownership caller's
error message as its `reason`. Report the typed reason code
`ownership-query-failed` there and carry the message as `error`, so a reader of
the diagnostic log groups unanswered ownership questions by why, not by the
text each caller happened to throw.

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>

---------

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>
thymikee added a commit that referenced this pull request Nov 24, 2026
…#4991)

* fix(android): check ownership before a failed boot stops its emulator

stop is gated only by the `device-shutdown` policy. A second `open` that binds
to the booting `emulator-NNNN` becomes that emulator's owner, and the first
open's later failure stopped it out from under that owner. The daemon's
failed-open cleanup (#4842) already refuses to stop a launch somebody else
holds; the readiness stop had no such answer.

Readiness now asks whoever owns the boot before its stop signals the launched
pid. The daemon answers from the same session and claim state its cleanup
reads, under that device's claim lock, so a claim being written right now
answers as ownership too. A held device is never signaled, whichever reason
failed the boot, and an answer that cannot be reached keeps the launch. A route
that opens no session, such as `boot`, has no ownership to ask about and keeps
following the boot's own rule.

Keeping a launch after its own failure is now the boot wait's own knowledge
rather than an undocumented `bootBudgetSpent` field on the error's details.

Taking the optional notes from #4863's final review: ADR 0029's amendment now
says the emulator host gates its own `terminate` and the launched-process stop
uses the request-level flag, and a composition test pins the shutdown gate into
the composed emulator host's `terminate`.

* fix(android): record an unanswered ownership question

The readiness stop already keeps a launch when nobody can say who owns the
emulator, but it swallows the reason. It now emits
android_emulator_launch_ownership_unanswered, in the same style as the
terminate refusal beside it, and the open's help, the command docs and ADR 0029
say that an unreachable answer stops nothing.

The failed-open cleanup asks the session half of the same question through
holdsDeviceInAnotherSession, so both stops read ownership one way.

* fix(android): keep a launch whose boot never resolved a serial

Sessions and device claims are keyed under the emulator's serial, so an AVD
name is not an address anything can be held under. Asking ownership about a
boot that never resolved a serial could therefore only answer "not held", which
is a guess at somebody else's emulator: the guard was vacuous exactly when the
boot failed before discovery named the serial, and the stop killed the
emulator a concurrent open had already taken.

Readiness now treats that answer as unknown and keeps the launch, the same way
it treats an unreachable one, and records
android_emulator_launch_ownership_unanswered with the reason. No re-resolution
is added: whoever resolves the serial is the open that binds to it.

The daemon test pinned the vacuous false answer for an AVD; it asks about a
serial nobody holds instead, which is the other real answer.

* docs(4867): scope the ownership answer to the open that launched the boot

Only `open` reads the session and claim state that answers ownership, and only
an `open` publishes the session a held device would belong to. `boot`, app
deployment, `apps`, `appstate`, `push` and `doctor` keep the boot's own rule and
stop their launch; covering every route from the daemon's readiness composition
is filed as a follow-up (#4867 comment r4660185881 finding).

The commands doc sentence and ADR 0029 said the whole boot-time rule, including
`boot` and deployment, stops nothing when the device is held. They now state the
scope: only `open` asks who holds the emulator.

* fix(android): stop a launch whose boot failed before a serial exists

Ownership is recorded under the emulator's serial, so a boot that failed
before discovery named one has nothing to ask about. Settling that address
would take one more inventory pass, and every inventory probe runs under the
request's own signal, which a canceled request has already aborted: the pass
could only fail. Ask ownership only of a boot that resolved a serial, and let
the earlier window stop its launch as base did, so no emulator is left running
with no session to own it (#4863).

Issue #4993 tracks giving that window, and every other daemon route that can
stop a failed boot's launch, an ownership answer from state that needs no
inventory.

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>

* fix(android): keep the ownership diagnostic's reason a code

android_emulator_launch_ownership_unanswered recorded the ownership caller's
error message as its `reason`. Report the typed reason code
`ownership-query-failed` there and carry the message as `error`, so a reader of
the diagnostic log groups unanswered ownership questions by why, not by the
text each caller happened to throw.

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>

---------

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>
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.

1 participant