|
|
21ad0d |
From a752e7995bfdabb08b5bb6bcd437b9a5d2e53a7a Mon Sep 17 00:00:00 2001
|
|
|
21ad0d |
From: John Kacur <jkacur@redhat.com>
|
|
|
21ad0d |
Date: Thu, 9 Dec 2021 10:11:07 -0500
|
|
|
21ad0d |
Subject: [PATCH 2/2] python-linux-procfs: Fix UnicodeDecodeError
|
|
|
21ad0d |
|
|
|
21ad0d |
Commit 7570fc0d6082 meant to solve the UnicodeDecodeError
|
|
|
21ad0d |
|
|
|
21ad0d |
Instead it actually increased the problem by reading lines as bytes
|
|
|
21ad0d |
and decoding them.
|
|
|
21ad0d |
|
|
|
21ad0d |
The original problem is hard to trigger and doesn't trigger consistently
|
|
|
21ad0d |
with reproducers. In addition there seems to be a difference in how this
|
|
|
21ad0d |
is handled between python-3.6 to python-3.9
|
|
|
21ad0d |
|
|
|
21ad0d |
For now, we should return the code to reading as utf-8 (the default)
|
|
|
21ad0d |
since that handles more cases than the current code.
|
|
|
21ad0d |
|
|
|
21ad0d |
We can catch the UnicodeDecodeError and ignore it for now. It is not
|
|
|
21ad0d |
ideal because we are not handling some pids that trigger the error.
|
|
|
21ad0d |
|
|
|
21ad0d |
This patch also includes a fix for a FileNotFoundError which can occur if
|
|
|
21ad0d |
a pid exits and disappears before we try to read it in the /proc file
|
|
|
21ad0d |
system.
|
|
|
21ad0d |
|
|
|
21ad0d |
Signed-off-by: John Kacur <jkacur@redhat.com>
|
|
|
21ad0d |
---
|
|
|
21ad0d |
procfs/procfs.py | 18 ++++++++++++++----
|
|
|
21ad0d |
1 file changed, 14 insertions(+), 4 deletions(-)
|
|
|
21ad0d |
|
|
|
21ad0d |
diff --git a/procfs/procfs.py b/procfs/procfs.py
|
|
|
21ad0d |
index a78bac5376e3..de55dfc1aef4 100755
|
|
|
21ad0d |
--- a/procfs/procfs.py
|
|
|
21ad0d |
+++ b/procfs/procfs.py
|
|
|
21ad0d |
@@ -44,7 +44,11 @@ def process_cmdline(pid_info):
|
|
|
21ad0d |
if pid_info["cmdline"]:
|
|
|
21ad0d |
return reduce(lambda a, b: a + " %s" % b, pid_info["cmdline"]).strip()
|
|
|
21ad0d |
|
|
|
21ad0d |
- return pid_info["stat"]["comm"]
|
|
|
21ad0d |
+ try:
|
|
|
21ad0d |
+ """ If a pid disappears before we query it, return None """
|
|
|
21ad0d |
+ return pid_info["stat"]["comm"]
|
|
|
21ad0d |
+ except:
|
|
|
21ad0d |
+ return None
|
|
|
21ad0d |
|
|
|
21ad0d |
|
|
|
21ad0d |
class pidstat:
|
|
|
21ad0d |
@@ -374,9 +378,15 @@ class process:
|
|
|
21ad0d |
return hasattr(self, attr)
|
|
|
21ad0d |
|
|
|
21ad0d |
def load_cmdline(self):
|
|
|
21ad0d |
- with open("/proc/%d/cmdline" % self.pid, mode='rb') as f:
|
|
|
21ad0d |
- cmdline = f.readline().decode(encoding='unicode_escape')
|
|
|
21ad0d |
- self.cmdline = cmdline.strip().split('\0')[:-1]
|
|
|
21ad0d |
+ try:
|
|
|
21ad0d |
+ with open("/proc/%d/cmdline" % self.pid) as f:
|
|
|
21ad0d |
+ self.cmdline = f.readline().strip().split('\0')[:-1]
|
|
|
21ad0d |
+ except FileNotFoundError:
|
|
|
21ad0d |
+ """ This can happen when a pid disappears """
|
|
|
21ad0d |
+ self.cmdline = None
|
|
|
21ad0d |
+ except UnicodeDecodeError:
|
|
|
21ad0d |
+ """ TODO - this shouldn't happen, needs to be investigated """
|
|
|
21ad0d |
+ self.cmdline = None
|
|
|
21ad0d |
|
|
|
21ad0d |
def load_threads(self):
|
|
|
21ad0d |
self.threads = pidstats("/proc/%d/task/" % self.pid)
|
|
|
21ad0d |
--
|
|
|
21ad0d |
2.31.1
|
|
|
21ad0d |
|