python-linux-procfs: Fix when pids complete

- Propagate an error to the user when a pid completes
- Handle a pid disappearing in pflags

Resolves: rhbz#2012288
Signed-off-by: John Kacur <jkacur@redhat.com>
This commit is contained in:
John Kacur 2021-11-23 13:40:17 -05:00
parent 0ebf821b7e
commit 177c3d2403
3 changed files with 127 additions and 2 deletions

View File

@ -0,0 +1,68 @@
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

View File

@ -0,0 +1,50 @@
From eb984b30e325bbf27844bf9c1f90767504468db5 Mon Sep 17 00:00:00 2001
From: John Kacur <jkacur@redhat.com>
Date: Tue, 23 Nov 2021 13:01:05 -0500
Subject: [PATCH 2/3] python-linux-procfs: pflags: Handle pids that completed
Sometimes pids disappear when they are completed.
Programs such as pflags that use procfs must account for that.
The solution is to simply recognize this situation, and to continue.
Signed-off-by: John Kacur <jkacur@redhat.com>
---
pflags | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/pflags b/pflags
index 3407b6f51c96..46d396c87c2b 100755
--- a/pflags
+++ b/pflags
@@ -50,14 +50,25 @@ def main(argv):
pids = list(ps.processes.keys())
pids.sort()
- len_comms = [len(ps[pid]["stat"]["comm"]) for pid in pids if pid in ps]
+ len_comms = []
+ for pid in pids:
+ if pid in ps:
+ try:
+ len(ps[pid]["stat"]["comm"])
+ except (TypeError, FileNotFoundError):
+ continue
+ len_comms.append(len(ps[pid]["stat"]["comm"]))
+
max_comm_len = max(len_comms, default=0)
del len_comms
for pid in pids:
if pid not in ps:
continue
- flags = ps[pid].stat.process_flags()
+ try:
+ flags = ps[pid].stat.process_flags()
+ except AttributeError:
+ continue
# Remove flags that were superseeded
if "PF_THREAD_BOUND" in flags and "PF_NO_SETAFFINITY" in flags:
flags.remove("PF_THREAD_BOUND")
--
2.31.1

View File

@ -1,6 +1,6 @@
Name: python-linux-procfs Name: python-linux-procfs
Version: 0.6.3 Version: 0.6.3
Release: 3%{?dist} Release: 4%{?dist}
License: GPLv2 License: GPLv2
Summary: Linux /proc abstraction classes Summary: Linux /proc abstraction classes
URL: https://git.kernel.org/pub/scm/libs/python/%{name}/%{name}.git URL: https://git.kernel.org/pub/scm/libs/python/%{name}/%{name}.git
@ -14,6 +14,8 @@ Abstractions to extract information from the Linux kernel /proc files.
# PATCHES # PATCHES
Patch1: python-linux-procfs-Fix-traceback-with-non-utf8-char.patch Patch1: python-linux-procfs-Fix-traceback-with-non-utf8-char.patch
Patch2: python-linux-procfs-Propagate-error-to-user-if-a-pid.patch
Patch3: python-linux-procfs-pflags-Handle-pids-that-complete.patch
%description %_description %description %_description
@ -21,7 +23,7 @@ Patch1: python-linux-procfs-Fix-traceback-with-non-utf8-char.patch
Summary: %summary Summary: %summary
%{?python_provide:%python_provide python3-linux-procfs} %{?python_provide:%python_provide python3-linux-procfs}
Requires: python3-six Requires: python3
%description -n python3-linux-procfs %_description %description -n python3-linux-procfs %_description
@ -44,6 +46,11 @@ rm -rf %{buildroot}
%license COPYING %license COPYING
%changelog %changelog
* Tue Nov 23 2021 John Kacur <jkacur@redhat.com> - 0.6.3-4
- Propagate error to user if pid completed
- Handle pid completed in pflags
Resolves: rhbz#2012288
* Mon Nov 22 2021 John Kacur <jkacur@redhat.com> - 0.6.3-3 * Mon Nov 22 2021 John Kacur <jkacur@redhat.com> - 0.6.3-3
- Fix traceback with non-utf8 chars - Fix traceback with non-utf8 chars
Resolves: rhbz#2022530 Resolves: rhbz#2022530