log: fixed a race condition leading to random crashes