diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -3444,6 +3444,12 @@ Py_BEGIN_ALLOW_THREADS dirp = fdopendir(fd); + if (dirp == NULL) { + int save_errno = errno; + (void)close(fd); + errno = save_errno; + fd = -1; + } Py_END_ALLOW_THREADS } else