Fixed a locking race condition