Fixes to address file descriptor leakage