Repository: spyder-ide/spyder

Issue:
OSError when trying to start files server
## Description

### What steps will reproduce the problem?

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

？？？???????????????????????????????????????????????

### Traceback
```python-traceback
  File "D:\Anaconda3\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "D:\Anaconda3\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "D:\Anaconda3\lib\site-packages\spyder\app\mainwindow.py", line 3168, in start_open_files_server
    self.open_files_server.bind(('127.0.0.1', port))
OSError: [WinError 10013] 以一种访问权限不允许的方式做了一个访问套接字的尝试。
WARNING:traitlets:kernel restarted
WARNING:traitlets:kernel restarted
WARNING:traitlets:kernel restarted
WARNING:traitlets:kernel restarted
WARNING:traitlets:KernelRestarter: restart failed
WARNING:traitlets:kernel died: 25.0005099773407
WARNING:traitlets:kernel died: 25.00770354270935
```

## Versions

* Spyder version: 4.2.5 
* Python version: 3.8.8
* Qt version: 5.9.7
* PyQt5 version: 5.9.2
* 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             :  1.6.0 (OK)
diff_match_patch >=20181111     :  20200713 (OK)
intervaltree >=3.0.2            :  3.1.0 (OK)
IPython >=7.6.0                 :  7.22.0 (OK)
jedi =0.17.2                    :  0.17.2 (OK)
jsonschema >=3.2.0              :  3.2.0 (OK)
keyring >=17.0.0                :  22.3.0 (OK)
nbconvert >=4.0                 :  6.0.7 (OK)
numpydoc >=0.6.0                :  1.1.0 (OK)
paramiko >=2.4.0                :  2.7.2 (OK)
parso =0.7.0                    :  0.7.0 (OK)
pexpect >=4.4.0                 :  4.8.0 (OK)
pickleshare >=0.4               :  0.7.5 (OK)
psutil >=5.3                    :  5.8.0 (OK)
pygments >=2.0                  :  2.8.1 (OK)
pylint >=1.0                    :  2.7.4 (OK)
pyls >=0.36.2;<1.0.0            :  0.36.2 (OK)
pyls_black >=0.4.6              :  0.4.6 (OK)
pyls_spyder >=0.3.2             :  0.3.2 (OK)
qdarkstyle >=2.8;<3.0           :  2.8.1 (OK)
qtawesome >=0.5.7               :  1.0.2 (OK)
qtconsole >=5.0.3               :  5.0.3 (OK)
qtpy >=1.5.0                    :  1.9.0 (OK)
rtree >=0.8.3                   :  0.9.7 (OK)
setuptools >=39.0.0             :  52.0.0.post20210125 (OK)
sphinx >=0.6.6                  :  4.0.1 (OK)
spyder_kernels >=1.10.2;<1.11.0 :  1.10.2 (OK)
textdistance >=4.2.0            :  4.2.1 (OK)
three_merge >=0.1.1             :  0.1.1 (OK)
watchdog >=0.10.3;<2.0.0        :  1.0.2 (OK)
zmq >=17                        :  20.0.0 (OK)

# Optional:
cython >=0.21                   :  0.29.23 (OK)
matplotlib >=2.0.0              :  3.3.4 (OK)
numpy >=1.7                     :  1.22.2 (OK)
pandas >=1.1.1                  :  1.1.3 (OK)
scipy >=0.17.0                  :  1.7.1 (OK)
sympy >=0.7.3                   :  1.8 (OK)
```



Discussion:
you are evidently running quite an old version of spyder itself, as well as a number of libraries. Please try updating to a newer version. With the number of things out-of-date, I would personally recommend you install a totally new version of python from python.org and install spyder from scratch via `pip`. This way everything will be the most recent version available from pypi. If you have a strong reason to keep using the old installation of python, you _can_ run spyder on the new python, while executing code with the old version, but it's never a bad idea to try and make your code run on the newest version.
这是来自QQ邮箱的自动回复邮件。您好，我已收到您的邮件。
Hey @Deng-Yao, thanks for reporting. It seems your antivirus or firewall doesn't allow to open ports and Spyder crashes due to that.

We'll fix that in our next version (5.3.2), to be released at the beginning of July. For now, please open the Anaconda Prompt and run there

    spyder --new-instance

Afterwards, please go to the menu

    Tools > Preferences > Application > Advanced settings

and disable the option called

    Use a single instance

After that you can close Spyder and start it again as you normally do and shouldn't see any issues.

Resolving patch:
diff --git a/spyder/app/mainwindow.py b/spyder/app/mainwindow.py
--- a/spyder/app/mainwindow.py
+++ b/spyder/app/mainwindow.py
@@ -1167,7 +1167,6 @@ def post_visible_setup(self):
             # when it gets a client connected to it
             self.sig_open_external_file.connect(self.open_external_file)
 
-
         # Update plugins toggle actions to show the "Switch to" plugin shortcut
         self._update_shortcuts_in_panes_menu()
 
@@ -1817,14 +1816,25 @@ def set_prefs_size(self, size):
         """Save preferences dialog size."""
         self.prefs_dialog_size = size
 
-    # -- Open files server
+    # ---- Open files server
     def start_open_files_server(self):
         self.open_files_server.setsockopt(socket.SOL_SOCKET,
                                           socket.SO_REUSEADDR, 1)
         port = select_port(default_port=OPEN_FILES_PORT)
         CONF.set('main', 'open_files_port', port)
-        self.open_files_server.bind(('127.0.0.1', port))
+
+        # This is necessary in case it's not possible to bind a port for the
+        # server in the system.
+        # Fixes spyder-ide/spyder#18262
+        try:
+            self.open_files_server.bind(('127.0.0.1', port))
+        except OSError:
+            self.open_files_server = None
+            return
+
+        # Number of petitions the server can queue
         self.open_files_server.listen(20)
+
         while 1:  # 1 is faster than True
             try:
                 req, dummy = self.open_files_server.accept()
diff --git a/spyder/app/start.py b/spyder/app/start.py
--- a/spyder/app/start.py
+++ b/spyder/app/start.py
@@ -81,10 +81,11 @@ def send_args_to_spyder(args):
     """
     from spyder.config.manager import CONF
     port = CONF.get('main', 'open_files_port')
+    print_warning = True
 
     # Wait ~50 secs for the server to be up
     # Taken from https://stackoverflow.com/a/4766598/438386
-    for _x in range(200):
+    for __ in range(200):
         try:
             for arg in args:
                 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
@@ -95,6 +96,13 @@ def send_args_to_spyder(args):
                 client.send(osp.abspath(arg))
                 client.close()
         except socket.error:
+            # Print informative warning to let users know what Spyder is doing
+            if print_warning:
+                print("Waiting for the server to open files and directories "
+                      "to be up (perhaps it failed to be started).")
+                print_warning = False
+
+            # Wait 250 ms before trying again
             time.sleep(0.25)
             continue
         break
@@ -249,7 +257,7 @@ def main():
                 send_args_to_spyder(args)
             else:
                 print("Spyder is already running. If you want to open a new \n"
-                      "instance, please pass to it the --new-instance option")
+                      "instance, please use the --new-instance option")
     else:
         from spyder.app import mainwindow
         if running_under_pytest():


Test patch: