Fixed threading issue.
I was still not able to reproduce the infinite loop. I just fix the
thread safety.