Skip to content

fix: fail fast on invoke methods no handler claims - #59

Merged
AdamMusa merged 1 commit into
codex/engine-initializationfrom
fix/invoke-unhandled-methods-fail-fast
Nov 2, 2026
Merged

AdamMusa merged 1 commit into
codex/engine-initializationfrom
fix/invoke-unhandled-methods-fail-fast

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

Engine-only. This is the single generic fix for the "Ruby call waits for its whole timeout" symptom in wave-3 items 2–4. It fully covers item 4 (fix/window-methods-unsupported-platforms), which therefore has no separate PR.

Root cause

When a control has no invoke listener, Control.invokeMethod waits for one to be added, up to the Ruby timeout. That wait is deliberate: Ruby can send a patch that adds a control and an invoke in the same breath, and the invoke must not lose the race with the control's first build. But the same wait also hung every call that a mounted renderer simply does not handle:

  • Window on mobile and web: WindowService.init() returns before registering a listener when !isDesktopPlatform(), so every Window method waited out its timeout.
  • ContextMenu open on iOS/macOS: the Cupertino renderer registers no listener.

The Cupertino SearchBar open_view/close_view was not a timeout. That renderer registers a listener whose default: throws Unknown SearchBar method, so Ruby got an immediate error. Its native implementation is a separate PR.

Fix

Track whether a control is actually rendered, and fail fast only then:

  • Control: new isAttached / attach() / detach(). Attachment is counted, so a control rendered twice stays attached until both renderings are gone.
  • ControlWidget: a custom StatelessElement attaches the control in mount. In update it moves the attachment when the same element is rebuilt with a different control, and it detaches in unmount. Renderers register listeners in initState, inside that same synchronous mount, and invokes are dispatched between frames. So by the time an invoke is handled, an attached control has registered every listener it ever will.
  • ServiceBinding: attaches after service.init() and detaches on dispose.
  • Control.invokeMethod: an attached control with no listeners now replies immediately with:
    UnsupportedError("Window(30).center is not supported on android: no handler for "center" is registered for this control")
    Unattached controls (not built yet, or never) keep the old wait-for-listener behavior, so the patch-then-invoke race still works.

What Ruby sees: on_result gets the error right away instead of "Timeout waiting for invoke method listener ..." after 10 s or more.

Desktop Window race (fixed after QA review of 3ed74b8)

On desktop, WindowService.init() registered its invoke listener only after an async platform round trip that reads the window state, but ServiceBinding marks the control attached right after init(). A page.window.* call in that gap (for example wait_until_ready_to_show right after startup) would have been rejected as "not supported on macOS"; on the base it reached the platform.

Fix in services/window.dart (5 added lines):

  • The listener is registered synchronously in init().
  • _invokeMethod first awaits the existing _initWindowStateCompleter, so early calls wait for the window state and then act.
  • dispose() removes the listener unconditionally.

Audit of every other registration site: Page, Video, the QR scanner and all core and extension services and controls register their listener synchronously in init()/initState(), the QR scanner also in didUpdateWidget. Window was the only asynchronous one.

Tests

test/widgets/invoke_fail_fast_test.dart (8 tests):

  • A mounted Text (no handlers) settles within one frame, with an UnsupportedError naming the control, the method and the platform. The Ruby timeout is 30 s and fake time is not advanced.
  • The iOS ContextMenu.open invoke settles within one frame with an UnsupportedError.
  • On Android and on iOS, the Window service's center, close and start_resizing settle within one frame with an UnsupportedError. After ServiceBinding.dispose() the control is detached.
  • Race regression: a TextField focus invoked before the field mounts stays pending, then completes and focuses the field once the widget mounts.
  • Desktop Window race: this is QA's qa_e59_race_test.dart probe, kept as a regression test. With macOS and a window_manager channel that answers after 2 ms, wait_until_ready_to_show invoked right after the binding is still pending while the state is read, not rejected. It then completes with no error, and waitUntilReadyToShow reaches the platform after getBounds. Without the window.dart change, this test fails because the call settles immediately as "not supported".
  • A control that never mounts still gets a TimeoutException.
  • Attachment moves when the same element is rebuilt with another control, survives when the control is rendered twice and one copy is removed, and clears on unmount.

Without this fix, the Window invoke on mobile hangs the test for its whole timeout; I hit exactly that while writing #52.

verify_pr.sh (Flutter 3.41.2) at the final head afc3e02, rebased onto the current base f137eec:

PASS engine:ruflet flutter test         +757: All tests passed!
PASS engine:ruflet analyze              No issues found!
RESULT: PASS

git merge-tree shows this merges cleanly with every open engine branch, including #52, which edits the lines just above in invokeMethod.

Follow-ups (separate PRs)

  • fix/cupertino-search-bar-open-close: native iOS open_view/close_view.
  • fix/cupertino-context-menu-open: CupertinoContextMenu has no programmatic open API, so iOS open replies with an error that explains why. The generic message here is correct but says less.

Control.invokeMethod waited up to the Ruby timeout whenever a control
had no invoke listener. That wait exists for invokes that race ahead of
a control's first build, but it also hung every call a mounted renderer
simply does not handle: ContextMenu.open on iOS, and every Window method
on mobile and web, where WindowService registers no listener.

Track whether a control is rendered: ControlWidget's element attaches
the control while mounted (moving the attachment when the widget is
rebuilt with another control), and ServiceBinding attaches services
after init(). An attached control without listeners now replies at once
with an UnsupportedError naming the control, method and platform.
Unmounted controls keep the old wait-for-listener behavior.

On desktop, WindowService used to register its listener only after an
async read of the window state, so an early page.window call would now
be rejected. It registers synchronously in init() and each method waits
for the window state before acting. Every other service and control
already registers its listener synchronously.
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