Skip to content

Fix crash when searching in the Python Packages dialog - #1795

Open
deepratna-awale wants to merge 6 commits into
LykosAI:mainfrom
deepratna-awale:fix-python-packages-search-crash
Open

deepratna-awale wants to merge 6 commits into
LykosAI:mainfrom
deepratna-awale:fix-python-packages-search-crash

Conversation

@deepratna-awale

Copy link
Copy Markdown
Contributor

Fixes #693

Problem

Searching in Packages → ⋯ → Python Packages can crash Stability Matrix (e.g. typing "ja" to find jax). Pasting the full name works around it.

Selecting a package loads its details (pip show / pip index versions) via LoadExtraInfo(...).SafeFireAndForget() with no exception handler. With no handler, AsyncAwaitBestPractices lets the exception escape, so anything other than a ProcessException (e.g. FileNotFoundException from the uv/pip/pyvenv.cfg checks in the venv runner, InvalidOperationException from PipIndexResult.Parse, ArgumentOutOfRangeException from PipShowResult.Parse) is rethrown on the UI thread and ends the app.

The list box uses SelectionMode="AlwaysSelected", so as the search filter narrows the list the selection jumps to a new package on almost every keystroke. Each jump started another details load, and those loads overlapped against the same venv, each one rewriting pyvenv.cfg and spawning its own pip processes. Pasting the name changes the selection once, which is why the workaround helps.

Changes

  • PythonPackagesViewModel
    • Failures while loading package details are logged as warnings instead of being rethrown on the UI thread.
    • Only one package's details load at a time (SemaphoreSlim). A load still waiting for its turn is dropped when the selection moves on, and a load whose package is no longer selected is skipped.
    • No load is started for an item that is already loading, or before the venv path is known.
    • Venv runner creation moved into one helper (it was copied in three places), with an internal factory hook for tests.
  • PythonPackagesItemViewModel.LoadExtraInfo takes a venv runner factory instead of a path plus PyBaseInstall, and skips if it is already loading.
  • PipIndexResult.Parse returns no versions instead of throwing when the output has no Available versions: line.
  • PipShowResult.Parse no longer throws when a License: line has no Location: line after it.

Testing

  • New headless UI tests (PythonPackagesDialogTests) open the real dialog over a substituted venv runner and type into the search box:
    • TypingInSearch_WhenPackageDetailsFailToLoad_DoesNotCrash: pip show throws FileNotFoundException. On the old code the exception is rethrown on the dispatcher and fails the test. It passes with the fix.
    • TypingInSearch_LoadsOnePackagesDetailsAtATime: on the old code more than one detail load ran at once. With the fix, at most one runs at a time.
  • New unit tests: PipIndexResultTests and two extra PipShowResultsTests cases (license without location, location before license).
  • StabilityMatrix.UITests: 21/21 passed. StabilityMatrix.Tests: all passed except the TestLicenseUrl / TestPreviewImageUri checks, which need outbound network access that my test environment blocks (unrelated to this change).

Selecting a package loads its details (pip show / pip index) in a
fire-and-forget task with no exception handler, so any failure other than
a ProcessException was rethrown on the UI thread and took down the app.
Typing in the search box moves the selection on almost every keystroke,
starting one of these loads per keystroke, overlapping against the same
venv.

- Log failures of the details load instead of rethrowing them
- Load one package's details at a time and skip loads whose package is no
  longer selected
- Don't start a second load for an item that is already loading
- PipIndexResult.Parse returns no versions instead of throwing when the
  output has no "Available versions:" line
- PipShowResult.Parse no longer throws when "License:" has no following
  "Location:" line

Fixes LykosAI#693
@mohnjiles

Copy link
Copy Markdown
Member

Nice digging on this one. The diagnosis checks out: the Avalonia app never sets a default SafeFireAndForget handler, so with none passed the exception lands on the UI thread. The handler, the finally reset of IsLoading, and the semaphore plus per-selection CTS all look right to me. Both parser fixes are real bugs too (the missing Location: range and First → FirstOrDefault).

A few non-blocking notes:

  • The "already loaded / already loading" guard now lives in three places (PythonPackagesViewModel ~158 and ~189, and PythonPackagesItemViewModel:84). It would be cleaner with one owner.
  • A failed load is only logged, so the details pane stays empty with no hint. That's much better than a crash, but a short "couldn't load details" state could be a follow-up.
  • extraInfoCts and extraInfoLock aren't disposed when the dialog closes. That's minor since the VM is transient.

Looks good to merge.

@deepratna-awale

deepratna-awale commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

@mohnjiles I would like to do this before you Merge.

PythonPackagesItemViewModel now owns the already loaded / loading check,
so the dialog view model only checks that the selection hasn't moved on.
A failed load shows a message in the details pane and is retried when
the package is selected again. Pending loads are cancelled when the
dialog closes, and the cancellation source and lock are disposed with
the view model.
@deepratna-awale

Copy link
Copy Markdown
Contributor Author
  • The "already loaded / loading" check now lives only in PythonPackagesItemViewModel.LoadExtraInfo. The dialog view model only checks that the selection hasn't moved on while waiting for the lock.

  • A failed load shows "Could not load details for this package. Select it again to retry." in the details pane, with the error in a tooltip. Selecting the package again retries it.

  • Closing the dialog cancels pending loads. The view model disposes the CTS and semaphore, and PackageCardViewModel disposes the view model after the dialog closes. A load still running when the dialog closes finishes without throwing.

New UI tests cover the error and retry path and closing the dialog mid-load.

@ionite34 ionite34 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the follow-ups! One thing left: if pip index fails, selecting the package again doesn't retry. PipShowResult is set (line 112) before PipIndex runs (line 117), so after an index failure the guard at line 94 returns early and the error stays until the dialog closes.

Assigning PipShowResult only after both calls succeed should fix it.

PipShowResult was set before pip index ran, so an index failure left the
item looking loaded and selecting it again never retried. It is now
assigned only once both calls succeed.
@deepratna-awale

Copy link
Copy Markdown
Contributor Author

Noice catch, aarigato. PipShowResult is now assigned only after both pip show and pip index succeed, so a failed index lookup leaves the item unloaded and selecting it again retries. Added FailedIndexLookup_RetriesWhenSelectedAgain to cover it.

@deepratna-awale

Copy link
Copy Markdown
Contributor Author

The last commit is just for peas in a pod achievement on github lol

Co-authored-by: Ionite34 <ionite34@users.noreply.github.com>
@deepratna-awale
deepratna-awale force-pushed the fix-python-packages-search-crash branch from 6cb63fa to 7d1b666 Compare November 17, 2026 10:37

This branch has not been deployed

No deployments
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.

[Bug] Python Packages /CRASH/

3 participants