Ensure closing of subprocess stdout and stderr pipes, avoiding errors due to too many open files