Fix "Session is closed" error during package installation #3132
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The "Install All Packages" button would fail on fresh installs (where no
python/venv directory exists yet) with a "Session is closed" error. This error would be repeatedly spammed in the terminal, preventing any packages from being installed. The package download spinners would spin forever with no actual progress.Workaround: Installing a single package first (e.g., "Install PyTorch") would work, and then "Install All Packages" would work on subsequent attempts.
Root Cause
When installing dependencies, the worker process is stopped and restarted (to avoid conflicts during pip installation). The sequence is:
worker.stop()- closes the aiohttp sessionworker.start()- creates a new sessionHowever, the
stop()method was closing the session but not settingself._session = None. This created a race condition:/installed-dependenciesendpoint during installationworker.get_packages()which callswait_for_ready()wait_for_ready()checksif self._session is not None- this returnsTrueeven though the session is closedRuntimeError: Session is closedSolution
Modified
WorkerServer.stop()to properly nullify resources after closing them:Now
wait_for_ready()will properly wait for the worker to be restarted (and a new session created) before allowing any requests to proceed.Testing
Fixes the issue described where "Install All Packages" fails on fresh installs.
Original prompt
This section details on the original issue you should resolve
<issue_title>"Install All Packages" button breaks due to error with http://127.0.0.1:8000/installed-dependencies</issue_title>
<issue_description>Information:
Description
This happens on completely fresh installs (where there's no
python/venv dir yet).The "Install All Packages" button breaks due to attempting to connect to
http://127.0.0.1:8000/installed-dependencies, which errors with "Session is closed". This error is repeatedly spammed forever in the terminal.As a result, nothing is being installed. The package download spinners spin forever, and no download progress is happening in the terminal or GUI.
Workaround
Clicking on a single package and installing just ONE package works properly, such as clicking on "Install PyTorch".
And after successfully installing a single package, THEN the "Install All Packages" button starts working too.
Logs