python-language-server icon indicating copy to clipboard operation
python-language-server copied to clipboard

'Too many open files' error coming from the server

Open dpkatz opened this issue 8 years ago • 18 comments

Hi -

when I use pyls and pyls-mypy on a python file in Emacs everything works fine for a while. Documentation pops up, function signatures are shown inline, etc. Then, after perhaps a few dozen interactions I get the following (repeatable) error:

Error from the Language Server: jedi.api.environment.InvalidPythonEnvironment: Could not get version information for ’/Users/dkatz/.local/share/virtualenvs/lsp_example_project-cOuqdw3J/bin/python3.7’: OSError(24, ’Too many open files’) (Invalid Parameters) [16 times]

Once that error is raised, I see it every time the cursor hits any interesting piece of code. Note that I see this with two different LSP clients for Emacs (lsp-mode and eglot), so I suspect the problem is at the server.

I can trigger this behavior even with a completely trivial file like this:

import sys

by just moving the cursor on and off of sys several times (which I assume triggers communication with the server each time as it pops up the module information).

If I watch the number of python processes as a function of how I interact with the server I see something like this (taken from moving the cursor on and off of sys in the trivial file mentioned above):

event effect python processes
Open python file in Emacs 5
Put cursor on sys pop up module info 13
Move cursor off sys module info popup goes away 13
Put cursor back on sys pop up module info 18
Move cursor off sys module info goes away 19
Put cursor back on sys pop up module info 23
Move cursor off sys module info goes away 24
Put cursor back on sys pop up module info 28
Move cursor off sys module info goes away 29
Put cursor back on sys pop up module info 34
Move cursor off sys module info goes away 35
Put cursor back on sys pop up module info 39
Move cursor off sys module info goes away 40
Put cursor back on sys pop up module info 44
Move cursor off sys module info goes away 45
Put cursor back on sys pop up module info 49
Move cursor off sys module info goes away 50

and so on until I got the error with 81 python processes running. The number 81 seems to be pretty consistent for me - I get that number of processes with different files and different interactions (e.g. module info, function info, displaying function signatures, etc.)

Any ideas?

Thanks.

Dan



\

Setup:

OS: macOS 10.13.6

Python: 3.7.0 Python package installer: pipenv Python packages:

  • python-language-server 0.21.2 [all]
  • pyls-mypy 0.1.3

dpkatz avatar Oct 11 '18 13:10 dpkatz

I removed pyls-mypy from my virtualenv and this behavior went away. Apparently it's an issue on that plugin. Sorry for the confusion - I'll move the issue over there.

dpkatz avatar Oct 11 '18 14:10 dpkatz

Actually, I started looking at pyls-mypy and I realized that I'm not competent to know if this is a problem in pyls-mypy or in the plugin framework in pyls. I'm re-opening this issue so that someone more knowledgeable than me can close it again if that's appropriate.

Sorry for the confusion.

dpkatz avatar Oct 11 '18 14:10 dpkatz

I'm seeing similar behavior with emacs-lsp, and without mypy/pyls-mypy installed.

andrew-christianson avatar Oct 12 '18 23:10 andrew-christianson

seeing same error, after reading python file for some time..

Error from the Language Server: jedi.api.environment.InvalidPythonEnvironment: Could not get version information for ’/Users/showgood/.pyenv/versions/3.4.0/bin/python3.4’: OSError(24, ’Too many open files’) (Invalid Parameters) [22 times]

showgood avatar Oct 13 '18 01:10 showgood

And I can confirm that as well now. Using pyls-mypy makes the issue worse, but it happens even without.

dpkatz avatar Oct 13 '18 01:10 dpkatz

I'm having the same issue, but only when using Python 2. When installed under Python 3, everything works fine.

JeremyWeed avatar Oct 16 '18 15:10 JeremyWeed

Thanks all for the thorough debugging. Could you please reproduce the issue and then upload the output of lsof -p <pid> where <pid> is the process ID of pyls.

Note there may be sensitive information in the output, please check before uploading.

gatesn avatar Oct 16 '18 21:10 gatesn

lsof.txt This is what I get. I removed my project directory and virtualenv name from the output, but everything else should be the same.

JeremyWeed avatar Oct 16 '18 21:10 JeremyWeed

I've looked into this a bit. The issue seems to be jedi@862f6118, which modifies get_cached_default_environment to check the value of the VIRTUAL_ENV env var, and spawn a new Jedi Environment if it differs from the cached environment environment.

Something like:

@@ -31,6 +31,9 @@ class Workspace(object):
         self._root_path = uris.to_fs_path(self._root_uri)
         self._docs = {}
 
+        # jedi
+        self._jedi_environment = None
+
         # Whilst incubating, keep rope private
         self.__rope = None
         self.__rope_config = None
@@ -242,7 +245,8 @@ class Document(object):
         kwargs = {
             'source': self.source,
             'path': self.path,
-            'sys_path': self.sys_path()
+            'sys_path': self.sys_path(),
+            'environment': self.jedi_environment()
         }
         if position:
             kwargs['line'] = position['line'] + 1
@@ -254,7 +258,12 @@ class Document(object):
         path = list(self._extra_sys_path)
 
         # TODO(gatesn): #339 - make better use of jedi environments, they seem pretty powerful
-        environment = jedi.api.environment.get_cached_default_environment()
+        environment = self.jedi_environment()
         path.extend(environment.get_sys_path())
 
         return path
+
+    def jedi_environment(self):
+        if self._jedi_environment is None:
+            self._jedi_environment = jedi.api.environment.get_default_environment()
+        return self._jedi_environment

(basically just storing the Jedi Environment in the language server Workspace, and passing it to the Jedi.Script workhorse) seemed to resolve the process spawning issue.

I ran into some other issues though - the single Jedi process hit 100% CPU frequently, and blocked editing in emacs.

andrew-christianson avatar Oct 16 '18 22:10 andrew-christianson

I have the same issue but it only happens with pyenv, with virtualenvwrapper works well

arkhan avatar Nov 07 '18 16:11 arkhan

Linking probably relevant PR on Jedi: https://github.com/davidhalter/jedi/pull/1238

dsedivec avatar Nov 08 '18 15:11 dsedivec

I tried this but I got the same problem

Linking probably relevant PR on Jedi: davidhalter/jedi#1238

arkhan avatar Nov 08 '18 18:11 arkhan

Okay, I've been able to narrow this down and hack together a solution for this on my end. I'm using Spacemacs with Pyenv-mode and pyenv + pyenv-virtualenv to automatically switch my Python version when I open files. I determined:

  1. Pyenv-mode doesn't interface with pyenv-virtualenv to set VIRTUAL_ENV when you open a file in a virtualenv, but it will change the python version/location so everything else will look the same.
  2. Jedi spawns a bunch of processes when VIRTUAL_ENV isn't set (davidhalter/jedi#1238)
  3. lsp-python will spawn a new language server/jedi process without VIRTUAL_ENV set, since it inherits emacs' environment, not your shell environment

You can force pyenv-mode to set VIRTUAL_ENV with:

  (defun pyenv-venv-wrapper-act (&optional ARG PRED)
    (setenv "VIRTUAL_ENV" (shell-command-to-string "_pyenv_virtualenv_hook; echo -n $VIRTUAL_ENV")))
  (advice-add 'pyenv-mode-set :after 'pyenv-venv-wrapper-act)
  (defun pyenv-venv-wrapper-deact (&optional ARG PRED)
    (setenv "VIRTUAL_ENV"))
  (advice-add 'pyenv-mode-unset :after 'pyenv-venv-wrapper-deact)

Hopefully this helps anyone with the same problem.

JeremyWeed avatar Nov 29 '18 17:11 JeremyWeed

Looks like this got fixed in the latest release of jedi: davidhalter/jedi#1238. I've been using that all morning and it seems to do the trick.

dpkatz avatar Dec 17 '18 17:12 dpkatz

This should be resolved in Jedi now

gatesn avatar Feb 15 '19 11:02 gatesn

I'm still seeing this behavior using Python 2.7, pyls 0.23.2 and jedi 0.13.2. Is it fixed for Python 2, or just Python 3?

jdormit avatar Feb 22 '19 15:02 jdormit

I'm seeing the same error for Py2 too. Just editing simple single .py file. No virtual environments involved.

danilaml avatar Feb 23 '19 01:02 danilaml

There seems to be a bug that still exists on the master branch that leads to an explosion in python subprocesses. This seems to happen when using pipenv to manage virtualenvs.

There is a MR for that here: https://github.com/davidhalter/jedi/pull/1308

Hopefully it will be fixed soon.

nikhilmitrax avatar Apr 05 '19 07:04 nikhilmitrax