Commit 0b5ff71a authored by Jérome Perrin's avatar Jérome Perrin

openoffice: fix psutil usage in _releaseOpenOfficePort

This uses psutil.process_iter to iterate on all processes, but in the meantime
some process might terminate.

As we could observe in the logs:

    2021-04-26 12:53:02 - Cloudooo - DEBUG - Stop Pid - 15816
    Exception in thread Thread-1:
    Traceback (most recent call last):
      File "python2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
        self.run()
      File "cloudooo-repository/cloudooo/handler/ooo/monitor/request.py", line 60, in run
        self.openoffice.restart()
      File "cloudooo-repository/cloudooo/handler/ooo/application/application.py", line 79, in restart
        self.stop()
      File "cloudooo-repository/cloudooo/handler/ooo/application/openoffice.py", line 169, in stop
        self._releaseOpenOfficePort()
      File "cloudooo-repository/cloudooo/handler/ooo/application/openoffice.py", line 122, in _releaseOpenOfficePort
        if process.exe() == join(self.office_binary_path, self._bin_soffice):
      File "psutil-5.8.0-py2.7-linux-x86_64.egg/psutil/__init__.py", line 660, in exe
        exe = self._proc.exe()
      File "psutil-5.8.0-py2.7-linux-x86_64.egg/psutil/_pslinux.py", line 1688, in exe
        raise NoSuchProcess(self.pid, self._name)
    NoSuchProcess: psutil.NoSuchProcess process no longer exists (pid=19382)

Update to catch and ignore such exceptions, and minor refactorings such as:
 - Not calculate the path of openoffice binary in the loop, this is not
   supposed to change.
 - Remove reference to lsof in logged message, this does not use lsof.
 - Catch all exceptions.
parent be6c35c0
Pipeline #15345 failed with stage
in 0 seconds
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
import pkg_resources import pkg_resources
import psutil import psutil
from psutil import AccessDenied from psutil import AccessDenied, NoSuchProcess
from os.path import exists, join from os.path import exists, join
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from threading import Lock from threading import Lock
...@@ -117,20 +117,18 @@ class OpenOffice(Application): ...@@ -117,20 +117,18 @@ class OpenOffice(Application):
return return
def _releaseOpenOfficePort(self): def _releaseOpenOfficePort(self):
openoffice_exe = join(self.office_binary_path, self._bin_soffice)
for process in psutil.process_iter(): for process in psutil.process_iter():
try: try:
if process.exe() == join(self.office_binary_path, self._bin_soffice): if process.exe() == openoffice_exe:
for connection in process.connections(): for connection in process.connections():
if connection.status == "LISTEN" and \ if connection.status == "LISTEN" and \
connection.laddr[1] == self.port: connection.laddr[1] == self.port:
process.terminate() process.terminate()
except AccessDenied, e: except (NoSuchProcess, AccessDenied):
pass pass
except TypeError, e: except Exception:
# exception to prevent one psutil issue with zombie processes logger.error("Unexpected error releasing openoffice port", exc_info=True)
logger.debug(e)
except NotImplementedError, e:
logger.error("lsof isn't installed on this machine: " + str(e))
def start(self, init=True): def start(self, init=True):
"""Start Instance.""" """Start Instance."""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment