diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2019-02-27 02:01:10 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2019-02-27 02:01:10 +0100 |
commit | a5d179e0d97eeeb8112ca2bf72f846bd1ca2b161 (patch) | |
tree | c4129c6aec1f2ab8194df445321fa0ebc2a5ca2b | |
parent | d4d896512d01c7c14a4c145232e1c6bb6f4c3631 (diff) | |
download | psutil-a5d179e0d97eeeb8112ca2bf72f846bd1ca2b161.tar.gz |
_assert_alive() refactor (linux)
-rw-r--r-- | psutil/_pslinux.py | 20 | ||||
-rw-r--r-- | psutil/_pssunos.py | 18 |
2 files changed, 21 insertions, 17 deletions
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 011b3541..2eadc4c7 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -1540,6 +1540,12 @@ class Process(object): self._ppid = None self._procfs_path = get_procfs_path() + def _assert_alive(self): + """Raise NSP if the process disappeared on us.""" + # For those C function who do not raise NSP, possibly returning + # incorrect or incomplete result. + os.stat('%s/%s' % (self._procfs_path, self.pid)) + @memoize_when_activated def _parse_stat_file(self): """Parse /proc/{pid}/stat file and return a dict with various @@ -1906,8 +1912,7 @@ class Process(object): ntuple = _common.pthread(int(thread_id), utime, stime) retlist.append(ntuple) if hit_enoent: - # raise NSP if the process disappeared on us - os.stat('%s/%s' % (self._procfs_path, self.pid)) + self._assert_alive() return retlist @wrap_exceptions @@ -2070,9 +2075,8 @@ class Process(object): flags = int(f.readline().split()[1], 8) except IOError as err: if err.errno == errno.ENOENT: - # fd gone in the meantime; does not - # necessarily mean the process disappeared - # on us. + # fd gone in the meantime; process may + # still be alive hit_enoent = True else: raise @@ -2082,15 +2086,13 @@ class Process(object): path, int(fd), int(pos), mode, flags) retlist.append(ntuple) if hit_enoent: - # raise NSP if the process disappeared on us - os.stat('%s/%s' % (self._procfs_path, self.pid)) + self._assert_alive() return retlist @wrap_exceptions def connections(self, kind='inet'): ret = _connections.retrieve(kind, self.pid) - # raise NSP if the process disappeared on us - os.stat('%s/%s' % (self._procfs_path, self.pid)) + self._assert_alive() return ret @wrap_exceptions diff --git a/psutil/_pssunos.py b/psutil/_pssunos.py index faadecfe..17469eac 100644 --- a/psutil/_pssunos.py +++ b/psutil/_pssunos.py @@ -380,6 +380,12 @@ class Process(object): self._ppid = None self._procfs_path = get_procfs_path() + def _assert_alive(self): + """Raise NSP if the process disappeared on us.""" + # For those C function who do not raise NSP, possibly returning + # incorrect or incomplete result. + os.stat('%s/%s' % (self._procfs_path, self.pid)) + def oneshot_enter(self): self._proc_name_and_args.cache_activate(self) self._proc_basic_info.cache_activate(self) @@ -522,8 +528,7 @@ class Process(object): continue raise if hit_enoent: - # raise NSP if the process disappeared on us - os.stat('%s/%s' % (procfs_path, self.pid)) + self._assert_alive() @wrap_exceptions def cwd(self): @@ -585,8 +590,7 @@ class Process(object): nt = _common.pthread(tid, utime, stime) ret.append(nt) if hit_enoent: - # raise NSP if the process disappeared on us - os.stat('%s/%s' % (procfs_path, self.pid)) + self._assert_alive() return ret @wrap_exceptions @@ -610,8 +614,7 @@ class Process(object): if isfile_strict(file): retlist.append(_common.popenfile(file, int(fd))) if hit_enoent: - # raise NSP if the process disappeared on us - os.stat('%s/%s' % (procfs_path, self.pid)) + self._assert_alive() return retlist def _get_unix_sockets(self, pid): @@ -711,8 +714,7 @@ class Process(object): raise retlist.append((addr, perm, name, rss, anon, locked)) if hit_enoent: - # raise NSP if the process disappeared on us - os.stat('%s/%s' % (procfs_path, self.pid)) + self._assert_alive() return retlist @wrap_exceptions |