Add a mutex to prevent deadlock.