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

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