Merge pull request #114 from chriskuehl/fix-race-reading-proc
Fix race reading /proc in tests
This commit is contained in:
commit
e1205f33f7
1 changed files with 6 additions and 3 deletions
|
@ -50,14 +50,17 @@ def child_pids(pid):
|
||||||
"""Return a list of direct child PIDs for the given PID."""
|
"""Return a list of direct child PIDs for the given PID."""
|
||||||
children = set()
|
children = set()
|
||||||
for p in LocalPath('/proc').listdir():
|
for p in LocalPath('/proc').listdir():
|
||||||
stat = p.join('stat')
|
try:
|
||||||
if stat.isfile():
|
stat = open(p.join('stat').strpath).read()
|
||||||
stat = stat.open().read()
|
|
||||||
m = re.match('^\d+ \(.+?\) [a-zA-Z] (\d+) ', stat)
|
m = re.match('^\d+ \(.+?\) [a-zA-Z] (\d+) ', stat)
|
||||||
assert m, stat
|
assert m, stat
|
||||||
ppid = int(m.group(1))
|
ppid = int(m.group(1))
|
||||||
if ppid == pid:
|
if ppid == pid:
|
||||||
children.add(int(p.basename))
|
children.add(int(p.basename))
|
||||||
|
except IOError:
|
||||||
|
# Happens when the process exits after listing it, or between
|
||||||
|
# opening stat and reading it.
|
||||||
|
pass
|
||||||
return children
|
return children
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue