Ensure open file handles are closed again.

This fixes a "Error: EMFILE, too many open files" error we've been seeing.