Repository: spyder-ide/spyder

Issue:
Spyder Profiler Hangs/Freezes
## Description
I am trying to profile a script in spyder on Windows 10 but it freezes

**What steps will reproduce the problem?**
I am on Windows 10
1. I created and activated a virtual environment with `>conda create --name deeplearningtest3 python` and `activate deeplearningtest3`
2. installed ipython, seaborn, spyder, gym, and tensorflow-gpu using conda
3. opened spyder and began profiling my script (main5.py)

[main5.txt](https://github.com/spyder-ide/spyder/files/1635874/main5.txt)



**What is the expected output? What do you see instead?**
My script runs fine without profiling but freezes after one loop iteration when run in profile mode. I am given the attached error when I stop profiling

**Please provide any additional information below**

  File "C:\Users\User\Anaconda3\envs\deeplearningtest3\lib\site-packages\spyder_profiler\widgets\profilergui.py", line 264, in <lambda>
    self.finished(ec, es))
  File "C:\Users\User\Anaconda3\envs\deeplearningtest3\lib\site-packages\spyder_profiler\widgets\profilergui.py", line 329, in finished
    self.show_data(justanalyzed=True)
  File "C:\Users\User\Anaconda3\envs\deeplearningtest3\lib\site-packages\spyder_profiler\widgets\profilergui.py", line 350, in show_data
    self.datatree.load_data(self.DATAPATH)
  File "C:\Users\User\Anaconda3\envs\deeplearningtest3\lib\site-packages\spyder_profiler\widgets\profilergui.py", line 462, in load_data
    stats_indi = [pstats.Stats(profdatafile),]
  File "C:\Users\User\Anaconda3\envs\deeplearningtest3\lib\pstats.py", line 72, in __init__
    self.init(arg)
  File "C:\Users\User\Anaconda3\envs\deeplearningtest3\lib\pstats.py", line 86, in init
    self.load_stats(arg)
  File "C:\Users\User\Anaconda3\envs\deeplearningtest3\lib\pstats.py", line 99, in load_stats
    with open(arg, 'rb') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\User\\.spyder-py3\\profiler.results'


## Version and main components

* Spyder Version: 3.2.6 
* Python Version: 3.6.4
* Qt Versions: 5.6.2, PyQt5 5.6 on Windows

## Dependencies
```
pyflakes >=0.6.0 :  1.6.0 (OK)
pycodestyle >=2.3:  2.3.1 (OK)
pygments >=2.0   :  2.2.0 (OK)
pandas >=0.13.1  :  None (OK)
numpy >=1.7      :  1.12.1 (OK)
sphinx >=0.6.6   :  1.6.6 (OK)
rope >=0.9.4     :  0.10.7 (OK)
jedi >=0.9.0     :  0.11.1 (OK)
nbconvert >=4.0  :  5.3.1 (OK)
sympy >=0.7.3    :  None (NOK)
cython >=0.21    :  None (NOK)
qtconsole >=4.2.0:  4.3.1 (OK)
IPython >=4.0    :  6.2.1 (OK)
pylint >=0.25    :  1.8.1 (OK)

```



Discussion:
I thought we fixed the proximate error, i.e. #5587 , with #5634 in 3.2.5?
@CAM-Gerlach Thank you for the response. I followed your link to issue #5587 and applied the fix suggested by Lemming0815 (calling reset_config_files()). But I got the same error as before. 
@CAM-Gerlach, we need to address this problem in 3.2.7 because it seems it was not properly solved in previous versions.
@ccordoba12 Yes, I believe I've seen this ~three times now on >=3.2.5 versions though the first ~two didn't follow up.

Resolving patch:
diff --git a/spyder_profiler/widgets/profilergui.py b/spyder_profiler/widgets/profilergui.py
--- a/spyder_profiler/widgets/profilergui.py
+++ b/spyder_profiler/widgets/profilergui.py
@@ -262,7 +262,7 @@ def start(self, wdir=None, args=None, pythonpath=None):
                                           lambda: self.read_output(error=True))
         self.process.finished.connect(lambda ec, es=QProcess.ExitStatus:
                                       self.finished(ec, es))
-        self.stop_button.clicked.connect(self.process.kill)
+        self.stop_button.clicked.connect(self.kill)
 
         if pythonpath is not None:
             env = [to_text_string(_pth)
@@ -276,6 +276,7 @@ def start(self, wdir=None, args=None, pythonpath=None):
         
         self.output = ''
         self.error_output = ''
+        self.stopped = False
         
         p_args = ['-m', 'cProfile', '-o', self.DATAPATH]
         if os.name == 'nt':
@@ -298,7 +299,12 @@ def start(self, wdir=None, args=None, pythonpath=None):
         if not running:
             QMessageBox.critical(self, _("Error"),
                                  _("Process failed to start"))
-    
+
+    def kill(self):
+        """Stop button pressed."""
+        self.process.kill()
+        self.stopped = True
+
     def set_running_state(self, state=True):
         self.start_button.setEnabled(not state)
         self.stop_button.setEnabled(state)
@@ -344,6 +350,11 @@ def show_data(self, justanalyzed=False):
         if not filename:
             return
 
+        if self.stopped:
+            self.datelabel.setText(_('Run stopped by user.'))
+            self.datatree.initialize_view()
+            return
+
         self.datelabel.setText(_('Sorting data, please wait...'))
         QApplication.processEvents()
         
@@ -459,13 +470,16 @@ def initialize_view(self):
     def load_data(self, profdatafile):
         """Load profiler data saved by profile/cProfile module"""
         import pstats
-        stats_indi = [pstats.Stats(profdatafile),]
+        try:
+            stats_indi = [pstats.Stats(profdatafile), ]
+        except (OSError, IOError):
+            return
         self.profdata = stats_indi[0]
         
         if self.compare_file is not None:
             try:
                 stats_indi.append(pstats.Stats(self.compare_file))
-            except IOError as e:
+            except (OSError, IOError) as e:
                 QMessageBox.critical(
                     self, _("Error"),
                     _("Error when trying to load profiler results"))


Test patch: