avoid unsound `close` during `readdir` loop