Repository: spyder-ide/spyder

Issue:
TimeoutError and crash at startup when restoring files in the editor if OneDrive is not started
<!--- **PLEASE READ:** When submitting here, please ensure you've completed the following checklist and checked the boxes to confirm. Issue reports without it may be closed. Thanks! --->

### 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``
    * [ ] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [ ] Tried the other applicable steps from the Troubleshooting Guide
* [x] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description
Random problem when launching spyder, I just confirmed it, I was using it normally, then I closed it and when trying to reopen it problem aroused. No modifications to libraries nor anything was done between this two events. 

I've searched extensively here and everywhere and found nothing applicable to my situation, I am using a MacBook Pro M1 Max, I'm not using anaconda, since I'm using tensorflow GPU acceleration with metal through miniconda. I'm just using spyder standalone app, and as I said, sometimes it works flawlessly for days, but other times just crashes upon launch


### What steps reproduce the problem?

1. No steps, since problem since to be random
2. 
3. 

### What is the expected output? What do you see instead?

Expected output is spyder window. Instead I get the window but with a popup message saying "Launch Error See the py2app website for debugging launch issues" And gives me only two options, "Visit Website" and "Terminate" both close spyder. I can't use the program at all while this popup is open.


### Paste Traceback/Error Below (if applicable)
<!--- Copy from error dialog or View > Panes > Internal Console --->

```python-traceback

PASTE TRACEBACK HERE

I've run the app from the terminal and get this output.

The available OpenGL surface format was either not version 3.2 or higher or not a Core Profile.
Chromium on macOS will fall back to software rendering in this case.
Hardware acceleration and features such as WebGL will not be available.
fromIccProfile: failed minimal tag size sanity
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/__boot__.py", line 251, in <module>
    _run()
  File "/Applications/Spyder.app/Contents/Resources/__boot__.py", line 174, in _run
    exec(compile(source, path, "exec"), globals(), globals())
  File "/Applications/Spyder.app/Contents/Resources/spyder", line 3, in <module>
    start.main()
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/app/start.py", line 266, in main
    mainwindow.main(options, args)
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/app/mainwindow.py", line 1822, in main
    mainwindow = create_window(MainWindow, app, splash, options, args)
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/app/utils.py", line 300, in create_window
    main.post_visible_setup()
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/app/mainwindow.py", line 1053, in post_visible_setup
    self.reopen_last_session()
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/app/mainwindow.py", line 1091, in reopen_last_session
    editor.setup_open_files(close_previous_files=False)
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/editor/plugin.py", line 3474, in setup_open_files
    self.load(filenames[index::-1], goto=clines[index::-1],
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/editor/plugin.py", line 2273, in load
    finfo = self.editorstacks[0].load(
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/editor/widgets/editor.py", line 2736, in load
    text, enc = encoding.read(filename)
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/utils/encoding.py", line 308, in read
    text, encoding = decode( open(filename, 'rb').read() )
TimeoutError: [Errno 60] Operation timed out
2023-03-10 16:31:50.137 Spyder[1535:14979] Launch error
2023-03-10 16:31:50.137 Spyder[1535:14979] Launch error
See the py2app website for debugging launch issues
Uncaught TypeError: Cannot read property 'fit' of undefined
QProcess: Destroyed while process ("/Applications/Spyder.app/Contents/MacOS/python") is still running.
QProcess: Destroyed while process ("/Applications/Spyder.app/Contents/MacOS/python") is still running.
QProcess: Destroyed while process ("/Applications/Spyder.app/Contents/MacOS/python") is still running.


```

## 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.2 Latest Mac standalone
* Python version: My Mac has 3.10.9 but I think spyder is using 3.9.14 cause that is shown in the spyder console before it freezes.
* Qt version: 5.15.9
* PyQt version: 5.15.17
* Operating System name/version: macOS Ventura 13.2.1

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```
PASTE DEPENDENCIES HERE

I can't access to this menu
```

I am sorry if some info is lacking, and will be happy to add anything requested. This is the first time I'm posting here and I'm not a computer guru.


Discussion:
Looks like having some open scripts (open in the spyder IDE) stored in OneDrive and not having OneDrive open at the moment causes the issue. I opened OneDrive and everything worked as expected.

Admin please delete this issue. Sorry for the mistake. 

Thank you for your work!
Hey @Gerard568, thanks for reporting. Actually, this error is something we can fix on our side to prevent it from showing up to users. We'll do that in our next version (5.4.3), to be released at the end of the month.

Resolving patch:
diff --git a/spyder/plugins/editor/plugin.py b/spyder/plugins/editor/plugin.py
--- a/spyder/plugins/editor/plugin.py
+++ b/spyder/plugins/editor/plugin.py
@@ -2262,17 +2262,25 @@ def _convert(fname):
                                                        editorwindow,
                                                        focus=focus)
             if current_editor is None:
-                # -- Not a valid filename:
+                # Not a valid filename, we need to continue.
                 if not osp.isfile(filename):
                     continue
-                # --
+
                 current_es = self.get_current_editorstack(editorwindow)
+
                 # Creating the editor widget in the first editorstack
                 # (the one that can't be destroyed), then cloning this
                 # editor widget in all other editorstacks:
                 finfo = self.editorstacks[0].load(
                     filename, set_current=False, add_where=add_where,
                     processevents=processevents)
+
+                # This can happen when it was not possible to load filename
+                # from disk.
+                # Fixes spyder-ide/spyder#20670
+                if finfo is None:
+                    continue
+
                 self._clone_file_everywhere(finfo)
                 current_editor = current_es.set_current_filename(filename,
                                                                  focus=focus)
@@ -2281,6 +2289,7 @@ def _convert(fname):
                 self.register_widget_shortcuts(current_editor)
                 current_es.analyze_script()
                 self.__add_recent_file(filename)
+
             if goto is not None:  # 'word' is assumed to be None as well
                 current_editor.go_to_line(goto[index], word=word,
                                           start_column=start_column,
@@ -2288,6 +2297,7 @@ def _convert(fname):
             current_editor.clearFocus()
             current_editor.setFocus()
             current_editor.window().raise_()
+
             if processevents:
                 QApplication.processEvents()
             else:
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
@@ -2756,8 +2756,14 @@ def load(self, filename, set_current=True, add_where='end',
         if processevents:
             self.starting_long_process.emit(_("Loading %s...") % filename)
 
-        # Read file contents
-        text, enc = encoding.read(filename)
+        # This is necessary to avoid a crash at startup when trying to restore
+        # files from the previous session.
+        # Fixes spyder-ide/spyder#20670
+        try:
+            # Read file contents
+            text, enc = encoding.read(filename)
+        except Exception:
+            return
 
         # Associate hash of file's text with its name for autosave
         self.autosave.file_hashes[filename] = hash(text)


Test patch: