diff --git a/Modules/_testexternalinspection.c b/Modules/_testexternalinspection.c index 2a665affb5e7f8..2ed79f317b0417 100644 --- a/Modules/_testexternalinspection.c +++ b/Modules/_testexternalinspection.c @@ -345,8 +345,10 @@ get_py_runtime_linux(pid_t pid) } exit: - if (close(fd) != 0) { + if (fd >= 0 && close(fd) != 0) { + PyObject *exc = PyErr_GetRaisedException(); PyErr_SetFromErrno(PyExc_OSError); + _PyErr_ChainExceptions1(exc); } if (file_memory != NULL) { munmap(file_memory, file_stats.st_size);