Repository: spyder-ide/spyder

Issue:
Unavailable shared folder causes Spyder to crash on startup
### 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
    * [ ] Reset preferences with ``spyder --reset`` (not necessary, see below)
    * [x] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [ ] Tried the other applicable steps from the Troubleshooting Guide (not necessary, see below)
* [x] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description
When Spyder has been closed with a file in a shared folder on a different computer on the network (Windows) and is started again when the shared folder is not available (for me: after a Windows restart and before providing the credentials for the shared folder), Spyder will crash during startup, at the moment when it tries to re-open that file.


### What steps reproduce the problem?

1. Have two Windows computers, _A_ and _B_, on a network.
2. On _A_, create a folder and share it, but only with some other user on _A_.
3. When someone on _B_ tries to access the shared folder, they will have to enter the credentials of that user before getting access.
4. Create a Python file in the shared folder.
5. Open the Python file with Spyder on _B_, then close Spyder.
6. Restart _B_ so Windows forgets the credentials for accessing the shared folder.
7. Start Spyder on _B_.

### What is the expected output? What do you see instead?

Spyder should open fine, simply not opening the unavailable file. (Just like when a file got deleted in the meantime.)

Instead, it crashes as soon as it attempts to re-open the now unavailable file.

After re-entering the credentials for the shared folder (by accessing it using e.g. Windows Explorer) Spyder starts fine again.

### Paste Traceback/Error Below (if applicable)
<!--- Copy from error dialog or View > Panes > Internal Console --->

```python-traceback
C:\WINDOWS\system32>spyder
Traceback (most recent call last):
  File "C:\Users\wilde\miniconda3\lib\site-packages\spyder\plugins\editor\widgets\editor.py", line 1518, in has_filename
    return data_filenames.index(filename)
ValueError: '\\\\bruegel\\rk\\BeneIII\\2022-01 - Sandwich-Resonator\\khapaev\\wiggle_inductance\\wiggle_inductance.py' is not in list

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\wilde\miniconda3\lib\ntpath.py", line 647, in realpath
    path = _getfinalpathname(path)
OSError: [WinError 1326] The user name or password is incorrect: '\\\\bruegel\\rk\\BeneIII\\2022-01 - Sandwich-Resonator\\khapaev\\wiggle_inductance\\wiggle_inductance.py'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\wilde\miniconda3\Scripts\spyder-script.py", line 10, in <module>
    sys.exit(main())
  File "C:\Users\wilde\miniconda3\lib\site-packages\spyder\app\start.py", line 237, in main
    mainwindow.main(options, args)
  File "C:\Users\wilde\miniconda3\lib\site-packages\spyder\app\mainwindow.py", line 2081, in main
    mainwindow = create_window(MainWindow, app, splash, options, args)
  File "C:\Users\wilde\miniconda3\lib\site-packages\spyder\app\utils.py", line 300, in create_window
    main.post_visible_setup()
  File "C:\Users\wilde\miniconda3\lib\site-packages\spyder\app\mainwindow.py", line 1222, in post_visible_setup
    editor.setup_open_files(close_previous_files=False)
  File "C:\Users\wilde\miniconda3\lib\site-packages\spyder\plugins\editor\plugin.py", line 3444, in setup_open_files
    self.load(filenames[index+1:], goto=clines[index:],
  File "C:\Users\wilde\miniconda3\lib\site-packages\spyder\plugins\editor\plugin.py", line 2244, in load
    current_editor = self.set_current_filename(filename,
  File "C:\Users\wilde\miniconda3\lib\site-packages\spyder\plugins\editor\plugin.py", line 1775, in set_current_filename
    return editorstack.set_current_filename(filename, focus)
  File "C:\Users\wilde\miniconda3\lib\site-packages\spyder\plugins\editor\widgets\editor.py", line 1533, in set_current_filename
    index = self.has_filename(filename)
  File "C:\Users\wilde\miniconda3\lib\site-packages\spyder\plugins\editor\widgets\editor.py", line 1520, in has_filename
    filename = fixpath(filename)
  File "C:\Users\wilde\miniconda3\lib\site-packages\spyder\plugins\explorer\widgets\utils.py", line 58, in fixpath
    return norm(osp.abspath(osp.realpath(path)))
  File "C:\Users\wilde\miniconda3\lib\ntpath.py", line 651, in realpath
    path = _getfinalpathname_nonstrict(path)
  File "C:\Users\wilde\miniconda3\lib\ntpath.py", line 601, in _getfinalpathname_nonstrict
    path = _getfinalpathname(path)
OSError: [WinError 1326] The user name or password is incorrect: '\\\\bruegel\\rk\\BeneIII\\2022-01 - Sandwich-Resonator\\khapaev\\wiggle_inductance\\wiggle_inductance.py'
```

## 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.3.0
* Python version: 3.9.12 64-bit
* Qt version: 5.12.9
* PyQt version: 5.12.3
* Operating System name/version: Windows 11

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```
# Mandatory:
atomicwrites >=1.2.0          :  1.4.0 (OK)
chardet >=2.0.0               :  4.0.0 (OK)
cloudpickle >=0.5.0           :  2.0.0 (OK)
cookiecutter >=1.6.0          :  1.7.3 (OK)
diff_match_patch >=20181111   :  20200713 (OK)
intervaltree >=3.0.2          :  3.0.2 (OK)
IPython >=7.31.1;<8.0.0       :  7.32.0 (OK)
jedi >=0.17.2;<0.19.0         :  0.18.1 (OK)
jellyfish >=0.7               :  0.9.0 (OK)
jsonschema >=3.2.0            :  4.4.0 (OK)
keyring >=17.0.0              :  23.4.0 (OK)
nbconvert >=4.0               :  6.5.0 (OK)
numpydoc >=0.6.0              :  1.2.1 (OK)
paramiko >=2.4.0              :  2.10.3 (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.0 (OK)
pygments >=2.0                :  2.11.2 (OK)
pylint >=2.5.0                :  2.13.5 (OK)
pyls_spyder >=0.4.0           :  0.4.0 (OK)
pylsp >=1.4.1;<1.5.0          :  1.4.1 (OK)
pylsp_black >=1.2.0           :  1.2.1 (OK)
qdarkstyle >=3.0.2;<3.1.0     :  3.0.3 (OK)
qstylizer >=0.1.10            :  0.2.1 (OK)
qtawesome >=1.0.2             :  1.1.1 (OK)
qtconsole >=5.3.0;<5.4.0      :  5.3.0 (OK)
qtpy >=2.0.1                  :  2.0.1 (OK)
rtree >=0.9.7                 :  1.0.0 (OK)
setuptools >=49.6.0           :  62.1.0 (OK)
sphinx >=0.6.6                :  4.5.0 (OK)
spyder_kernels >=2.3.0;<2.4.0 :  2.3.0 (OK)
textdistance >=4.2.0          :  4.2.2 (OK)
three_merge >=0.1.1           :  0.1.1 (OK)
watchdog >=0.10.3             :  2.1.7 (OK)
zmq >=17                      :  22.3.0 (OK)

# Optional:
cython >=0.21                 :  None (NOK)
matplotlib >=3.0.0            :  3.5.1 (OK)
numpy >=1.7                   :  1.22.3 (OK)
pandas >=1.1.1                :  1.4.2 (OK)
scipy >=0.17.0                :  1.8.0 (OK)
sympy >=0.7.3                 :  None (NOK)
```



Discussion:
Hey @schtandard, thanks for reporting. Although not easy to reproduce, we've seen this kind of errors before, so we'll try to fix it in our next version (5.3.1), to be released in some weeks.

Resolving patch:
diff --git a/spyder/plugins/editor/widgets/editor.py b/spyder/plugins/editor/widgets/editor.py
--- a/spyder/plugins/editor/widgets/editor.py
+++ b/spyder/plugins/editor/widgets/editor.py
@@ -1517,7 +1517,13 @@ def has_filename(self, filename):
             # Try finding without calling the slow realpath
             return data_filenames.index(filename)
         except ValueError:
-            filename = fixpath(filename)
+            # See note about OSError on set_current_filename
+            # Fixes spyder-ide/spyder#17685
+            try:
+                filename = fixpath(filename)
+            except OSError:
+                return None
+
             for index, editor_filename in enumerate(data_filenames):
                 if filename == fixpath(editor_filename):
                     return index
@@ -1525,13 +1531,17 @@ def has_filename(self, filename):
 
     def set_current_filename(self, filename, focus=True):
         """Set current filename and return the associated editor instance."""
-        # This is necessary to catch an error on Windows for files in a
-        # directory junction pointing to a symlink whose target is on a
-        # network drive that is unavailable at startup.
+        # FileNotFoundError: This is necessary to catch an error on Windows
+        # for files in a directory junction pointing to a symlink whose target
+        # is on a network drive that is unavailable at startup.
         # Fixes spyder-ide/spyder#15714
+        # OSError: This is necessary to catch an error on Windows when Spyder
+        # was closed with a file in a shared folder on a different computer on
+        # the network, and is started again when that folder is not available.
+        # Fixes spyder-ide/spyder#17685
         try:
             index = self.has_filename(filename)
-        except FileNotFoundError:
+        except (FileNotFoundError, OSError):
             index = None
 
         if index is not None:


Test patch: