Ensure an open file handle is closed

There is no specific test to validate this fix: we tried a few
approaches, but none were satisfactory. However, the rootfs
integration tests fail when the file handle is not closed.
Improve test directory cleanup