Blame SOURCES/python-linux-procfs-Propagate-error-to-user-if-a-pid.patch

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