Skip to content

fix: stop Citry UI menus and pickers from looping while idle - #483

Merged
JuroOravec merged 1 commit into
mainfrom
fix/cmenu-focus-loop
Oct 28, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
fix/cmenu-focus-loop

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Several Citry UI runtimes kept an idle page busy. Each one wrote an attribute, class, tabIndex or text with the value already in place. That write still queues a MutationObserver record, so an observer that repairs the markup ran again, wrote again, and never stopped. Nothing looked wrong on the page, but it burned CPU and battery while nobody touched it.

What was looping

Before this change, I measured these while idle in Chromium and Firefox:

Component What repeated
Open CMenu / CContextMenu with a submenu 150 to 180 focus() calls a second. updateCurrent rewrote each item's tabIndex; the structure observer did not recognize a submenu trigger's tabindex change as its own, so it reconciled, which focused the current item again.
CSplitButton about 1,700 observer timers a second. Its markup repair rewrote class with classList.add even when the class was there, re-set the loading indicator's hidden, and wrote the trigger's loading attributes and then removed them, all on every pass.
CTimePicker with its CListbox about 600 observer timers a second. The picker and the listbox each rewrote labels and attributes that the other one observes.

A second, smaller bug: when a server re-render updated an open CMenu, the new controller set inert on the surface during setup, which blurred the focused item a second time.

The fix

  • Each write is skipped when the value is already in place. This covers cmenu.py (tabIndex, classes, the submenu's physical-side dataset), cbutton.py (a guarded setAttribute, plus writesLoadingState: false for the split button's trigger, so its loading attributes are not written and removed), clistbox.py, ctime_picker.py, and the shared attr helper in _form_control_runtime.py. That helper did not loop on its own, but it has the same bug, so it is fixed in the same pass.
  • The structure observer recognizes a submenu trigger's tabindex record as one of its own.
  • A controller that takes over an open menu whose item has focus no longer makes the surface inert. It looks for the focused item through shadow roots too. If setup then fails because the CSS anchor name cannot be resolved, the surface is made inert before the error, so it does not stay interactive with nothing driving it.

Each guarded write compares against the live DOM value, so a value that someone else changed is still written back. Only the record of a write that changes nothing goes away.

Tests

  • test_cmenu_e2e.py: test_open_menu_with_a_focused_submenu_item_is_idle, and test_correlated_rerender_does_not_blur_the_focused_item_a_second_time (at most one blur).
  • Idle tests for CSplitButton and CTimePicker, and assertions in the CButton tests.
  • A new guard, quality/tests/e2e/test_idle_scenarios_e2e.py. It opens every browser scenario in the quality catalog in its active state, waits for it to settle, then samples three back-to-back 300 ms windows. It fails a scenario when, in every window, focus() ran 5 or more times or one timer or one kind of DOM change repeated 10 or more times. A real loop is busy in every window, while settling work such as a smooth scroll ending is not. With origin/main's cmenu.py, cbutton.py, ctime_picker.py and clistbox.py swapped in, it fails menu.states and context-menu.states (about 50 focus() calls a window), split-button.states (about 490 timers a window) and time.states (about 195).

Not covered by a test: the shadow-root focus check, and making the surface inert when anchor setup fails.

Cost of the guard, and why it runs weekly

The guard catches a Citry UI component that falls into an idle loop in a quality-catalog scenario's active state, which no ordinary test notices, because the page looks and works the same.

  • It takes 63 to 65 s on Chromium with -n 3, and 3:01 serially.
  • The PR e2e job runs with --dist loadfile, which keeps one file on one worker, so the guard could add up to about 3 minutes to the PR e2e job. That is more than the one minute that CLAUDE.md's "Design verification" rules allow without the maintainer's explicit agreement.
  • So the guard is marked with a new weekly marker. The PR e2e job now runs -m "e2e and not weekly", and the weekly cross-browser job (py--tests-cross-browser.yml, -m e2e) runs the guard on Chromium, Firefox and WebKit, about 3 minutes per browser on one worker. docs/codebase.md describes the marker.
  • The trade-off: a new idle loop can reach main and be reported up to a week later. An alternative is to split the scenarios across about four files, so loadfile spreads them over the four workers at about 45 s each, which would keep the guard on PRs. I did not do that here.

