First deregister, then lock. This is important to avoid deadlocks
between unregistering connections.
Don't readlock connections when they're about to be shut down.