Fix error path so mutex is unlocked before returning