Fix threading so we don't cause deadlocks.