A while ago I wrote a Rust crate to deal exactly with this problem (which is a problem if you want to use SSH programmatically to run commands remotely).
The README contains my own explanation of the phenomenon.
The README contains my own explanation of the phenomenon.