Fix leaky fd issue (bug #2832)