vmolsa / psutil

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

wait_procs won't wait for timeout #470

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
When waiting for 2 or more processes, as soon as the gone processes are more 
than the alive processes, wait_procs() will return, irregardless of the 
remaining timeout.

Attaching patch to psutil-1.2.1; however the same problem affects the latest 
version in mercurial as of Jan 19 2014 (adapting the patch is trivial).

Original issue reported on code.google.com by crusade...@gmail.com on 27 Jan 2014 at 11:40

Attachments:

GoogleCodeExporter commented 9 years ago
I'm not sure I understand.
Are you saying wait_procs() returns immediately even if there are still 
processess to wait for? 

Original comment by g.rodola on 28 Jan 2014 at 1:02

GoogleCodeExporter commented 9 years ago
Yes exactly. The current algorithm goes:

try:
    max_timeout = 1.0 / (len(alive) - len(gone))
except ZeroDivisionError:
    max_timeout = 1.0
timeout = min((deadline - timer()), max_timeout)

So whenever len(gone) > len(alive), the loop breaks and the whole method exits 
prematurely. Also, the algorithm as it is now fails in its intent of having the 
whole loop take a total of 1 cumulative second.

Original comment by crusade...@gmail.com on 28 Jan 2014 at 1:16

GoogleCodeExporter commented 9 years ago
Ah, I see what you mean. I think your patch is incorrect though in that it 
needs to take ZeroDivionError into account.
AFAIU it should just be:

diff --git a/psutil/__init__.py b/psutil/__init__.py
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -1272,7 +1272,7 @@
             # processes may disappear in the meantime and their PID
             # reused.
             try:
-                max_timeout = 1.0 / (len(alive) - len(gone))
+                max_timeout = 1.0 / len(alive)
             except ZeroDivisionError:
                 max_timeout = 1.0  # one alive remaining
             if timeout is not None:

Original comment by g.rodola on 28 Jan 2014 at 1:52

GoogleCodeExporter commented 9 years ago
Inogre my last comment about ZeroDivisionError.
I committed your patch as-is in revision 07229e0f40bd.
Thanks.

Original comment by g.rodola on 28 Jan 2014 at 1:24

GoogleCodeExporter commented 9 years ago
Closing out as fixed as 2.0.0 version is finally out.

Original comment by g.rodola on 10 Mar 2014 at 11:36