Skip to content

fix(daemon): concurrency and cancellation safety (review P0-B) - #197

Merged
Derrors merged 2 commits into
mainfrom
fix/p0-daemon
Nov 25, 2026
Merged

Derrors merged 2 commits into
mainfrom
fix/p0-daemon

Conversation

@Derrors

@Derrors Derrors commented Nov 25, 2026

Copy link
Copy Markdown
Owner

What and why

P0, second batch, from the same full review of main after #195 (the daemon's concurrency and cancellation). Independent of #196; based on main.

  • Native AX requests dropped halfway (ictl-ax Session): a background check (the control page's alert and status polls) that gives way to an action is dropped mid-request, which left the DTX stream mid-message for the next request. Now a request dropped before its answer marks the session abandoned (is_closed), so it is opened again; a closed connection ends the wait; writes time out after 5 s as an I/O timeout; an unanswered request still keeps the session.
  • Background checks never restart axauditd (core::BACKGROUND task-local, restart_stuck_ax): they may be dropped halfway; only actions decide that.
  • Waiting only while waiting for the lock (lock_admitted(…, counted)): an action held while its task was paused counted as waiting, and the page's background checks gave way for the whole pause. The Web lane's lock never counts.
  • Main loop: a supervision round asked for (Daemon::wake) that couldn't take the device was only noticed when the loop next woke, up to the 10 s tick; now the loop awaits the running round and asks again 200 ms later. At most 256 clients at once (more wait in the socket's queue); finished connections are reaped before new ones are accepted.
  • Scoped tasks:
    • scope_gate lets the lock go before asking the user, so the app in front could change before the action ran; scope_recheck checks again once the action holds the device lock (no question then: out of scope by now is refused).
    • WebEval is checked against the app that shows the page (Check::Page, in the Web lane after the page is picked), not the one in front.
  • Long loops: TapElement.scroll.max is bounded by the daemon too (1..=15; a raw u32::MAX held the phone for hours). The scroll search and wait_for (WDA and native) stop as soon as may_continue says no (task paused or taken over, caller gone); the search taps nothing then. The search's end check uses unchanged_viewport (complete, non-empty reads only), as scroll_collect does.

Tests

1. Unit tests

  • cargo test --workspace --all-features --locked: 560 passed; clippy -D warnings; cargo fmt --check for all 11 packages; tests/check_regression_cases.py: 0 missing
  • New: a_cancelled_request_abandons_the_session, an_unanswered_request_keeps_the_session, a_closed_connection_ends_the_wait (ictl-ax); waiting_for_a_paused_task_doesnt_hold_off_the_checks, a_scoped_action_is_checked_again_with_the_lock_held, a_tap_by_text_scrolls_within_bounds; within_scope and Check::Page in an_approved_app_joins_the_scope_for_the_rest_of_the_task and what_each_request_checks_against_the_scope
  • No existing test removed, ignored or loosened
  • No unit test for the main loop (serve) itself; tested on the device (below)

2. Real-device regression

  • Device, host: iPhone 15 Pro Max (iPhone16,2), iOS 27.2.1, native control; iPad (iPad11,1) iOS 26.7.3 for the switch; Debian 12
  • tests/device/run.py: 16 calls, 0 failed

3. Regression cases

4. Real-device proof

  • Background checks under load: two threads sending alert_check/status with the background flag nonstop while 24 actions ran (swipes, scroll-search taps, Home, launches), twice: 0 failed actions, ~9300 checks gave way, no AX reconnect or axauditd restart in the log; 4 s after the actions the checks answered again
  • TapElement with scroll.max 16 or 0: refused at once
  • A task scoped to Settings: scroll-search tap, taps and launching Settings went through (the lock-held recheck refused nothing); launching Notes was asked about, rejected, not done
  • wait_for for 20 s, task paused at 2.5 s: answered "interrupted" at 2.7 s (it held the phone for the whole timeout before)
  • Scroll search for a missing item, task paused: stopped after 1 swipe, nothing tapped (not_sent, preflight)
  • web_eval on Safari's page by the Settings-scoped task: asked once for Safari; rejected → not run (not_sent, preflight); approved → run; the next call not asked again
  • Switching iPhone → iPad → iPhone: the iPad ready in 2 s, the iPhone in 0.66 s
  • Untested: an app actually changing between the scope check and the action (not reproducible on purpose; the recheck's refusal path is unit-tested); a page shown by an app not in front (iOS lists only the front app's pages); a macOS host

Checklist

  • MCP tool names, parameters and default outputs stay compatible (D10); the MCP server already limited max_scrolls to 1..=15
  • No new dependencies
  • CHANGELOG.md and CHANGELOG.zh-CN.md (Unreleased)
  • No private data in the outputs above

🤖 Generated with Claude Code

Derrors and others added 2 commits November 25, 2026 19:37
- ictl-ax: a request dropped halfway (a background check giving way) abandons the
  accessibility session instead of leaving its stream mid-message; writes time out;
  a closed connection ends the wait; an unanswered request keeps the session
- background checks never restart the phone's accessibility daemon
- an action counts as waiting for the phone only while it waits for the lock, not
  while its task is paused
- main loop: an asked-for supervision round that couldn't run is retried 200 ms
  later; at most 256 clients at once; finished connections are reaped first
- scoped tasks: the front app is checked again once the action holds the device
  lock; WebEval is checked against the app that shows the page
- TapElement scroll.max is bounded (1..=15); scroll search and wait_for stop when
  the task is paused or the caller goes away

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… lock, tap check, AX reader)

- scope_recheck: an action the user just allowed on a screen whose app WDA
  didn't name goes through while it is still unnamed; an unnamed front app
  otherwise gets the full "can't tell" text (it said "became  ")
- WebEval in a scoped task asks the user without holding the Web Inspector,
  then starts over (lock, task state, device, page) and runs only on the app
  allowed; a page whose host isn't known (unlisted host, WebKit's own process)
  counts as an unknown app and never joins the scope
- tap_element checks may_continue right before tapping
- ictl-ax: a request dropped halfway also stops the session's reader; the
  reader's replies time out after 5 s
- main loop: a round that ran clears a pending retry; supervision rounds come
  before new clients; scope_gate's look-up counts as waiting

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Derrors

Derrors commented Nov 25, 2026

Copy link
Copy Markdown
Owner Author

Review fixes (685220d):

  1. scope_recheck: an action the user just allowed on a screen whose app WDA didn't name goes through while that screen is still unnamed; otherwise an unnamed app gets the full "can't tell which app is in front" text. Verdict split into recheck_front, unit-tested (the_app_in_front_is_judged_again_without_asking).
  2. WebEval in a scoped task: within the scope it runs right away; otherwise web_eval returns Ask(app), the Web Inspector lock is dropped, the user is asked, and the lane starts over (lock_admitted with still, device, web_pages/pick), running only on the app allowed (another app out of scope now: refused without asking). host_bundle: a WebContent process whose host isn't listed, or WebKit's own process, is an unknown app (""), never added to the scope. Tests a_page_outside_the_scope_is_asked_about_once, a_page_s_app_for_the_scope_is_its_host_or_unknown.
  3. tap_element checks may_continue right before tap_at.
  4. ictl-ax: dropping an unfinished request also aborts the session's reader; the reader's replies time out after WRITE_TIMEOUT. a_cancelled_request_abandons_the_session now checks the reader stopped.
  • Main loop: a round that ran clears a pending retry, an asked-for round starting clears it too; tick/wake/retry come before accept under biased. scope_gate's look-up holds a Waiting.

Device (iPhone 15 Pro Max, iOS 27.2.1): while a scoped task's web_eval waited for the user, a web_targets from elsewhere answered in 0.02 s (it waited behind the question before); approve → ran, the next call not asked; reject → refused (not_sent/preflight); in-scope launch/tap/scroll-search not refused by the recheck; pause interrupts wait_for (2.3 s); run.py 16/16; background-poll stress 24 actions 0 failed, no AX reconnect or axauditd restart.

@Derrors
Derrors merged commit 4240009 into main Nov 25, 2026
7 checks passed
@Derrors
Derrors deleted the fix/p0-daemon branch November 25, 2026 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant