Prevent deadlock and 100% cpu usage