Repository: spyder-ide/spyder

Issue:
RuntimeError when opening a file
## Description

### What steps will reproduce the problem?

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

I don't know what's going on with Spyder but it's not letting me open a folder.

### Traceback
```python-traceback
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/layout/plugin.py", line 838, in switch_to_plugin
    plugin.change_visibility(True, force_focus=force_focus)
AttributeError: 'Editor' object has no attribute 'change_visibility'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/editor/plugin.py", line 2145, in load
    self.switch_to_plugin()
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/editor/plugin.py", line 1712, in switch_to_plugin
    super(Editor, self).switch_to_plugin()
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/api/plugins/old_api.py", line 426, in switch_to_plugin
    super(BasePluginWidget, self)._switch_to_plugin()
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/base.py", line 298, in _switch_to_plugin
    self.main.switch_to_plugin(self)
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/app/mainwindow.py", line 558, in switch_to_plugin
    self.layouts.switch_to_plugin(plugin, force_focus=force_focus)
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/layout/plugin.py", line 841, in switch_to_plugin
    plugin._visibility_changed(True)
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/editor/plugin.py", line 453, in _visibility_changed
    SpyderPluginWidget._visibility_changed(self, enable)
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/base.py", line 405, in _visibility_changed
    widget = self.get_focus_widget()
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/editor/plugin.py", line 449, in get_focus_widget
    return self.get_current_editor()
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/editor/plugin.py", line 1754, in get_current_editor
    editorstack = self.get_current_editorstack()
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/editor/plugin.py", line 1745, in get_current_editorstack
    editorstack = self.__get_focused_editorstack()
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/editor/plugin.py", line 1431, in __get_focused_editorstack
    if editorstack.isAncestorOf(fwidget):
RuntimeError: wrapped C/C++ object of type EditorStack has been deleted
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/editor/utils/autosave.py", line 134, in do_autosave
    stack = self.editor.get_current_editorstack()
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/editor/plugin.py", line 1745, in get_current_editorstack
    editorstack = self.__get_focused_editorstack()
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/editor/plugin.py", line 1431, in __get_focused_editorstack
    if editorstack.isAncestorOf(fwidget):
RuntimeError: wrapped C/C++ object of type EditorStack has been deleted
```

## Versions

* Spyder version: 5.4.0 510c6249c (standalone)
* Python version: 3.9.14 64-bit
* Qt version: 5.15.2
* PyQt5 version: 5.15.7
* Operating System: Darwin 21.6.0

### Dependencies

```
# Mandatory:
atomicwrites >=1.2.0                 :  1.4.1 (OK)
chardet >=2.0.0                      :  5.0.0 (OK)
cloudpickle >=0.5.0                  :  2.2.0 (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;<8.0.0              :  7.34.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.17.0 (OK)
keyring >=17.0.0                     :  23.9.3 (OK)
nbconvert >=4.0                      :  7.2.3 (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.3 (OK)
pygments >=2.0                       :  2.13.0 (OK)
pylint >=2.5.0;<3.0                  :  2.15.5 (OK)
pylint_venv >=2.1.1                  :  None (OK)
pyls_spyder >=0.4.0                  :  0.4.0 (OK)
pylsp >=1.6.0;<1.7.0                 :  1.6.0 (OK)
pylsp_black >=1.2.0                  :  1.2.1 (OK)
qdarkstyle >=3.0.2;<3.1.0            :  3.0.3 (OK)
qstylizer >=0.2.2                    :  0.2.2 (OK)
qtawesome >=1.2.1                    :  1.2.1 (OK)
qtconsole >=5.4.0;<5.5.0             :  5.4.0 (OK)
qtpy >=2.1.0                         :  2.2.1 (OK)
rtree >=0.9.7                        :  1.0.1 (OK)
setuptools >=49.6.0                  :  65.5.1 (OK)
sphinx >=0.6.6                       :  5.1.1 (OK)
spyder_kernels >=2.4.0;<2.5.0        :  2.4.0 (OK)
textdistance >=4.2.0                 :  4.5.0 (OK)
three_merge >=0.1.1                  :  0.1.1 (OK)
watchdog >=0.10.3                    :  2.1.9 (OK)
zmq >=22.1.0                         :  24.0.1 (OK)

# Optional:
cython >=0.21                        :  0.29.32 (OK)
matplotlib >=3.0.0                   :  3.6.2 (OK)
numpy >=1.7                          :  1.22.4 (OK)
pandas >=1.1.1                       :  1.5.1 (OK)
scipy >=0.17.0                       :  1.9.3 (OK)
sympy >=0.7.3                        :  1.11.1 (OK)

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



Discussion:
Hey @elemusma, thanks for reporting. It seems you have a spit editor, like this

![imagen](https://user-images.githubusercontent.com/365293/201944275-bb633c9d-c215-44d2-b4fb-2a2cbcee8b39.png)

or an editor window, like this

![imagen](https://user-images.githubusercontent.com/365293/201944617-7e0f8e4f-3f1c-43d5-849e-62849b8e333f.png)

and then you close any of them before the autosave mechanism finished to do its job.

Could you confirm if you had any of those configurations?
@elemusma, don't worry if you don't remember the circumstances that triggered this bug. I think I know how to fix it.

The fix will be available in our next version (5.4.1), to be released before the end of the year. In the meantime, you can safely ignore it because it doesn't cause a major loss of functionality.

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
@@ -1984,7 +1984,6 @@ def new(self, fname=None, editorstack=None, text=None):
         fname=<basestring> --> create file
         """
         # If no text is provided, create default content
-        empty = False
         try:
             if text is None:
                 default_content = True
@@ -2140,10 +2139,13 @@ def load(self, filenames=None, goto=None, word='',
         """
         cursor_history_state = self.__ignore_cursor_history
         self.__ignore_cursor_history = True
-        # Switch to editor before trying to load a file
+
+        # Switch to editor before trying to load a file.
+        # Here we catch RuntimeError to avoid an issue when loading files.
+        # Fixes spyder-ide/spyder#20055
         try:
             self.switch_to_plugin()
-        except AttributeError:
+        except (AttributeError, RuntimeError):
             pass
 
         editor0 = self.get_current_editor()
diff --git a/spyder/plugins/editor/widgets/codeeditor.py b/spyder/plugins/editor/widgets/codeeditor.py
--- a/spyder/plugins/editor/widgets/codeeditor.py
+++ b/spyder/plugins/editor/widgets/codeeditor.py
@@ -2028,7 +2028,14 @@ def notify_save(self):
     def notify_close(self):
         """Send close request."""
         self._pending_server_requests = []
-        self._server_requests_timer.stop()
+
+        # This is necessary to prevent an error when closing the file.
+        # Fixes spyder-ide/spyder#20071
+        try:
+            self._server_requests_timer.stop()
+        except RuntimeError:
+            pass
+
         if self.completions_available:
             # This is necessary to prevent an error in our tests.
             try:


Test patch: