Fix closing of file that possibly failed to open