summaryrefslogtreecommitdiff
path: root/psutil
diff options
context:
space:
mode:
authorDaniel Li <daniel.li@deshaw.com>2022-09-29 11:44:52 -0400
committerGitHub <noreply@github.com>2022-09-29 17:44:52 +0200
commit052c1e2ddbd712c201786b7cc9983a4284d3a6c8 (patch)
tree5634bf2f7488b38e2a4f38adcac562636269c119 /psutil
parent69b572ef62ff349495d7884e231ed9faec7775c6 (diff)
downloadpsutil-052c1e2ddbd712c201786b7cc9983a4284d3a6c8.tar.gz
Resolve race condition in Process.threads() (#2151)
* Resolve race condition in Process.threads() Process.threads() has a race condition triggered when a thread exits after the open_binary() call and before the read() call. When this happens, the read() call raises ProcessLookupError. Handle the race condition by catching ProcessLookupError from read() and treating the same as a FileNotFoundError from open_binary(). This is the same approach used in ppid_map(). Signed-off-by: Daniel Li <daniel.li@deshaw.com> * Also catch ProcessLookupError in open_files() Signed-off-by: Daniel Li <daniel.li@deshaw.com>
Diffstat (limited to 'psutil')
-rw-r--r--psutil/_pslinux.py8
1 files changed, 4 insertions, 4 deletions
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py
index 206241f6..9dc9643a 100644
--- a/psutil/_pslinux.py
+++ b/psutil/_pslinux.py
@@ -2061,9 +2061,9 @@ class Process(object):
try:
with open_binary(fname) as f:
st = f.read().strip()
- except FileNotFoundError:
- # no such file or directory; it means thread
- # disappeared on us
+ except (FileNotFoundError, ProcessLookupError):
+ # no such file or directory or no such process;
+ # it means thread disappeared on us
hit_enoent = True
continue
# ignore the first two values ("pid (exe)")
@@ -2217,7 +2217,7 @@ class Process(object):
with open_binary(file) as f:
pos = int(f.readline().split()[1])
flags = int(f.readline().split()[1], 8)
- except FileNotFoundError:
+ except (FileNotFoundError, ProcessLookupError):
# fd gone in the meantime; process may
# still be alive
hit_enoent = True