Skip to content

Stop keybinding scans from looping on mocked APIs - #8876

Closed
yashranaway wants to merge 7 commits into
omacom:quattrofrom
yashranaway:fix/keybinding-scan-noop-iteration
Closed

yashranaway wants to merge 7 commits into
omacom:quattrofrom
yashranaway:fix/keybinding-scan-noop-iteration

Conversation

@yashranaway

@yashranaway yashranaway commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

The keybindings menu can hang on ipairs(hl.get_monitors()) and miss user bindings after loading Omarchy's console config. This fixes both paths. Addresses #8819 and #7025.

<<<< AI wording below >>>>

Problem

The offline Lua scanner returns a fallback object for every unknown key. ipairs() never sees nil and can loop forever. The same scan also reaches qconsole.lua without an active monitor; its mock raises during a numeric comparison, so the scanner stops before loading the user's bindings.

Fix

Return nil for numeric indexes on the fallback, API, and dispatcher mocks. Return nil for runtime workspace and active-monitor queries while scanning config offline, allowing the console setup to skip sizing and continue to user bindings.

Verification

The keybindings menu test passes in an Alpine container with Lua 5.4 and xkbcli. It checks all three iterable mocks and confirms a user binding after Omarchy's console config renders with its chord. Shell syntax, command-style checks, and git diff --check pass.

Copilot AI balanced review requested due to automatic review settings August 29, 2026 06:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Prevents mocked Lua API results from causing infinite ipairs() scans.

Changes:

  • Return nil for numeric fallback-proxy indexes.
  • Add a timeout-backed regression scenario.
  • The test must still validate timeout success explicitly.

Tip

If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 1 comment.

File Description
bin/omarchy-menu-keybindings Terminates numeric fallback iteration.
test/shell.d/keybindings-menu-test.sh Adds the regression scenario and timeout.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread test/shell.d/keybindings-menu-test.sh Resolved
yashranaway and others added 2 commits August 29, 2026 06:45
The appended loop never executed. The scan stops at the config's first Lua error, and Omarchy's own defaults raise one at default/hypr/qconsole.lua:64 while `require("default.hypr.omarchy")` runs on hyprland.lua:14 — sixteen lines before the end of the file. Reverting the sentinel change left the test at 15 passed in 0.3s, so it proved nothing about the fix it was added for.

Prepending the loop puts it ahead of that abort: the test now times out at 5s without the sentinel change and passes with it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed at head 62db589, with the tests run on a disposable Omarchy VM rather than locally. The hang is real and the mechanism you chose is the right one, but the regression test that shipped with it did not exercise the fix — I pushed a one-line correction for that. The rest below is for the maintainer to decide, not for you to action.

The mechanism is correct, and it is the only one that works. I probed the alternatives on a worker: __len returning 0 does not stop ipairs (it still loops forever), __ipairs is ignored in Lua 5.4 and later, and returning {} for numeric keys loops forever too since {} is non-nil. ipairs reads t[i] through __index until it gets nil and consults nothing else, so returning nil for numeric keys is the only thing that terminates it. Worth recording so nobody "improves" this later with __len.

The regression test passes on the unfixed code. Mutation-tested: with your __index change reverted on the worker, test/shell.d/keybindings-menu-test.sh reported 15 passed, 0 failed in 0.33s. The loop appended at line 22 lands at the end of ~/.config/hypr/hyprland.lua and the scan never reaches it — hyprland.lua:14 requires default.hypr.omarchy, which requires default/hypr/qconsole.lua, whose fit() call at line 79 evaluates monitor.scale <= 0 at line 64 and raises attempt to compare table with number. The pcall(dofile, config) at bin/omarchy-menu-keybindings:238 swallows it, so the scan ends sixteen lines above your loop.

I pushed 6197459, which prepends the loop instead of appending it. It now times out at 5s with the sentinel change reverted and passes in under a second with it, so timeout 5 does the work you meant it to.

A consequence of that same abort: #8819's own repro does not reproduce on quattro. The reporter's loop sits in ~/.config/hypr/monitors.lua, required at hyprland.lua:19 — after the qconsole abort at line 14. I ran that exact shape with the sentinel change reverted and it exits in 1s instead of hanging. The hang needs the loop to run ahead of Omarchy's own defaults. (#7025 is the same report, filed earlier.)

The abort is a bug of its own and costs users their bindings today. With an o.bind(...) added to ~/.config/hypr/bindings.lua (required at hyprland.lua:21), the scan recovers 226 binds and not one of them is the user's, because it died at qconsole first. That is pre-existing on quattro and not something this PR caused, but it is half of what #8819 describes and this PR does not touch it.

The change has a cost worth naming. Before it, hl.get_monitors()[1].name returned the chainable sentinel and worked; after it, [1] is nil and .name raises, which pcall swallows and which truncates the scan at that line. Every hl.bind below it loses its source metadata, so those rows still render from hyprctl binds but reach dispatch_binding with an empty dispatcher and silently do nothing when selected. In practice the qconsole abort already truncates the scan earlier on a stock install, so this only bites a config that indexes numerically before hyprland.lua:14 — but it is a real trade rather than a free win.

There is a competing PR: #7564. It makes the same numeric-nil change and additionally gives the sentinel __len, __lt/__le, arithmetic, __concat and __tostring so load-time reads degrade instead of raising, plus a timeout 10 around the scan. I ran its sentinel against a stock config on the worker: no abort, and the user's own bind is recovered (227 binds against this PR's 226). Which of the two to take, or whether to take both, is the maintainer's call — I have not touched #7564.

One shape neither this PR nor the sentinel closes. noop is still truthy, so while hl.some_api() do and for value in hl.iterator() do spin forever exactly as before. #7564's timeout around the scan would bound those; this change would not.

Checked by me and by codex at xhigh reasoning as an independent second reviewer. Codex reached the same two conclusions I did — its independence is not currently guaranteed, since it can read this session's own files — but it contributed two things I had not reasoned about. First, overriding ipairs inside the scanner process rather than changing the sentinel (local native_ipairs = ipairs; ipairs = function(value) if value == noop then return function() end, value, 0 end return native_ipairs(value) end) terminates iteration while leaving hl.get_monitors()[1].name chain-safe, which avoids the trade above entirely. Second, table.insert(noop, value) writes into the shared sentinel's raw table and contaminates every later mocked result. Neither is a defect in what you wrote; both matter if this and #7564 get reconciled.

Tests: test/shell.d/keybindings-menu-test.sh on a worker, 15 passed with the fix and a 5s timeout without it, plus direct Lua probes of the metatable alternatives. Waiting on the maintainer to choose between this and #7564; nothing further needed from you unless that choice lands here.

@yashranaway

Copy link
Copy Markdown
Contributor Author

Thanks, I reviewed the prepended regression and will leave the sentinel tradeoff and #7564 choice to maintainers.

@omarchybot

Copy link
Copy Markdown
Collaborator

Re-reviewed at 6197459, unchanged since the last pass. This only answers the review thread above, and corrects something I got wrong in my own last comment.

Copilot's comment does not hold at this head, and your fix does work — though not for the reason either of you gave.

The strict mode does stop the test on a timeout. Bash's rule is that a simple command consisting only of an assignment takes the exit status of its last command substitution, so rendered=$(keybindings) returns 124 when timeout kills the scan and errexit aborts there. That rule has a well-known hole — local rendered=$(...), or declare/export/readonly, swallows the status, because the status becomes that of local — and none of the eight sites (lines 71, 119, 139, 152, 165, 177, 191, 203) is written that way or sits anywhere errexit is suppressed. So it lands.

Proven by mutation on a disposable Omarchy VM rather than argued: reverting the numeric-nil guard in bin/omarchy-menu-keybindings and running test/shell.d/keybindings-menu-test.sh gives 0 passed, 0 failed, exit 124 — the file aborts at line 71 before a single assertion runs. Unmutated, 15 passed.

The partial-output scenario could not have happened even before you added strict mode. Two barriers, either one enough. The Lua scan's print at bin/omarchy-menu-keybindings:219 writes into the process-substitution pipe that build_lua_bind_cache consumes at line 76, which only fills associative arrays — those records never reach stdout at all. And build_lua_bind_cache runs at line 518, before dynamic_bindings or any display output; on a cache miss the whole thing is redirected into a mktemp file at line 551 and only mvd into place on success. A killed scan emits nothing, not partial rows. Running the same mutation with line 3 deleted confirms it: the test still failed, at [[ -n $rendered ]] on line 72, with rendered empty. Copilot's bash reasoning was sound in general; its premise about this program was wrong.

Strict mode is still worth keeping. It fails at the assignment rather than relying on the next assertion happening to be the emptiness check, and it time-boxes all eight scans in the file rather than one.

I overstated something last time. I wrote that returning nil for numeric keys "is the only thing that works". That is right about the sentinel's own metamethods — __len, __ipairs and returning {} all still loop — but too broad as stated. The scan runs in its own lua process, so it can also replace the global ipairs before dofileing the config:

local builtin_ipairs = ipairs

ipairs = function(value)
  if value == noop then
    return function() return nil end, value, 0
  end

  return builtin_ipairs(value)
end

Checked on the same VM under Lua 5.5: ipairs(noop) yields zero iterations, real tables iterate normally, and noop[1].name still answers — which is exactly the chain-safety the numeric-nil approach gives up. Under the fix as shipped, noop[1].name raises attempt to index a nil value. Nothing in this tree relies on that chain, so it is a trade-off rather than a defect, and not a request to change anything: it is the maintainer's call, and the same call as choosing between this and #7564.

Nothing else changed, and nothing is needed from you. Still waiting on the maintainer.

Reviewed by Claude Opus 5 in Claude Code with a second opinion from Codex at xhigh reasoning, tests run on a disposable Omarchy VM rather than locally. Codex's first run reviewed the wrong commit and was re-run pinned to this head; the ipairs alternative above came from the pinned run and is its own contribution, not agreement with something already written down.

@twagner12

Copy link
Copy Markdown
Contributor

Confirming this fixes the reported case on Lua 5.5.1 — with this patch applied, ipairs(hl.get_monitors()) terminates and the scan completes.

Two other stubs in the same file have the identical shape and aren't covered by patching noop alone: dsp_proxy (line 169) and the hl fallback metatable (line 223). Both answer every key, including integer ones, so they're endless sequences too.

Tested with exactly this PR's change applied (only noop patched), using a synthetic hyprland.lua against a throwaway $HOME:

config pattern with this PR
ipairs(hl.get_monitors()) (the reported case) ok
ipairs(hl) still hangs
ipairs(hl.dsp) still hangs
ipairs(hl.dsp.anything) still hangs

The same guard on those two __index functions closes them. Factoring it out keeps it to one explanation:

-- ipairs() stops at the first nil, so a stub whose __index answers every key is
-- an endless sequence and iterating one never terminates. Integer keys have to
-- report "no such element" instead.
local function sequence_probe(key)
  return math.type(key) == "integer"
end

Two things worth recording, both dead ends on 5.5.1 — I tried them before landing on the same approach you took:

  • __len returning 0 does not stop ipairs (it iterates by index and ignores __len).
  • __ipairs is not honored — it was removed in Lua 5.4.

One symptom I didn't see mentioned in this PR or the linked issues: the hang doesn't only burn CPU, it silently truncates the menu. Any hl.bind after the offending loop is never printed, so those keybindings just disappear. In my repro the bind before the loop appeared and the one after it did not — worth a line in the PR description, since it makes this a correctness bug and not only a performance one.

Verified no regression either way: against a real config the scan emits an identical 227 bindings before and after.

@calledtoconstruct

Copy link
Copy Markdown

This nil-for-numeric-index change is the right fix for the ipairs spin. It is not the whole scanner bug.

Stock default/hypr/qconsole.lua does monitor.scale <= 0. Under the stub, monitor.scale is the noop table, which has no __le, and Lua raises attempt to compare table with number. The scan pcalls that, so it never reaches the user's ~/.config/hypr/bindings.lua (required after omarchy in hyprland.lua). #7025 / #8819 / #9052 / #9395 / #10214 stay wrong for stock configs if this lands alone: the menu still shows user binds as SUPER + → … with a blank chord.

#10388 (closed as a duplicate of this) added comparison and arithmetic metamethods on the noop and a regression that a user bind after require("default.hypr.omarchy") renders as SUPER + Y. Without that, this PR and #10212 / #7564 still conflict in omarchy-menu-keybindings and none of them close the qconsole abort.

If this is the one that merges, please take the qconsole stub and that user-bind assertion from #10388. Happy to reopen #10388 as a follow-up on top of this if you would rather keep this PR to the ipairs guard only.

@yashranaway

Copy link
Copy Markdown
Contributor Author

You're right about the qconsole abort; the updated scanner skips monitor sizing when no compositor is present, and a regression now checks a user bind after qconsole.

@omarchybot

Copy link
Copy Markdown
Collaborator

Re-reviewed at 42b43b3, merged locally onto current quattro (c05d901; it merges cleanly), with everything run on a disposable Omarchy VM under Lua 5.5.1.

The head fixes what it says. test/shell.d/keybindings-menu-test.sh passes 17 of 17 on this branch merged with quattro. On plain quattro with the same test file it times out (exit 124) before any assertion runs, so the regression does fail without the fix. The stock-config case is closed too: a user o.bind in ~/.config/hypr/bindings.lua is recovered once qconsole no longer aborts the scan. ./test/cli has one failure, vscode generated theme references current theme file, and it fails the same way on quattro, so it has nothing to do with this change.

One side effect is new at this head. hl.get_active_monitor and hl.get_workspace now return nil, which keeps qconsole's fit() out of the comparison, but it changes what a user's own config sees. I ran small configs through the scan, each one followed by a bind, and checked whether that bind came out:

config line quattro this PR #7564
ipairs(hl.get_monitors()) hangs ok ok
ipairs(hl) / ipairs(hl.dsp) hangs ok hangs
hl.get_active_monitor().name ok aborts ok
hl.get_active_monitor().scale > 1 aborts aborts ok
hl.get_monitors()[1].name ok aborts aborts
while hl.some_api() do end hangs hangs hangs, but killed after 10s

"Aborts" means the pcall swallows the error and every bind declared after that line goes missing from the menu. hl.get_active_monitor().name works on quattro today and stops working here.

The competing fix is #7564. It makes the same numeric-nil change on noop. It also gives the sentinel comparison, arithmetic, length and concatenation metamethods, so a load-time read degrades instead of raising, and it wraps the scan in timeout 10 in the command itself, so a shape nothing handles can't leave a lua spinning after the menu closes. On these probes it covers more of the realistic configs. What only this PR has is the numeric guard on dsp_proxy and the hl fallback, which closes ipairs(hl) and ipairs(hl.dsp). Those loops are far less likely in a real config, but the guard is a small, safe addition to #7564 if the maintainer wants both. Because of that prepended loop, this PR's test also times out against #7564's scanner, so these two branches conflict in both files and can't simply both land.

My recommendation is #7564, with this PR's dsp_proxy/hl guards carried over if wanted. Which one to take is the maintainer's call, so I've pushed nothing here and left #7564 untouched.

No second opinion ran on this pass. The configured reviewer, Codex Medium, refused because today's review budget was already spent. Everything above is one model's review (Claude Opus 5.5 in Claude Code), checked against the worker runs described here. Waiting on the maintainer; nothing is needed from you.

@yashranaway

Copy link
Copy Markdown
Contributor Author

Confirmed the nil monitor stub can abort user configurations before later binds are recorded. I'm closing this in favor of #7564; the small numeric guards for hl and hl.dsp can be carried over if needed.

@yashranaway yashranaway closed this Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants