Make sure file descriptors are closed after each loop