From 1922a7674011852b64f3405e47113ba540a86c77 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sat, 4 Mar 2017 16:13:50 +0700 Subject: [PATCH] fix #986: [Linux] Process.cwd() may raise NoSuchProcess instead of ZombieProcess. --- HISTORY.rst | 1 + psutil/_pslinux.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/HISTORY.rst b/HISTORY.rst index b96cf034e..ad931e770 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -17,6 +17,7 @@ - 872_: [Linux] can now compile on Linux by using MUSL C library. - 985_: [Windows] Fix a crash in `Process.open_files` when the worker thread for `NtQueryObject` times out. +- 986_: [Linux] Process.cwd() may raise NoSuchProcess instead of ZombieProcess. 5.1.3 ===== diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 2884e4d14..533b5485d 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -1617,7 +1617,16 @@ def memory_maps(self): @wrap_exceptions def cwd(self): - return readlink("%s/%s/cwd" % (self._procfs_path, self.pid)) + try: + return readlink("%s/%s/cwd" % (self._procfs_path, self.pid)) + except OSError as err: + # https://github.com/giampaolo/psutil/issues/986 + if err.errno in (errno.ENOENT, errno.ESRCH): + if not pid_exists(self.pid): + raise NoSuchProcess(self.pid, self._name) + else: + raise ZombieProcess(self.pid, self._name, self._ppid) + raise @wrap_exceptions def num_ctx_switches(self, _ctxsw_re=re.compile(b'ctxt_switches:\t(\d+)')):