Do you really need to lock the keydir to change its refcount? The only other place where the refcount is changed is line 346 and there the global_keydirs_lock is held, just like here.