Fix file leak in key manager