fix bug closing file descriptors