The guard cannot see mutations inside shadow roots, loops inside iframes, or loops that start only after a popup closes. A component that runs a per-frame loop on purpose, as CMenuSubmenu does while a submenu is open, would trip the timer rule. No scenario's active state opens a submenu today.

Asset budget

The context-menu family's raw JS budget in test_asset_report.py goes from 160 KiB to 161 KiB. Only CMenu's shared menu runtime is in that family's JS, and its new write guards and the focus check add 1,096 bytes: the family is 163,292 bytes raw on main and 164,388 on this branch, after I shortened the new comments. Getting back under 160 KiB would mean deleting every new comment and squeezing the code. Gzip is 35,760 bytes against its 35,840-byte budget (80 bytes to spare), and brotli is 29,992 against 30,720, so neither of those budgets changes. The next change to CMenu's runtime will likely need to raise the gzip budget.

Evidence

  • Chromium: 214 passed for the cmenu, csplitbutton, cbutton and ccontext_menu component tests (-n 3 --dist loadfile). An earlier run, before the final comment edits, passed 234 e2e tests across 19 component directories, including the form-control components. The citry_ui non-e2e tests passed (3,299) apart from the asset budget, which this PR updates; the 36 asset report tests now pass.
  • Firefox: the cmenu, csplitbutton and ctime_picker e2e directories and the idle guard pass, except three tests that fail the same way on origin/main: cmenu's test_a_key_scrolls_the_page_to_an_item_below_the_screen (end and open-on-last; the open PR test: make four e2e checks pass on Firefox #460 fixes both and renames the test to test_a_key_shows_an_item_that_starts_below_the_screen) and csplitbutton's test_layers_example_logs_menu_actions_and_opens_the_dialog_for_provenance, a Firefox failure that already exists on main and has no fix in progress. WebKit was not run locally, because WebKit hangs on this machine.
  • ruff, ruff format, mypy (also with --platform linux) and pnpm run citry-ui:check-assets pass. The cargo phases and the full pytest run are left to PR CI.
  • An independent review ran, its findings are folded in, and a re-review of the result ran before this PR.

🤖 Generated with Claude Code

An open CMenu or CContextMenu with a submenu, a CSplitButton, and a
CTimePicker with its CListbox kept an idle page busy. Each runtime wrote an
attribute, class, tabIndex or text with the value already in place. That
write still queues a MutationObserver record, so the observer that repairs
the markup ran again and wrote again. In CMenu the reconcile focused the
current item 150 to 180 times a second.

Each of those writes, and the shared form-control attr helper, now skips a
value that is already in place. The structure observer recognizes a submenu
trigger's tabindex change as its own. A controller that takes over an open
menu with a focused item no longer makes the surface inert, which blurred
the item a second time on a server re-render.

A new quality guard opens every browser scenario and fails when focus(), a
timer or a DOM change repeats in each of three idle windows. It is marked
with the new `weekly` marker and runs in the weekly cross-browser job,
because it could add up to about 3 minutes to the pull-request e2e job. The
context-menu family's raw JS budget goes from 160 KiB to 161 KiB.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec force-pushed the fix/cmenu-focus-loop branch from 51bd454 to 7571754 Compare October 27, 2026 23:25
JuroOravec added a commit that referenced this pull request Oct 28, 2026
… the original re-render test (not for merge)
JuroOravec added a commit that referenced this pull request Oct 28, 2026
@JuroOravec
JuroOravec merged commit a554567 into main Oct 28, 2026
34 of 39 checks passed
@JuroOravec
JuroOravec deleted the fix/cmenu-focus-loop branch October 28, 2026 17:44
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