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":
On Tue, Nov 23, 2021 at 01:11:55PM -0500, John Kacur wrote:
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
Looks good Reviewed-by: Leah Leshchinsky lleshchi@redhat.com
On Wed, 24 Nov 2021, Leah Leshchinsky wrote:
On Tue, Nov 23, 2021 at 01:11:55PM -0500, John Kacur wrote:
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
Looks good Reviewed-by: Leah Leshchinsky lleshchi@redhat.com
Added your Reviewed-by, thanks
John
tuna-devel@lists.fedorahosted.org