Repository: spyder-ide/spyder

Issue:
Above 5.4.3, Spyder launch stuck when loading Pythonpath manager
### Issue Report Checklist

* [x] Searched the [issues page](https://github.com/spyder-ide/spyder/issues?q=is%3Aissue) for similar reports
* [x] Read the relevant sections of the [Spyder Troubleshooting Guide](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ) and followed its advice
* [x] Reproduced the issue after updating with ``conda update spyder`` (or ``pip``, if not using Anaconda)
* [ ] Could not reproduce inside ``jupyter qtconsole`` (if console-related)
* [x] Tried basic troubleshooting (if a bug/error)
    * [x] Restarted Spyder
    * [x] Reset preferences with ``spyder --reset``
    * [x] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [x] Tried the other applicable steps from the Troubleshooting Guide
* [x] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description

I have been running Spyder for many years. It works perfectly with 5.4.2, when updating to 5.4.3 or 5.4.4, launch gets stuck on "Launching code analysis"

### What steps reproduce the problem?

1. Launch Spyder >5.4.3
2. It gets stuck 

Personal note: I have been installing the last version of Spyder on Ubuntu 22.04.2 LTS through Anaconda on the laptops of my students, it works.

### What is the expected output? What do you see instead?

I would expected Spyder to launch, it gets stuck.

### Paste Traceback/Error Below (if applicable)

[spyder-debug.log](https://github.com/spyder-ide/spyder/files/12136318/spyder-debug.log)

## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->

* Spyder version: > 5.4.3
* Python version:  3.11.4
* Qt version: 5.15.8
* PyQt version: 5.15.7
* Operating System name/version: Ubuntu 22.04.2 LTS

### Dependencies

I cannot access the Menu since Spyder does not launch. Here is the conda environment I created to perform the final test (mamba create -n spyder-env python=3 spyder)

[mamba_env.txt](https://github.com/spyder-ide/spyder/files/12136317/mamba_env.txt)

For the Devs who are going to check this, thanks a lot for all your work. I really enjoy working with Spyder.



Discussion:
Hey @mmontarges, thanks for reporting. I tried to reproduce your problem in a Python 3.11, conda-forge environment, but I was unable to.

However, reading carefully your debug file, it seems Spyder is not hanging when loading the Code Analysis plugin but the Pythonpath manager one.

Do you have the `PYTHONPATH` environment variable set in your system? You can check that by running the `printenv` command in a terminal and looking for it.
> For the Devs who are going to check this, thanks a lot for all your work. I really enjoy working with Spyder.

And thanks a lot for your kind words! We really appreciate users letting us know that.
Hello,

Indeed, I had a PYTHONPATH set, so I removed it but the problem remains.

[env_variables.txt](https://github.com/spyder-ide/spyder/files/12163771/env_variables.txt)
[spyder-debug.log](https://github.com/spyder-ide/spyder/files/12163774/spyder-debug.log)

@mmontarges, sorry for the delay in answering back. Please open a terminal, start IPython and run there:

```python
In [1]: from spyder.utils.environ import get_user_env
In [2]: env = get_user_env()
In [3]: env
```

After doing that, are you experiencing a hang too?
Hi @ccordoba12 

No problem, before today I would not have had the time. Perfect timing. Yes IPython hangs on line 2.
Ok, that's what I thought. Thanks for the confirmation.

@mrclary, what could we do about this in the `5.x` branch? At least we should prevent the subprocess that tries to get the user env vars to hang.
> @mrclary, what could we do about this in the `5.x` branch? At least we should prevent the subprocess that tries to get the user env vars to hang.

We could update `get_user_env_variables` to provide a timeout with `Popen.communicate`. However, I don't know if this will resolve the issue because I cannot reproduce it on my Ubuntu VM. I'd really like to know what is causing it to hang.
> We could update get_user_env_variables to provide a timeout with Popen.communicate

Ok, that's good ro know.

> I don't know if this will resolve the issue because I cannot reproduce it on my Ubuntu VM.

We can ask @mmontarges to apply the change on his system and check.

> I'd really like to know what is causing it to hang.

Since the implementation changed completely in master, I don't know if it's worth it to put more effort into finding the cause. In addition, it's quite important to provide a fix for 5.5 since this is making Spyder hang at startup.
@mmontarges, could you check if the following fixes the issue for you?

Add a timeout to `proc.communicate` by modifying line 75 of `/home/mmontarges/Softwares/Anaconda/envs/spyder-env/lib/python3.11/site-packages/spyder/utils/environ.py`.
```python
            stdout, stderr = proc.communicate(timeout=2)
```
Hi @mrclary, indeed this fix solves the problem. Thank you !
@mrclary, could we reduce that timeout to 0.5? I guess that getting the user env vars should be really fast, so if it takes more than 0.5 sec, then we can be sure that it hanged. Besides, a 0.5 sec delay at startup is almost unnoticeable, but a 2 sec one surely is.
> @mrclary, could we reduce that timeout to 0.5? I guess that getting the user env vars should be really fast, so if it takes more than 0.5 sec, then we can be sure that it hanged. Besides, a 0.5 sec delay at startup is almost unnoticeable, but a 2 sec one surely is.

I'll submit a PR on the 5.x branch for this. And, yes, we can set the timeout to whatever is appropriate.

Resolving patch:
diff --git a/spyder/utils/environ.py b/spyder/utils/environ.py
--- a/spyder/utils/environ.py
+++ b/spyder/utils/environ.py
@@ -72,7 +72,8 @@ def get_user_environment_variables():
             else:
                 env = {}
             proc = run_shell_command(cmd, env=env, text=True)
-            stdout, stderr = proc.communicate()
+            # Use timeout to fix spyder-ide/spyder#21172
+            stdout, stderr = proc.communicate(timeout=0.5)
             env_var = eval(stdout, None)
     except Exception:
         return {}


Test patch: