Skip to content

fix: fail Window calls when the window state cannot be read - #79

Merged
AdamMusa merged 2 commits into
codex/engine-initializationfrom
fix/window-state-read-failure
Nov 2, 2026
Merged

AdamMusa merged 2 commits into
codex/engine-initializationfrom
fix/window-state-read-failure

Conversation

@AdamMusa

Copy link
Copy Markdown
Owner

Depends on #59 (fix/invoke-unhandled-methods-fail-fast, approved at afc3e02). This branch is #59's commit plus one commit (b8893b4). Please review only the last commit; once #59 merges, the diff shrinks to it. #59 itself is not amended.

Problem

On desktop, WindowService reads the window state once (getWindowState()), and with #59 every Window invoke awaits _initWindowStateCompleter before acting. If that read failed (a PlatformException from window_manager, for example), the catch only logged it. The completer never completed, so every pending and later Window call hung on the Dart side with no reply, and Ruby only saw its own timeout.

Fix (services/window.dart)

  • In the catch, complete the completer with a StateError("Window state could not be read: <error>"). Pending calls fail immediately, and later calls fail on their next await, which the backend returns to Ruby as the invoke error.
  • Call ignore() on the completer's future first, so a failure with no call waiting is not reported as an uncaught async error. Calls that do await it still receive the error.
  • _scheduleWindowUpdate skips once the read has failed. There is no window state to diff against. This keeps the base behavior, where updates were never applied after a failed read, and the queued then gets an onError so it cannot surface an unhandled error.

A successful read behaves exactly as in #59.

Tests

test/widgets/invoke_fail_fast_test.dart, new test (9 in the file). It runs on macOS with a window_manager channel whose getBounds throws after 2 ms:

  • center invoked during the read is pending, then fails as soon as the read fails, with a StateError naming "Window state could not be read" and the platform message.
  • A later close settles within one frame with a StateError.
  • A property change after the failure applies nothing: no setBounds, and center/close never reach the platform. No uncaught async errors occur.

Before the fix the pending call never settled (the test failed on settled == false). The earlier race test (a successful read) still passes.

verify_pr.sh (Flutter 3.41.2) at the final head b8893b4:

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

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.
If WindowService's initial window-state read failed, the readiness
completer never completed, so every Window invoke waited forever and
Ruby only saw its own timeout. Complete the completer with a StateError
in the catch so pending and later calls get an immediate error reply,
mark its future as handled so an unobserved failure is not reported as
uncaught, and stop scheduling window updates that have no state to diff
against.
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