Fix leaks of file handles

Especially with redirects or `cache: ‘file’`