Repository: spyder-ide/spyder

Issue:
Getting "Spyder already running" warning when it's not
## Description of your problem

On windows 7 I get a 

**What steps will reproduce the problem?**
1. Open sypder from cmd (spyder.bat)
2. Close (pressing X in rop right)
3. Attempt to open spyder again (cmd spyder.exe or .bat)

**What is the expected output? What do you see instead?**

I expect spyder to open I get this (which does work if I open a new instance)

```
Spyder is already running. If you want to open a new
instance, please pass to it the --new-instance option
```

**Please provide any additional information below**

This is not a show-stopping issue. It seems as though the lockfile mechanism isn't being properly released. I'm not sure how to "wipe the slate".
## Versions and main components
- Spyder Version: 2.5.3.2
- Python Version: 2.7.10
- Operating system: Windows 7
## Dependencies

Please go to the menu entry `Help > Optional Dependencies` (or
`Help > Dependencies`), press the button `Copy to clipboard`
and paste the contents below:
IPython >=1.0   :  2.4.1 (OK)
jedi >=0.8.1    :  0.9.0 (OK)
matplotlib >=1.0:  1.4.3 (OK)
pandas >=0.13.1 :  0.16.2 (OK)
pep8 >=0.6      :  1.6.2 (OK)
pyflakes >=0.5.0:  0.9.2 (OK)
pygments >=1.6  :  2.0.2 (OK)
pylint >=0.25   :  1.4.3 (OK)
rope >=0.9.2    :  0.10.2 (OK)
sphinx >=0.6.6  :  1.3.2 (OK)
sympy >=0.7.3   :  0.7.6.1 (OK)
zmq >=2.1.11    :  14.7.0 (OK)

Many thanks for a great application.



Discussion:
Note. This persists even after a reboot.

Please update Spyder to 2.3.8 and try again.

Thanks. The issue persists though (also the spyder.exe was removed - I launched using spdyer.bat).

I upgraded using `pip install spyder --upgrade`

I am encountering this issue as well. It persists through reboot. I have upgraded spyder and it continues.

I believe it started because I attempted to launch spyder through the dashboard, and it did not respond. I then launched it from console and encountered the issue.

Any suggestions on how to resolve this?

You need to look for a directory called `.spyder2` in your `Users\<your account>` directory, the find a file called `spyder.lock` and remove it.

Just ran into this issue with Spyder 3. The directory with the `spyder.lock` file now seems to be `<home dir>/.config/spyder[-py3]`.


Resolving patch:
diff --git a/spyder/utils/external/__init__.py b/spyder/utils/external/__init__.py
--- a/spyder/utils/external/__init__.py
+++ b/spyder/utils/external/__init__.py
@@ -8,6 +8,9 @@
 spyder.utils.external
 =====================
 
-External libraries needed for Spyder to work.
-Put here only untouched libraries, else put them in utils.
+* Libraries or modules taken from external projects verbatim.
+* Spyder developers can modify these modules to make them work
+  better for the project. So there's no guarantee that we will
+  maintain compatibility with the upstream project the code
+  was taken from.
 """
diff --git a/spyder/utils/external/lockfile.py b/spyder/utils/external/lockfile.py
--- a/spyder/utils/external/lockfile.py
+++ b/spyder/utils/external/lockfile.py
@@ -6,16 +6,20 @@
 """
 Filesystem-based interprocess mutex.
 
-Changes by the Spyder Team to the original Twisted file:
--. Rewrite kill Windows function to make it more reliable
+Taken from the Twisted project.
+Distributed under the MIT license.
+
+Changes by the Spyder Team to the original module:
+  * Rewrite kill Windows function to make it more reliable.
+  * Detect if the process that owns the lock is an Spyder one.
 """
 
 __metaclass__ = type
 
 import errno, os
-
 from time import time as _uniquefloat
 
+import psutil
 from spyder.py3compat import PY2, to_binary_string
 
 def unique():
@@ -171,10 +175,23 @@ def lock(self):
                     try:
                         if kill is not None:
                             kill(int(pid), 0)
+                        # Verify that the running process corresponds to
+                        # a Spyder one
+                        p = psutil.Process(int(pid))
+                        if os.name == 'nt':
+                            conditions = ['spyder' in c.lower()
+                                          for c in p.cmdline()]
+                        else:
+                            conditions = [p.name() == 'spyder',
+                                          p.name() == 'spyder3']
+                        # For DEV
+                        conditions += ['bootstrap.py' in p.cmdline()]
+                        if not any(conditions):
+                            raise(OSError(errno.ESRCH, 'No such process'))
                     except OSError as e:
                         if e.errno == errno.ESRCH:
-                            # The owner has vanished, try to claim it in the next
-                            # iteration through the loop.
+                            # The owner has vanished, try to claim it in the
+                            # next iteration through the loop.
                             try:
                                 rmlink(self.name)
                             except OSError as e:


Test patch: