Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,27 @@ def pipe_cloexec(self):


def _close_fds(self, but):
if sys.platform.startswith("linux"):
# Try closing the actual opened fds (obtained via /proc),
# as this is way faster than iterating through MAX_FD
try:
for f in os.listdir("/proc/self/fd"):
try:
fd = int(f)
except (ValueError, TypeError): # should not happen
continue
if fd < 3 or fd == but:
continue

try:
os.close(fd)
except OSError:
pass
return
except OSError:
# /proc not available? fall back to the old method
pass

if hasattr(os, 'closerange'):
os.closerange(3, but)
os.closerange(but + 1, MAXFD)
Expand Down