russh icon indicating copy to clipboard operation
russh copied to clipboard

channel closes in the middle of command execution

Open MrAliSalehi opened this issue 6 months ago • 2 comments

i have this code:

    pub async fn call<F: Future<Output = Res>>(
        &mut self,
        command: &str,
        on_data_cb: impl Fn(CryptoVec) -> F,
    ) -> eyre::Result<Option<u32>> {
        let  channel = self.session.channel_open_session().await?;
        let (mut read,write) = channel.split();
        
        write.exec(true, command).await?;
        
        let mut code = None;

        loop {
            let Some(msg) = read.wait().await else {
                info!("channel closed for cmd {command}");
                break;
            };
            match msg {
                ChannelMsg::Data { data } => {
                    on_data_cb(data).await.unwrap();
                }
                ChannelMsg::ExitStatus { exit_status } => {
                    code = Some(exit_status);
                    break;
                }
                _ => {}
            }
        }
        write.close().await?;
        Ok(code)
    }

when i execute "short" commands like "chmod"..etc, there is no problem , but when executing longer commands like "apt update" or executing a bash script, after a few seconds channels closes, literally in the middle of executing, no errors on other parts of the code, the read.wait() just returns None, and on write.close() i get Channel Send error

what is happening here exactly? is there a workaround to keep the channel open ?

MrAliSalehi avatar Jul 02 '25 16:07 MrAliSalehi

Can you grab a log with a higher log level (DEBUG) and also check the log on the server side? OpenSSH will log the disconnect reason when it happens.

Eugeny avatar Jul 02 '25 19:07 Eugeny

I'm also having issues with disconnections.

While using russh-sftp I'm being disconnected from the SFTP server during a write_all. Smaller files work fine.

Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh_sftp::protocol: packet type 104
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20  INFO sftp_relay::handlers::put_file: current remote path: /
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20  INFO sftp_relay::handlers::put_file: processing field = name: file, file name: TRE_20250702010101.csv, content_type>
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20  INFO sftp_relay::handlers::put_file: writing 2067749 bytes as file `/Inbound/TRE_20250702010101.csv` on FTP server
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh::sshbuffer: > msg type 94, len 74
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh::client: < msg type 94, seqn 14, len 58
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh_sftp::protocol: packet type 102
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh::sshbuffer: > msg type 94, len 34009
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh::sshbuffer: > msg type 94, len 34009
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh::sshbuffer: > msg type 94, len 34009
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh::sshbuffer: > msg type 94, len 34009
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh::sshbuffer: > msg type 94, len 34009
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh::sshbuffer: > msg type 94, len 34009
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh::sshbuffer: > msg type 94, len 23190
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh::client: received disconnect
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh::client: disconnected: ReceivedDisconnect(RemoteDisconnectInfo { reason_code: ByApplication, message: "User Disconnected", lang_tag: "" })
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh::client: drop session
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh_sftp::client: read half of sftp stream ended
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh_sftp::client: write half of sftp stream ended
Jul 07 16:20:25 localhost sftp-relay[364789]: 07/07 16:20 DEBUG russh::client: drop session
The final msg type 94, len 23190 does not represent the whole file. This tallies up to 227,244 bytes.

It appears that the RemoteDisconnectInfo message is probably coming the from the remote SFTP (GoAnywhere)?

Using sftp from the command-line seems to work fine for sending files though...

Any ideas?

leontoeides avatar Jul 07 '25 19:07 leontoeides