Fix file and socket leak