Merge pull request #114 from chriskuehl/fix-race-reading-proc

Fix race reading /proc in tests
This commit is contained in:
Chris Kuehl 2016-08-25 14:08:33 -07:00 committed by GitHub
commit e1205f33f7

View file

@ -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