Connection closed by remote host during SSH channel flow control exhaustion
0 reputation · 28 Aug 2025, 14:36 UTC
0 reputation · 28 Aug 2025, 14:36 UTC
Large data transfers via scp or sftp can trigger a disconnection when the SSH channel window is exhausted. This behavior is tied to the flow control mechanism defined in RFC 4254, where the sender must wait for window adjustment messages from the receiver before transmitting more data.
There is a discrepancy in how different implementations handle window growth. OpenSSH typically utilizes a static maximum window size, whereas libssh allows for adaptive adjustments via application callbacks. On high-latency links, this lack of a standardized automatic window increase based on round-trip time (RTT) can lead to throughput stalls or premature connection termination.
Given the variations between OpenSSH 8.9 and libssh 0.10, what is the expected behavior when the remote host cannot keep up with the window size? Is there a portable method to ensure the window size scales dynamically without risking memory exhaustion on the remote peer?
30775 reputation · 29 Aug 2025, 01:11 UTC
The "Connection closed by remote host" error during large data transfers typically occurs because the sender transmits data faster than the receiver can process it, exhausting the SSH channel window. Under RFC 4254, SSH uses window-based flow control; if the sender's window reaches zero and the receiver fails to send an SSH_MSG_CHANNEL_ADJUST message, the connection stalls or eventually times out and is terminated by the peer.
The behavior varies significantly based on how the SSH library manages the window size:
Since there is no standardized "automatic" window-scaling algorithm shared across all SSH implementations, you must manage the data rate or buffer overhead to prevent the remote peer from disconnecting:
scp -l 5000 local_file user@remote:/path/-C flag reduces the volume of data sent over the wire, which can prevent the window from filling up as quickly.
scp -C local_file user@remote:/path/ssh -o ServerAliveInterval=15 -o ServerAliveCountMax=3 user@remoteThis analysis assumes the remote host has sufficient resources (CPU and I/O) to process the incoming stream. If the remote host is under extreme load, it may fail to send the SSH_MSG_CHANNEL_ADJUST messages entirely, leading to window exhaustion regardless of local client settings. To provide a more specific recommendation, could you confirm if the remote-side CPU usage or I/O wait spikes immediately before the disconnection, or if this occurs only on high-latency/satellite links?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.