Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cached properties ignoring terminated processes and code refactoring #126

Closed
giampaolo opened this issue May 23, 2014 · 14 comments
Closed
Assignees

Comments

@giampaolo
Copy link
Owner

From g.rodola on October 30, 2010 01:48:04

As per private discussion with Jay we realized that some properties are 
different than the rest of the API in that they don't raise NSP exception as 
they should and need fixing.

A part of the API correctly raises NSP if the process has gone away at some point.
This is true for all those methods retrieving information which is variable in 
time, such as all get_* methods and send_signal(), terminate() and kill():

>>> import psutil
>>> p = psutil.Process(1431)
>>> p.get_cpu_times()
cputimes(user=0.02, system=0.0)
>>> p.kill()
>>> p.get_cpu_times()
Traceback (most recent call last):
  File "test/test_psutil.py", line 803, in test_zombie_process
    p.get_cpu_times()
  File "/usr/local/lib/python2.6/dist-packages/psutil/__init__.py", line 291, 
in get_cpu_times
    return self._platform_impl.get_cpu_times()
  File "/usr/local/lib/python2.6/dist-packages/psutil/_pslinux.py", line 177, in wrapper
    raise NoSuchProcess(self.pid, self._process_name)
NoSuchProcess: process no longer exists (pid=1431)


Other properties returning process *static* information, if accessed *before* 
the process disappears on us, just don't.
This is true for pid, ppid, name, exe and cmdline Process properties, which are 
all supposed to remain unchangeg during the entire life of the process:

>>> import psutil
>>> p = psutil.Process(1431)
>>> p.name
'python'
>>> p.kill()
>>> p.name
'python'


This is due to the fact that once those process information are retrieved the 
first time, results get cached and the underlying code which actually fetch the 
information from the kernel is no longer executed.
This was obviously done for performance but we think it's better for integrity 
to have an API behaving equally everywhere at cost of a reasonably little overhead.

As part of this process the code is gonna change quite a bit, despite no 
actually brand-new code is likely to be written.
Considering that it's not possible to commit a unique patch for all platforms 
I'm going to create a branch so that the trunk doesn't get broken in the process.
I recommend not to commit anything against the trunk as long as the branch 
doesn't get merged back to avoid collisions etc.

Original issue: http://code.google.com/p/psutil/issues/detail?id=126

@giampaolo giampaolo self-assigned this May 23, 2014
@giampaolo
Copy link
Owner Author

From g.rodola on October 29, 2010 16:56:30

Branch at: https://psutil.googlecode.com/svn/branches/cache-refactoring To use 
it from the local copy of the trunk just do:
svn switch https://psutil.googlecode.com/svn/branches/cache-refactoring

@giampaolo
Copy link
Owner Author

From g.rodola on October 29, 2010 17:15:28

Linux code committed in r752 .

Labels: -Progress-0in4 Progress-1in4

@giampaolo
Copy link
Owner Author

From g.rodola on October 29, 2010 18:18:49

FreeBSD code committed in r753 .

Labels: -Progress-1in4 Progress-2in4

@giampaolo
Copy link
Owner Author

From g.rodola on October 29, 2010 20:02:30

Windows code committed in r755 .
Still a couple of failures for zombie ppid and name properties but they're 
gonna get better soon.
Re-assigning to Jay for completion on OSX.

Owner: jloden
Labels: -Progress-2in4 Progress-3in4

@giampaolo
Copy link
Owner Author

From jlo...@gmail.com on October 30, 2010 08:39:48

OS X committed in r757 Everything is passing except for test_zombie_process() 
which is not raising NSP on exe attribute. It seems like it's not raising NSP 
when we read the cmdline to figure out the exe, so it falls through and returns 
"" for exe, instead of raising NSP from the exe attribute.

@giampaolo
Copy link
Owner Author

From jlo...@gmail.com on October 30, 2010 08:39:57

Labels: -Progress-3in4 Progress-4in4

@giampaolo
Copy link
Owner Author

From g.rodola on October 30, 2010 10:12:32

I think you can do as follows, as I did on Windows:

    def get_process_exe(self):
        # no such thing as "exe" on OSX; it will maybe be determined
        # later from cmdline[0]
        if not pid_exists(self.pid):
            raise NoSuchProcess(self.pid, self._process_name)
        return ""

@giampaolo
Copy link
Owner Author

From g.rodola on October 30, 2010 10:45:43

Merged branch changes back into trunk as r760 and deleted branch in r761 .

@giampaolo
Copy link
Owner Author

From jlo...@gmail.com on October 30, 2010 10:51:30

Ok, this works... I also had to do the same for cmdline as well.. it appears 
the cmdline function does not raise NoSuchProcess when the process has gone 
away either. 

This works for now, but for the long term I would like to refactor the OS X 
commandline code (and hopefully fix Issue #83 at the same time. I will open a 
new issue to remind myself to refactor the code at a future date.

@giampaolo
Copy link
Owner Author

From jlo...@gmail.com on October 30, 2010 10:53:53

Fixed OS X in r762

@giampaolo
Copy link
Owner Author

From jlo...@gmail.com on October 30, 2010 10:54:01

Status: FixedInSVN

@giampaolo
Copy link
Owner Author

From g.rodola on November 12, 2010 19:14:59

Status: Fixed

@giampaolo
Copy link
Owner Author

From g.rodola on June 09, 2011 15:33:51

Labels: -OpSys-All

@giampaolo
Copy link
Owner Author

From g.rodola on March 02, 2013 03:56:09

Updated csets after the SVN -> Mercurial migration: r752 == revision ??? r753 
== revision ??? r755 == revision ??? r757 == revision ??? r760 == revision 
9eb8a9833278 r762 == revision 22a716a1d49f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant