Skip to content

fix(cua-driver-rs)(get_window_state): 30s timeout + 2000-node cap for heavy webview apps - #1754

Merged
f-trycua merged 1 commit into
mainfrom
fix/get_window_state-timeout-element-cap
May 29, 2026
Merged

fix(cua-driver-rs)(get_window_state): 30s timeout + 2000-node cap for heavy webview apps#1754
f-trycua merged 1 commit into
mainfrom
fix/get_window_state-timeout-element-cap

Conversation

@f-trycua

@f-trycua f-trycua commented May 29, 2026

Copy link
Copy Markdown
Collaborator

Summary

Closes #1537. Cherry-picked from #1609 (@hippoley) — same fix concept, ported to the post-#1674 path layout (libs/cua-driver/rust/crates/).

When get_window_state walks the AX tree of a pathologically large app (Arc, Safari with many tabs, Electron), AXUIElementCopyAttributeValue can block indefinitely via XPC. There's no built-in deadline, so the MCP server just hangs forever — reported by @obaid on macOS 26.

Two changes, both Rust-only:

  • libs/cua-driver/rust/crates/platform-macos/src/ax/tree.rsMAX_ELEMENTS = 2000 cap on total nodes visited in a single walk. walk_element now threads a shared visited_count + truncated flag; when the cap is hit the walk stops early and TreeWalkResult.truncated = true. The renderer appends a warning line to tree_markdown so the agent knows the partial tree is intentional and existing element indices remain valid.
  • libs/cua-driver/rust/crates/platform-macos/src/tools/get_window_state.rs — wraps the spawn_blocking AX walk in tokio::time::timeout(Duration::from_secs(30)). On expiry returns a descriptive ToolResult::error suggesting capture_mode=vision or a query filter rather than hanging.

Notes

Test plan

  • cargo build --release -p cua-driver on macOS — green
  • cargo check -p platform-windows --target=x86_64-pc-windows-msvc — green (additive struct field doesn't touch Windows code)
  • Live repro on Arc / Safari w/ many tabs (the issue get_window_state hangs forever on heavy webview apps (Arc, Safari w/ many tabs) on macOS 26 #1537 repro): verify get_window_state returns a truncation warning rather than hanging
  • Verify the 30s timeout fires with a clear error message on a known-bad target

cc @obaid for the original repro, @hippoley for the original fix.

Summary by CodeRabbit

  • Bug Fixes
    • Added performance safeguards to prevent hangs when examining complex accessibility interfaces.
    • Implemented timeout protection (30 seconds) for accessibility tree operations with improved error guidance.
    • Enhanced reliability of accessibility tree traversal operations.

Review Change Stack

…apps

Closes #1537. Cherry-picked from #1609 (@hippoley) — same fix concept,
ported to the post-#1674 path layout (`libs/cua-driver/rust/crates/`)
and dropped the Swift backend change (path being deprecated per #1738).

- `ax/tree.rs`: `MAX_ELEMENTS = 2000` cap with `TreeWalkResult.truncated`
  surfaced as a warning line at the end of `tree_markdown`.
- `tools/get_window_state.rs`: `tokio::time::timeout(30s)` wrap on the
  `spawn_blocking` AX walk. On expiry returns a clear error suggesting
  `capture_mode=vision` or a `query` filter rather than hanging
  forever.

Reported by @obaid against Arc / Safari w/ many tabs on macOS 26
where `AXUIElementCopyAttributeValue` blocks indefinitely via XPC on
pathologically large trees.

Co-Authored-By: hippoley <noreply@github.com>
@vercel

vercel Bot commented May 29, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Ignored Ignored May 29, 2026 3:50pm

Request Review

@coderabbitai

coderabbitai Bot commented May 29, 2026

Copy link
Copy Markdown
Contributor

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 048976a4-3966-4707-8d74-03b79943bca7

📥 Commits

Reviewing files that changed from the base of the PR and between 8a77923 and f1390ee.

📒 Files selected for processing (2)
  • libs/cua-driver/rust/crates/platform-macos/src/ax/tree.rs
  • libs/cua-driver/rust/crates/platform-macos/src/tools/get_window_state.rs

📝 Walkthrough

Walkthrough

The PR hardens AX tree traversal against indefinite hangs on heavy webview apps by capping the total node visits at 2,000 and wrapping the walk in a 30-second timeout. The walker now tracks traversal progress and stops early with a truncation flag, while callers receive explicit timeout guidance instead of hanging forever.

Changes

AX Tree Traversal Protection

Layer / File(s) Summary
AX tree node visit cap and truncation tracking
libs/cua-driver/rust/crates/platform-macos/src/ax/tree.rs
MAX_ELEMENTS constant limits total visited nodes; TreeWalkResult gains a truncated: bool field; walk_tree initializes shared visited_count and truncated state passed to walk_element; walk_element enforces early-exit when cap is reached and sets the truncation flag; all recursive calls to walk_element propagate the cap state to halt traversal across the entire depth-first walk; truncation warning is appended to output when limit is hit.
30-second timeout on AX tree walk
libs/cua-driver/rust/crates/platform-macos/src/tools/get_window_state.rs
tokio::time::timeout wrapper replaces unconditional await on walk_tree blocking task; timeout elapsed returns error with guidance to switch capture_mode; walk failure returns underlying error message.

🎯 2 (Simple) | ⏱️ ~12 minutes

🐰 A rabbit bounces past the AX tree so tall,
Says "Let's cap this walk at two-thousand nodes, that's all!"
With a timeout of thirty, no more endless wait,
The apps won't hang heavy—we're fixing their fate! 🌳⏱️

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/get_window_state-timeout-element-cap

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@f-trycua
f-trycua marked this pull request as ready for review May 29, 2026 16:02
@f-trycua
f-trycua merged commit 73a84f5 into main May 29, 2026
10 checks passed
@f-trycua
f-trycua deleted the fix/get_window_state-timeout-element-cap branch May 29, 2026 16:02
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.

get_window_state hangs forever on heavy webview apps (Arc, Safari w/ many tabs) on macOS 26

1 participant