Split lock into ctx and cm locks + some lock fixes on connection errors