Repository: spyder-ide/spyder

Issue:
Error when undocking code window, after opening then closing a new window
## Description

### What steps will reproduce the problem?

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

open new window
close said window
undock code window

### Traceback
```python-traceback
Traceback (most recent call last):
  File "C:\Users\andrew.terentyev\AppData\Local\Programs\Spyder\pkgs\spyder\plugins\base.py", line 382, in _create_window
    self.set_ancestor(window)
  File "C:\Users\andrew.terentyev\AppData\Local\Programs\Spyder\pkgs\spyder\plugins\editor\plugin.py", line 1326, in set_ancestor
    comp_widget.setParent(ancestor)
RuntimeError: wrapped C/C++ object of type CompletionWidget has been deleted
```

## Versions

* Spyder version: 5.2.2 
* Python version: 3.7.9
* Qt version: 5.12.10
* PyQt5 version: 5.12.3
* Operating System: Windows 10

### Dependencies

```
# 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                  :  None (OK)
IPython >=7.6.0;<8.0.0        :  7.31.1 (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.5.0 (OK)
nbconvert >=4.0               :  6.4.0 (OK)
numpydoc >=0.6.0              :  1.1.0 (OK)
paramiko >=2.4.0              :  2.9.2 (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.12.2 (OK)
pyls_spyder >=0.4.0           :  0.4.0 (OK)
pylsp >=1.3.2;<1.4.0          :  1.3.3 (OK)
pylsp_black >=1.0.0           :  1.0.1 (OK)
qdarkstyle =3.0.2             :  3.0.2 (OK)
qstylizer >=0.1.10            :  0.2.1 (OK)
qtawesome >=1.0.2             :  1.1.1 (OK)
qtconsole >=5.2.1;<5.3.0      :  5.2.2 (OK)
qtpy >=1.5.0                  :  2.0.0 (OK)
rtree >=0.9.7                 :  0.9.7 (OK)
setuptools >=49.6.0           :  60.5.0 (OK)
sphinx >=0.6.6                :  4.3.2 (OK)
spyder_kernels >=2.2.1;<2.3.0 :  2.2.1 (OK)
textdistance >=4.2.0          :  4.2.2 (OK)
three_merge >=0.1.1           :  0.1.1 (OK)
watchdog                      :  2.1.6 (OK)
zmq >=17                      :  22.3.0 (OK)

# Optional:
cython >=0.21                 :  0.29.26 (OK)
matplotlib >=2.0.0            :  3.5.1 (OK)
numpy >=1.7                   :  1.19.3 (OK)
pandas >=1.1.1                :  1.3.5 (OK)
scipy >=0.17.0                :  1.7.3 (OK)
sympy >=0.7.3                 :  1.9 (OK)
```



Discussion:
Hey @Andreuha, thanks for reporting. We'll solve this error in our next version (5.3.0), to be released later this 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
@@ -1323,8 +1323,16 @@ def set_ancestor(self, ancestor):
             for finfo in editorstack.data:
                 comp_widget = finfo.editor.completion_widget
                 kite_call_to_action = finfo.editor.kite_call_to_action
-                comp_widget.setParent(ancestor)
-                kite_call_to_action.setParent(ancestor)
+
+                # This is necessary to catch an error when the plugin is
+                # undocked and docked back, and (probably) a completion is
+                # in progress.
+                # Fixes spyder-ide/spyder#17486
+                try:
+                    comp_widget.setParent(ancestor)
+                    kite_call_to_action.setParent(ancestor)
+                except RuntimeError:
+                    pass
 
     def _create_checkable_action(self, text, conf_name, method=''):
         """Helper function to create a checkable action.


Test patch: