Fix invalid read and memory leak in `lock_log()`
Protect against the possibility of multiple close attempts