Fixed edge case that could result in leaked file handles.