python-linux-procfs/python-linux-procfs-Propaga...

69 lines
2.3 KiB
Diff

From b7ea06b21456d465f2d9d11358fb803eb277357f Mon Sep 17 00:00:00 2001
From: John Kacur <jkacur@redhat.com>
Date: Tue, 23 Nov 2021 09:58:58 -0500
Subject: [PATCH 1/3] python-linux-procfs: Propagate error to user if a pid is
completed
If a pid is completed and disappears a FileNotFoundError will occur
because /proc/pid/stat will disappear too.
It is not possible to check for the file first because it could still
disappear between the time of the check and the time of use.
Propagate this error to the user.
The user should handle this with a try, except clause and ignore it if
an exception occurs.
Signed-off-by: John Kacur <jkacur@redhat.com>
---
procfs/procfs.py | 19 ++++++++++++++++---
1 file changed, 16 insertions(+), 3 deletions(-)
diff --git a/procfs/procfs.py b/procfs/procfs.py
index 408b2bcd0a31..a0e9977214fe 100755
--- a/procfs/procfs.py
+++ b/procfs/procfs.py
@@ -130,7 +130,12 @@ class pidstat:
def __init__(self, pid, basedir="/proc"):
self.pid = pid
- self.load(basedir)
+ try:
+ self.load(basedir)
+ except FileNotFoundError:
+ # The file representing the pid has disappeared
+ # propagate the error to the user to handle
+ raise
def __getitem__(self, fieldname):
return self.fields[fieldname]
@@ -151,7 +156,11 @@ class pidstat:
return fieldname in self.fields
def load(self, basedir="/proc"):
- f = open("%s/%d/stat" % (basedir, self.pid))
+ try:
+ f = open("%s/%d/stat" % (basedir, self.pid))
+ except FileNotFoundError:
+ # The pid has disappeared, propagate the error
+ raise
fields = f.readline().strip().split(') ')
f.close()
fields = fields[0].split(' (') + fields[1].split()
@@ -338,7 +347,11 @@ class process:
else:
sclass = pidstatus
- setattr(self, attr, sclass(self.pid, self.basedir))
+ try:
+ setattr(self, attr, sclass(self.pid, self.basedir))
+ except FileNotFoundError:
+ # The pid has disappeared, progate the error
+ raise
elif attr == "cmdline":
self.load_cmdline()
elif attr == "threads":
--
2.31.1