Skip to content

Commit

Permalink
OSX: get rid of catch_zombie() ctx mgr
Browse files Browse the repository at this point in the history
This was done in order to solve
#1044 and
#1100
...but its logic duplicates the one in wrap_exceptions()
decorator.

Also, this should solve #1901 as it did erroneously translated NSP in
AD.

Signed-off-by: Giampaolo Rodola <g.rodola@gmail.com>
  • Loading branch information
giampaolo committed Jan 8, 2021
1 parent 9bcbcbe commit 9127f2a
Showing 1 changed file with 10 additions and 47 deletions.
57 changes: 10 additions & 47 deletions psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

"""macOS platform implementation."""

import contextlib
import errno
import functools
import os
Expand Down Expand Up @@ -354,32 +353,6 @@ def wrapper(self, *args, **kwargs):
return wrapper


@contextlib.contextmanager
def catch_zombie(proc):
"""There are some poor C APIs which incorrectly raise ESRCH when
the process is still alive or it's a zombie, or even RuntimeError
(those who don't set errno). This is here in order to solve:
https://github.com/giampaolo/psutil/issues/1044
"""
try:
yield
except (OSError, RuntimeError) as err:
if isinstance(err, RuntimeError) or err.errno == errno.ESRCH:
try:
# status() is not supposed to lie and correctly detect
# zombies so if it raises ESRCH it's true.
status = proc.status()
except NoSuchProcess:
raise err
else:
if status == _common.STATUS_ZOMBIE:
raise ZombieProcess(proc.pid, proc._name, proc._ppid)
else:
raise AccessDenied(proc.pid, proc._name)
else:
raise


class Process(object):
"""Wrapper class around underlying C implementation."""

Expand All @@ -402,8 +375,7 @@ def _get_kinfo_proc(self):
@memoize_when_activated
def _get_pidtaskinfo(self):
# Note: should work for PIDs owned by user only.
with catch_zombie(self):
ret = cext.proc_pidtaskinfo_oneshot(self.pid)
ret = cext.proc_pidtaskinfo_oneshot(self.pid)
assert len(ret) == len(pidtaskinfo_map)
return ret

Expand All @@ -422,18 +394,15 @@ def name(self):

@wrap_exceptions
def exe(self):
with catch_zombie(self):
return cext.proc_exe(self.pid)
return cext.proc_exe(self.pid)

@wrap_exceptions
def cmdline(self):
with catch_zombie(self):
return cext.proc_cmdline(self.pid)
return cext.proc_cmdline(self.pid)

@wrap_exceptions
def environ(self):
with catch_zombie(self):
return parse_environ_block(cext.proc_environ(self.pid))
return parse_environ_block(cext.proc_environ(self.pid))

@wrap_exceptions
def ppid(self):
Expand All @@ -442,8 +411,7 @@ def ppid(self):

@wrap_exceptions
def cwd(self):
with catch_zombie(self):
return cext.proc_cwd(self.pid)
return cext.proc_cwd(self.pid)

@wrap_exceptions
def uids(self):
Expand Down Expand Up @@ -516,8 +484,7 @@ def open_files(self):
if self.pid == 0:
return []
files = []
with catch_zombie(self):
rawlist = cext.proc_open_files(self.pid)
rawlist = cext.proc_open_files(self.pid)
for path, fd in rawlist:
if isfile_strict(path):
ntuple = _common.popenfile(path, fd)
Expand All @@ -530,8 +497,7 @@ def connections(self, kind='inet'):
raise ValueError("invalid %r kind argument; choose between %s"
% (kind, ', '.join([repr(x) for x in conn_tmap])))
families, types = conn_tmap[kind]
with catch_zombie(self):
rawlist = cext.proc_connections(self.pid, families, types)
rawlist = cext.proc_connections(self.pid, families, types)
ret = []
for item in rawlist:
fd, fam, type, laddr, raddr, status = item
Expand All @@ -544,22 +510,19 @@ def connections(self, kind='inet'):
def num_fds(self):
if self.pid == 0:
return 0
with catch_zombie(self):
return cext.proc_num_fds(self.pid)
return cext.proc_num_fds(self.pid)

@wrap_exceptions
def wait(self, timeout=None):
return _psposix.wait_pid(self.pid, timeout, self._name)

@wrap_exceptions
def nice_get(self):
with catch_zombie(self):
return cext_posix.getpriority(self.pid)
return cext_posix.getpriority(self.pid)

@wrap_exceptions
def nice_set(self, value):
with catch_zombie(self):
return cext_posix.setpriority(self.pid, value)
return cext_posix.setpriority(self.pid, value)

@wrap_exceptions
def status(self):
Expand Down

0 comments on commit 9127f2a

Please sign in to comment.