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

NoSuchProcess not raised on Windows when accessing timing methods #36

Closed
giampaolo opened this issue May 23, 2014 · 9 comments
Closed

Comments

@giampaolo
Copy link
Owner

From [email protected] on March 09, 2009 19:20:34

It happens in zombie process test when accessing create_time property but
also get_cpu_times() and get_cpu_percent() methods.

======================================================================
FAIL: test_zombie_process (__main__.TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\pyftpdlib\svn\psutil\test\test_psutil.py", line 236, in
test_zombie_p
rocess
    self.assertRaises(psutil.NoSuchProcess, getattr, p, "create_time")
AssertionError: NoSuchProcess not raised

----------------------------------------------------------------------

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

@giampaolo
Copy link
Owner Author

From [email protected] on March 09, 2009 18:07:07

I looked into the C code by using some debugging prints and it seems that the culprit
is OpenProcess() which doesn't return NULL, maybe because it automatically caches
handlers of processes which existed in system process table.

By running the code below the exception is thrown at the last instruction.
Since it seems we can't rely on OpenProcess() we might borrow some of the code we
used in get_process_info() to make it raise NSP when necessary.


import time, subprocess

from psutil import Process

sub = subprocess.Popen('python')
time.sleep(0.1)
p = Process(sub.pid)
p.kill()
time.sleep(0.5)
p.create_time
p.create_time
p = Process(sub.pid)

@giampaolo
Copy link
Owner Author

From [email protected] on March 12, 2009 09:47:44

It's the same problem we had before, OpenProcess() will succeed even if the process
no longer exists in many cases, you have to also check the exit code of the process.
There's already a BOOL is_running(HANDLE hProcess) function in
arch/mswindows/process_info.c that returns TRUE or FALSE if the process is actually
running. 

However, even if you call is_running() right before getting process times, it won't
necessarily help if the process dies after that but before GetProcessTimes(). We need
to know what error code is being returned if GetProcessTimes() fails because the
process has gone away (e.g. ERROR_ACCESS_DENIED, ERROR_INVALID_PARAMETER). The only
way to find out is reproduce the error without suppressing the exception the way the
test case does and get the error code from the exception raised, or use printf
debugging to print GetLastError(). We already check for ERROR_ACCESS_DENIED from
GetProcessTimes() so maybe it's returning INVALID_PARAMETER to indicate a process
handle to a dead process?

Status: Accepted

@giampaolo
Copy link
Owner Author

From [email protected] on March 12, 2009 10:01:24

Actually, this one is different. If you print the value, you'll see that
get_create_time() is actually returning a real value, there's no error. There's no
NoSuchProcess exception raised, because the function actually succeeds in getting the
process create time. I haven't checked, but since both functions are calling
GetProcessTimes() it's likely the same thing is happening in get_cpu_times() too. 

I suggest we just close this as WontFix, and remove the assertions from the test
suite. If these functions succeed on a process that has recently died, then there's
no reason IMO to force our properties to raise an exception. If the function call to
GetProcessTimes() fails, then I think it makes sense for us to raise an exception,
which are already doing.

@giampaolo
Copy link
Owner Author

From [email protected] on March 12, 2009 11:46:53

Removed tests as r246 .

Status: WontFix

@giampaolo
Copy link
Owner Author

From [email protected] on February 22, 2010 15:22:32

(related to issue 76 )
It seems that GetExitProcess() ( r512 ) can solve this problem.
Comments?

Status: ReOpened
Labels: -Milestone-0.1.2

@giampaolo
Copy link
Owner Author

From [email protected] on February 22, 2010 15:40:25

Status: WaitingForReview

@giampaolo
Copy link
Owner Author

From [email protected] on February 28, 2010 17:01:03

Is this fixed? I don't see any errors from test_zombie_process on my Windows machine

@giampaolo
Copy link
Owner Author

From [email protected] on March 01, 2010 00:19:19

Yes, it's fixed. We are now using GetExitProcess() every time we use OpenProcess(),
which seems the right thing to do.

Status: Fixed

@giampaolo
Copy link
Owner Author

From g.rodola on March 02, 2013 03:47:08

Updated csets after the SVN -> Mercurial migration: r246 == revision 1cd2a04e4049 r512 == revision 27636ff3db58

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