Fix possible deadlock in error conditions