Repository: spyder-ide/spyder

Issue:
Some errors that occur when using the Find plugin
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

When searching for a string occurrence usinf the Find-tab, the results are displayed correctly, but after finalizing this 'internal problem' error occurs. This happens all the time for a long time already (several past Spyder versions).

### Traceback
```python-traceback
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/findinfiles/widgets/search_thread.py", line 101, in run
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/findinfiles/widgets/search_thread.py", line 155, in find_files_in_path
OSError: [Errno 62] Too many levels of symbolic links: '/Users/thomas/Documents/swDev/pyUtils/log/xxx/fashion-mnist-86.log'
```

## Versions

* Spyder version: 5.4.3 b6abc3d30 (standalone)
* Python version: 3.9.14 64-bit
* Qt version: 5.15.2
* PyQt5 version: 5.15.9
* Operating System: Darwin 21.6.0

### Dependencies

```
# Mandatory:
atomicwrites >=1.2.0                             :  1.4.1 (OK)
chardet >=2.0.0                                  :  5.1.0 (OK)
cloudpickle >=0.5.0                              :  2.2.1 (OK)
cookiecutter >=1.6.0                             :  2.1.1 (OK)
diff_match_patch >=20181111                      :  20200713 (OK)
intervaltree >=3.0.2                             :  3.1.0 (OK)
IPython >=7.31.1,<9.0.0,!=8.8.0,!=8.9.0,!=8.10.0 :  8.12.0 (OK)
jedi >=0.17.2,<0.19.0                            :  0.18.2 (OK)
jellyfish >=0.7                                  :  0.11.2 (OK)
jsonschema >=3.2.0                               :  4.17.3 (OK)
keyring >=17.0.0                                 :  23.13.1 (OK)
nbconvert >=4.0                                  :  7.3.0 (OK)
numpydoc >=0.6.0                                 :  1.5.0 (OK)
parso >=0.7.0,<0.9.0                             :  0.8.3 (OK)
pexpect >=4.4.0                                  :  4.8.0 (OK)
pickleshare >=0.4                                :  0.7.5 (OK)
psutil >=5.3                                     :  5.9.4 (OK)
pygments >=2.0                                   :  2.14.0 (OK)
pylint >=2.5.0,<3.0                              :  2.17.2 (OK)
pylint_venv >=2.1.1                              :  None (OK)
pyls_spyder >=0.4.0                              :  0.4.0 (OK)
pylsp >=1.7.2,<1.8.0                             :  1.7.2 (OK)
pylsp_black >=1.2.0                              :  1.2.1 (OK)
qdarkstyle >=3.0.2,<3.2.0                        :  3.1 (OK)
qstylizer >=0.2.2                                :  0.2.2 (OK)
qtawesome >=1.2.1                                :  1.2.3 (OK)
qtconsole >=5.4.2,<5.5.0                         :  5.4.2 (OK)
qtpy >=2.1.0                                     :  2.3.1 (OK)
rtree >=0.9.7                                    :  1.0.1 (OK)
setuptools >=49.6.0                              :  67.6.1 (OK)
sphinx >=0.6.6                                   :  5.1.1 (OK)
spyder_kernels >=2.4.3,<2.5.0                    :  2.4.3 (OK)
textdistance >=4.2.0                             :  4.5.0 (OK)
three_merge >=0.1.1                              :  0.1.1 (OK)
watchdog >=0.10.3                                :  3.0.0 (OK)
zmq >=22.1.0                                     :  25.0.2 (OK)

# Optional:
cython >=0.21                                    :  0.29.34 (OK)
matplotlib >=3.0.0                               :  3.7.1 (OK)
numpy >=1.7                                      :  1.24.2 (OK)
pandas >=1.1.1                                   :  2.0.0 (OK)
scipy >=0.17.0                                   :  1.10.1 (OK)
sympy >=0.7.3                                    :  1.11.1 (OK)

# Spyder plugins:
spyder_terminal.terminalplugin 1.2.2             :  1.2.2 (OK)
```

### Environment

<details><summary>Environment</summary>

```

```
</details>



Discussion:
Hi @tplocher thank you for the feedback! Checking seems like it could be that you have some broken symbolic links but probably we should handle this more gracefully like showing a message of some sort or directly ignoring such paths :+1:

Maybe this could help checking if there are broken symlinks: https://unix.stackexchange.com/a/49470

What do you think @ccordoba12 ? Maybe this will not happen if using the external binary to do the search proposed at https://github.com/spyder-ide/spyder/issues/20595 ?


> What do you think @ccordoba12 ? Maybe this will not happen if using the external binary to do the search proposed at https://github.com/spyder-ide/spyder/issues/20595 ?

Yep, this shouldn't be a problem if we use `ripgrep` for searches. For now, the best we can do is to catch and pass on this error, which we'll do in our next version (5.4.4), to be released at the beginning of July.
Thank you!
I deleted the broken links and the search with find performs normally.
However, Spyder crashes on exit (only after performing a search with find). The issue report window just pops up for 0.1 seconds, so I only was able to catch it with a video; see picture. Afterwards I get a problem report 'Spyder quit unexpectedly.'; see attached.
![crash](https://user-images.githubusercontent.com/67992848/231330660-9fc2244c-30d1-4105-9d7b-2be25bdf13e5.jpeg)
[spyderCrash.txt](https://github.com/spyder-ide/spyder/files/11206433/spyderCrash.txt)

Thanks for the additional info @tplocher.

@mrclary, could you explain @tplocher how to start our Mac app in debug mode in a terminal, so we can check what's error that happens during shutdown? Thanks!
@tplocher @ccordoba12, to launch the standalone Spyder.app from the terminal in debug mode, do the following:
```
$ /Applications/Spyder.app/Contents/MacOS/Spyder --debug-info=verbose
```
or
```
$ /Users/<user>/Applications/Spyder.app/Contents/MacOS/Spyder --debug-info=verbose
```
depending on where Spyder.app is located.
@tplocher, please use that command and post the last 100 lines or so that are printed in the terminal after you close Spyder.
[spyderLog20230415.txt](https://github.com/spyder-ide/spyder/files/11238097/spyderLog20230415.txt)

BTW: I upgraded to version 5.4.3 in the meantime 
------
* Spyder version: 5.4.3 b6abc3d30 (standalone)
* Python version: 3.9.14 64-bit
* Qt version: 5.15.2
* PyQt5 version: 5.15.9
* Operating System: Darwin 21.6.0

Thanks @tplocher for your help! I can see the error at shutdown you're experiencing in the file you uploaded. We'll fix that too in our next version (5.4.4), to be released in June.
Thank you for your quick attention to this matter! Best wishes, Thomas

Resolving patch:
diff --git a/spyder/plugins/findinfiles/widgets/main_widget.py b/spyder/plugins/findinfiles/widgets/main_widget.py
--- a/spyder/plugins/findinfiles/widgets/main_widget.py
+++ b/spyder/plugins/findinfiles/widgets/main_widget.py
@@ -462,7 +462,7 @@ def _stop_and_reset_thread(self, ignore_results=False):
             if self.search_thread.isRunning():
                 if ignore_results:
                     self.search_thread.sig_finished.disconnect(
-                        self.search_complete)
+                        self._handle_search_complete)
                 self.search_thread.stop()
                 self.search_thread.wait()
 
diff --git a/spyder/plugins/findinfiles/widgets/search_thread.py b/spyder/plugins/findinfiles/widgets/search_thread.py
--- a/spyder/plugins/findinfiles/widgets/search_thread.py
+++ b/spyder/plugins/findinfiles/widgets/search_thread.py
@@ -151,9 +151,16 @@ def find_files_in_path(self, path):
                     filename = os.path.join(path, f)
                     ext = osp.splitext(filename)[1]
 
-                    # Only search in regular files (i.e. not pipes)
-                    st_file_mode = os.stat(filename).st_mode
-                    if not stat.S_ISREG(st_file_mode):
+                    # Only search in regular files (i.e. not pipes).
+                    # The try/except is necessary to catch an error when
+                    # Python can't get the file status due to too many levels
+                    # of symbolic links.
+                    # Fixes spyder-ide/spyder#20798
+                    try:
+                        st_file_mode = os.stat(filename).st_mode
+                        if not stat.S_ISREG(st_file_mode):
+                            continue
+                    except OSError:
                         continue
 
                     # Exclude patterns defined by the user


Test patch: