Fix locking code on error path.