Repository: spyder-ide/spyder

Issue:
PyLS freezes spyder
## Description
Spyder froze completely while I was typing code. apparently it was caused by a blocking request to send something to the languageserver:
### What steps will reproduce the problem?

<!--- You can use Markdown here --->



### Traceback
```python-traceback
  File "/usr/local/lib/python3.7/site-packages/zmq/sugar/socket.py", line 400 in send
  File "/usr/local/lib/python3.7/site-packages/zmq/sugar/socket.py", line 611 in send_pyobj
  File "/Users/quentinpeter/pyscripts/spyder/spyder/plugins/completion/languageserver/client.py", line 325 in send
  File "/Users/quentinpeter/pyscripts/spyder/spyder/plugins/completion/languageserver/decorators.py", line 42 in wrapper
  File "/Users/quentinpeter/pyscripts/spyder/spyder/plugins/completion/languageserver/client.py", line 394 in perform_request
  File "/Users/quentinpeter/pyscripts/spyder/spyder/plugins/completion/languageserver/plugin.py", line 312 in send_request
  File "/Users/quentinpeter/pyscripts/spyder/spyder/plugins/completion/plugin.py", line 248 in send_request
  File "/Users/quentinpeter/pyscripts/spyder/spyder/plugins/editor/plugin.py", line 319 in send_completion_request
  File "/Users/quentinpeter/pyscripts/spyder/spyder/plugins/editor/widgets/editor.py", line 2571 in perform_completion_request
  File "/Users/quentinpeter/pyscripts/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 983 in emit_request
  File "/Users/quentinpeter/pyscripts/spyder/spyder/plugins/completion/decorators.py", line 22 in wrapper
  File "/Users/quentinpeter/pyscripts/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 3708 in insert_text
  File "/Users/quentinpeter/pyscripts/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 3885 in keyPressEvent
  File "/Users/quentinpeter/pyscripts/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 3686 in event
  File "/Users/quentinpeter/pyscripts/spyder/spyder/app/mainwindow.py", line 3634 in run_spyder
  File "/Users/quentinpeter/pyscripts/spyder/spyder/app/mainwindow.py", line 3735 in main
  File "/Users/quentinpeter/pyscripts/spyder/spyder/app/start.py", line 204 in main
  File "/Applications/Spyder.app/Contents/MacOS/Spyder", line 4 in <module>
```

## Versions

* Spyder version: 4.1.0.dev0 d01f324e0
* Python version: 3.7.5
* Qt version: 5.12.6
* PyQt5 version: 5.12.3
* Operating System: Darwin 19.2.0

### Dependencies

```
applaunchservices >=0.1.7    :  0.2.1 (OK)
atomicwrites >=1.2.0         :  1.3.0 (OK)
chardet >=2.0.0              :  3.0.4 (OK)
cloudpickle >=0.5.0          :  1.2.2 (OK)
diff_match_patch >=20181111  :  20181111 (OK)
intervaltree                 :  None (OK)
IPython >=4.0                :  7.10.2 (OK)
jedi =0.14.1                 :  0.14.1 (OK)
nbconvert >=4.0              :  5.6.1 (OK)
numpydoc >=0.6.0             :  0.9.1 (OK)
pexpect >=4.4.0              :  4.7.0 (OK)
pickleshare >=0.4            :  0.7.5 (OK)
psutil >=0.3                 :  5.6.5 (OK)
pygments >=2.0               :  2.4.2 (OK)
pylint >=0.25                :  2.4.4 (OK)
pyls >=0.31.2;<0.32.0        :  0.31.2 (OK)
zmq >=17                     :  18.1.1 (OK)
qdarkstyle >=2.7             :  2.7 (OK)
qtawesome >=0.5.7            :  0.6.0 (OK)
qtconsole >=4.6.0            :  4.7.0.dev0 (OK)
qtpy >=1.5.0                 :  1.9.0 (OK)
sphinx >=0.6.6               :  2.2.1 (OK)
spyder_kernels >=1.8.1;<2.0.0:  1.9.0.dev0 (OK)
watchdog                     :  None (OK)
cython >=0.21                :  0.29.14 (OK)
matplotlib >=2.0.0           :  3.1.2 (OK)
numpy >=1.7                  :  1.17.4 (OK)
pandas >=0.13.1              :  0.25.3 (OK)
scipy >=0.17.0               :  1.3.3 (OK)
sympy >=0.7.3                :  1.4 (OK)
```



Discussion:

Resolving patch:
diff --git a/spyder/plugins/completion/languageserver/client.py b/spyder/plugins/completion/languageserver/client.py
--- a/spyder/plugins/completion/languageserver/client.py
+++ b/spyder/plugins/completion/languageserver/client.py
@@ -18,6 +18,7 @@
 import signal
 import subprocess
 import sys
+import time
 
 # Third-party imports
 from qtpy.QtCore import QObject, Signal, QSocketNotifier, Slot
@@ -322,9 +323,24 @@ def send(self, method, params, kind):
             }
 
         logger.debug('{} request: {}'.format(self.language, method))
-        self.zmq_out_socket.send_pyobj(msg)
-        self.request_seq += 1
-        return int(_id)
+
+        # Try sending a message. If the send queue is full, keep trying for a
+        # a second before giving up.
+        timeout = 1
+        start_time = time.time()
+        timeout_time = start_time + timeout
+        while True:
+            try:
+                self.zmq_out_socket.send_pyobj(msg, flags=zmq.NOBLOCK)
+                self.request_seq += 1
+                return int(_id)
+            except zmq.error.Again:
+                if time.time() > timeout_time:
+                    self.sig_lsp_down.emit(self.language)
+                    return
+                # The send queue is full! wait 0.1 seconds before retrying.
+                logger.warning("The send queue is full! Retrying...")
+                time.sleep(.1)
 
     @Slot()
     def on_msg_received(self):


Test patch: