Repository: spyder-ide/spyder

Issue:
Spyder crashes because of socket initialization errors on Windows
I downloaded Anaconda last week and have never been able to load Spyder. The same error message keeps coming up that spyder crashed during last session and to try resetting spyder by "running spyder with the command line option '--reset': python spider --reset." I tried and that doesn't work. Other solutions that I found were to delete the spyder.lock folder and I do but that also doesn't resolve anything and the folder reappears after I try to load Spyder again and nothing happens. I've tried deleting the entire .spyder-py3 folder as that worked for some people but the folder also reappears. 

**What steps will reproduce the problem?**

1. Open up anaconda navigator
2. Doubleclick on spyder 3.1.2 
3. Error message appears that spyder crashed during last session







## Versions and main components

* Spyder Version: 3.1.2
* Python Version: 3.6
* Qt Version: 4.2.1
* PyQt Version:
* Operating system: Windows 7 Home Premium


## Dependencies

Please go to the menu entry `Help > Optional Dependencies` (or
`Help > Dependencies`), press the button `Copy to clipboard`
and paste the contents below:



Discussion:
Please open a terminal (i.e. cmd.exe) and run there

    spyder --show-console

Then post the errors printed there on this issue.
Traceback (most recent call last):
  File "C:\Users\User\Anaconda3\lib\site-packages\spyder\app\mainwindow.py", line 2989, in main
    mainwindow = run_spyder(app, options, args)
  File "C:\Users\User\Anaconda3\lib\site-packages\spyder\app\mainwindow.py", line 2891, in run_spyder
    main = MainWindow(options)
  File "C:\Users\User\Anaconda3\lib\site-packages\spyder\app\mainwindow.py", line 494, in __init__
    socket.IPPROTO_TCP)
  File "C:\Users\User\Anaconda3\lib\socket.py", line 144, in __init__
    _socket.socket.__init__(self, family, type, proto, fileno)
OSError: [WinError 10106] The requested service provider could not be loaded or initialized

It seems your antivirus/firewall is blocking Spyder's attempt to open a socket in your system.

What antivirus/firewall are you using?
I'm using Norton 360 Premier. 
I disabled Norton and tried loading Spyder again and it still doesn't work. I will try uninstalling and reinstalling with Norton disabled and see if it will work then. 
I have the same problem.I have solved it.
open a terminal (i.e. cmd.exe) and run there
`netsh winsock reset`
Then restart computer.
Hope to help you.
Thank you so much!! That solved the problem! 
@rickywz, what does

    netsh winsock reset

do?
@ccordoba12 Reset the Winsock directory to restore the network

Resolving patch:
diff --git a/spyder/app/mainwindow.py b/spyder/app/mainwindow.py
--- a/spyder/app/mainwindow.py
+++ b/spyder/app/mainwindow.py
@@ -490,10 +490,26 @@ def signal_handler(signum, frame=None):
         self.previous_focused_widget = None
 
         # Server to open external files on a single instance
-        self.open_files_server = socket.socket(socket.AF_INET,
-                                               socket.SOCK_STREAM,
-                                               socket.IPPROTO_TCP)
-
+        # This is needed in order to handle socket creation problems.
+        # See issue 4132
+        if os.name == 'nt':
+            try:
+                self.open_files_server = socket.socket(socket.AF_INET,
+                                                       socket.SOCK_STREAM,
+                                                       socket.IPPROTO_TCP)
+            except OSError as e:
+                self.open_files_server = None
+                QMessageBox.warning(None, "Spyder",
+                         _("An error occurred while creating a socket needed "
+                           "by Spyder. Please, try to run as an Administrator "
+                           "from cmd.exe the following command and then "
+                           "restart your computer: <br><br><span "
+                           "style=\'color: #555555\'><b>netsh winsock reset"
+                           "</b></span><br>"))
+        else:
+            self.open_files_server = socket.socket(socket.AF_INET,
+                                                   socket.SOCK_STREAM,
+                                                   socket.IPPROTO_TCP)
         self.apply_settings()
         self.debug_print("End of MainWindow constructor")
 
@@ -1173,7 +1189,8 @@ def post_visible_setup(self):
         # Server to maintain just one Spyder instance and open files in it if
         # the user tries to start other instances with
         # $ spyder foo.py
-        if CONF.get('main', 'single_instance') and not self.new_instance:
+        if (CONF.get('main', 'single_instance') and not self.new_instance
+                and self.open_files_server):
             t = threading.Thread(target=self.start_open_files_server)
             t.setDaemon(True)
             t.start()
@@ -2146,7 +2163,7 @@ def closing(self, cancelable=False):
                 return False
         prefix = 'window' + '/'
         self.save_current_window_settings(prefix)
-        if CONF.get('main', 'single_instance'):
+        if CONF.get('main', 'single_instance') and self.open_files_server:
             self.open_files_server.close()
         for plugin in self.thirdparty_plugins:
             if not plugin.closing_plugin(cancelable):


Test patch: