textual icon indicating copy to clipboard operation
textual copied to clipboard

[Bug] SelectionList raises OptionDoesNotExist when highlighted index is stale after filtering

Open hrshtmk opened this issue 2 months ago • 5 comments

Description

SelectionList._on_option_list_option_highlighted can raise an unhandled OptionDoesNotExist when the option_index on the event no longer corresponds to an existing option, e.g. a stale index left over after the option list was filtered or changed.

Real-world impact

This is crashing archinstall's "Additional packages" menu, which uses a filterable SelectionList. If keystrokes arrive while the list is being filtered or the screen is transitioning, the highlight handler gets called with an index that no longer exists. OptionDoesNotExist is raised, and the fallout surfaces further up as a NoMatches exception (No nodes match 'SelectionList' on SelectListScreen()), which takes down the whole installer.

Downstream PR with the workaround: https://github.com/archlinux/archinstall/pull/4780

Right now archinstall has to override the Textual handler in its own _SelectionList subclass to stay safe. A fix in Textual would let every app using SelectionList avoid this.

Steps to reproduce

  1. Create a SelectionList with a few options.
  2. Cause a highlight event with an index that is no longer valid (e.g. filter the options while a highlight is pending, or set highlighted to an out-of-range index).
  3. get_option_at_index raises OptionDoesNotExist inside the handler.

Minimal reproduction (included as a regression test on the branch below):

selection_list.highlighted = 999
event = OptionList.OptionHighlighted(selection_list, valid_option, 999)
selection_list._on_option_list_option_highlighted(event)  # raises

Expected behavior

A stale highlight event is ignored and no exception is raised.

Proposed fix

Bounds-check event.option_index in _on_option_list_option_highlighted and return early if the option doesn't exist. I have this ready with a regression test and a CHANGELOG entry:

Branch: https://github.com/hrshtmk/textual/tree/fix/selection-list-bounds Commit: 939d4d6

I know PRs are limited to collaborators, so I'm filing this with the fix on my branch. Happy to open a PR if a maintainer can enable that.

hrshtmk avatar Oct 02 '26 18:10 hrshtmk

We found the following entry in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This project is developed and maintained by Will McGugan. Consider sponsoring Will's work on this project (and others).

This is an automated reply, generated by FAQtory

github-actions[bot] avatar Oct 02 '26 18:10 github-actions[bot]

I'd like to work on this.

The bug is in SelectionList._on_option_list_option_highlighted — it can raise an unhandled OptionDoesNotExist when the option_index on the event no longer corresponds to an existing option.

I'll add a regression test and fix the root cause.

DawnofGenX avatar Oct 04 '26 21:10 DawnofGenX

I'd like to work on this.

The bug is in SelectionList._on_option_list_option_highlighted — it can raise an unhandled OptionDoesNotExist when the option_index on the event no longer corresponds to an existing option.

I'll add a regression test and fix the root cause.

Write a better LLM script, my guy. The fix, test and changelog are already attached in my description if you can't see.

hrshtmk avatar Oct 05 '26 03:10 hrshtmk

Hi! I've put together the fix along with a unit regression test and CHANGELOG entry on my branch: https://github.com/Guurdeep/textual/tree/fix/selection-list-bounds

Summary of Changes:

  • src/textual/widgets/_selection_list.py: Added try...except OptionDoesNotExist: handling in _on_option_list_option_highlighted so stale highlight events (e.g. after filtering options or screen transitions) are ignored gracefully.
  • tests/selection_list/test_selection_messages.py: Added test_stale_highlight_event_ignored() regression test.
  • CHANGELOG.md: Added entry under [Unreleased]. Since PR creation is restricted to repository collaborators, could a maintainer please enable PR access for this branch or pull the fix directly? Thanks

Guurdeep avatar Oct 08 '26 23:10 Guurdeep

Hi @willmcgugan,

This bug is currently blocking a fix downstream in archinstall, an opensource project. I already have the fix, regression test, and CHANGELOG entry ready on my branch hrshtmk/textual as mentioned in the original issue comment above. Please ignore the bot comments as they are copying my fix and descriptions word-for-word.

Since PR creation is restricted, could you please enable PR access for my branch or let me know how you'd like to pull it in?

Thanks for your time.

hrshtmk avatar Oct 21 '26 05:10 hrshtmk