Make sure fd is closed to avoid a possible resource leak