summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2020-10-17 03:19:21 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2020-10-17 03:19:21 +0200
commit239ebc19c8477893870e497fd344744cb5d964a7 (patch)
tree07e9cba5d94988754f7def5a750eb25b5b42f3c9
parent0398213c561faf641f60a625a02d7df17f0d1e89 (diff)
downloadpsutil-239ebc19c8477893870e497fd344744cb5d964a7.tar.gz
fix #1738 / osx / exe(): catch ENOENT which may occur if process is still alive but the executable file which launched it got deleted
-rw-r--r--HISTORY.rst2
-rw-r--r--psutil/_psutil_osx.c16
2 files changed, 15 insertions, 3 deletions
diff --git a/HISTORY.rst b/HISTORY.rst
index af5dab68..3be3915f 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -15,6 +15,8 @@ XXXX-XX-XX
**Bug fixes**
+- 1738_: [macOS] Process.exe() may raise FileNotFoundError if process is still
+ alive but the exe file which launched it got deleted.
- 1791_: [macOS] fix missing include for getpagesize().
- 1823_: [Windows] Process.open_files() may cause a segfault due to a NULL
pointer.
diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c
index fdf34d97..ee8d1c83 100644
--- a/psutil/_psutil_osx.c
+++ b/psutil/_psutil_osx.c
@@ -298,11 +298,21 @@ psutil_proc_exe(PyObject *self, PyObject *args) {
errno = 0;
ret = proc_pidpath(pid, &buf, sizeof(buf));
if (ret == 0) {
- if (pid == 0)
+ if (pid == 0) {
AccessDenied("automatically set for PID 0");
- else
+ return NULL;
+ }
+ else if (errno == ENOENT) {
+ // It may happen (file not found error) if the process is
+ // still alive but the executable which launched it got
+ // deleted, see:
+ // https://github.com/giampaolo/psutil/issues/1738
+ return Py_BuildValue("s", "");
+ }
+ else {
psutil_raise_for_pid(pid, "proc_pidpath()");
- return NULL;
+ return NULL;
+ }
}
return PyUnicode_DecodeFSDefault(buf);
}