I’ve just found accidentally a solution for a very rare (at least I’ve seen it only a few times), but rather annoying SSH client issue (bug?). Sometimes when the SSH connection times out OR after a clean exit from the remote shell, the SSH connection hangs and the terminal remains virtually between the remote and local machine. You’re unable to do anything on the remote machine as the disconnection is in progress, but you cannot do anything on your local machine yet as the SSH client is still runnig.
The solution for this is an SSH escape sequence, the sequence of the enter, tilde and period keys (so you should press enter and then “~” followed by “.”). This forces the SSH connection to close so your client should exit.
Source: Way to avoid ssh connection timeout & freezing of terminal tab question on superuser.com.