nikademus79 / psutil

Automatically exported from code.google.com/p/psutil
Other
0 stars 0 forks source link

Process get_threads() and get_num_threads() occasionally fails with NoSuchProcess exception on Windows #180

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
p = psutil.Process(os.getpid())
x = 0
while 1:
    x += 1
    p.get_threads()
    print x

What is the expected output? What do you see instead?
After a while  the script above will fail with a NoSuchProcess "process no 
longer exists" exception.
Problem is OpenThread() call in _psutil_mswindows.c which suddenly fails with 
ERROR_INVALID_PARAMETER which we treat as a synonym for "NoSuchProcess".
Still not sure what is the right thing to do in that case.

Original issue reported on code.google.com by g.rodola on 7 Jul 2011 at 10:49

GoogleCodeExporter commented 8 years ago
I don't understand the problem here. Can you please elaborate?

Original comment by wj32...@gmail.com on 7 Jul 2011 at 10:54

GoogleCodeExporter commented 8 years ago
Problem is the script above should never fail with "process no longer exists" 
because the process obviously still exists considering we are inside an 
infinite while loop. =)

This behavior comes from:
http://code.google.com/p/psutil/source/browse/tags/release-0.2.1/psutil/_psutil_
mswindows.c#966
What happens is OpenThread() fails with ERROR_INVALID_PARAMETER which we 
erroneously treat as a synonym for "process has gone away" and hence we raise 
NoSuchProcess exception.

I think OpenThread() returning ERROR_INVALID_PARAMETER at that point actually 
means that the thread has disappeared on us in which case we should just 
"continue" the for loop.

Original comment by g.rodola on 7 Jul 2011 at 10:59

GoogleCodeExporter commented 8 years ago
I see. STATUS_INVALID_CID (which is what NtOpenThread returns and is translated 
to ERROR_INVALID_PARAMETER) indicates that the thread does not exist, but that 
doesn't say anything about the process. In this case you probably want to 
continue the loop.

Original comment by wj32...@gmail.com on 7 Jul 2011 at 11:06

GoogleCodeExporter commented 8 years ago
By the way, if you used the get_process_info function you could tell if the 
process itself is dead, as well as getting a thread count, which is in 
SYSTEM_PROCESS_INFORMATION->NumberOfThreads. It's much faster and easier.

Original comment by wj32...@gmail.com on 7 Jul 2011 at 11:09

GoogleCodeExporter commented 8 years ago
We are not only getting the number of threads but also the CPU times the thread 
has spend in system and user mode.

Original comment by g.rodola on 7 Jul 2011 at 11:13

GoogleCodeExporter commented 8 years ago
It's all in there. SYSTEM_THREAD_INFORMATION has KernelTime, UserTime, 
CreateTime, and a bunch of other stuff.

Original comment by wj32...@gmail.com on 7 Jul 2011 at 11:15

GoogleCodeExporter commented 8 years ago
It turns out both get_threads() and get_num_threads() suffer this problem which 
is now fixed in r1088 and r1089.
Thanks wj32.64, I'll see to replace the current implementation by using 
SYSTEM_PROCESS_INFORMATION in a future commit.
Closing this out as fixed.

Original comment by g.rodola on 7 Jul 2011 at 1:11

GoogleCodeExporter commented 8 years ago

Original comment by g.rodola on 8 Jul 2011 at 7:07

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Updated csets after the SVN -> Mercurial migration:
r1088 == revision 86f100584cd6

Original comment by g.rodola on 2 Mar 2013 at 12:01