rust-lang / rust-lang/ssh2-rs

Incorrect timeout

Open
#341 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Rust
Stars
566
Forks
163
Avg merge
4h 51m
Merged PRs (30d)
2

Description

When setting a session timeout, the behaviour seems incorrect! Consider this code that connects via ssh and runs sleep 30 && echo DONE to sleep for 30 seconds:

    let username = "...";
    let ip = "...";
    let password = "...";

    let timeout_ms = 15_000;
    let command = "sleep 30 && echo DONE"

    let tcp = std::net::TcpStream::connect(format!("{ip}:22"))?;
    let mut sess = Session::new()?;
    sess.set_timeout(timeout_ms);
    sess.set_tcp_stream(tcp);
    sess.handshake()?;
    sess.userauth_password(&username, &password)?;
    if !sess.authenticated() {
        return Err(anyhow!("Failed to connect by SSH"));
    }

    // Connect channel in exec mode.
    let mut channel = sess.channel_session()?;
    channel.handle_extended_data(ssh2::ExtendedData::Merge)?;
    println!("running command: {command}");
    channel.exec(command)?;
    let mut output = String::new();
    channel.read_to_string(&mut output)?;
    channel.close()?;
    channel.wait_close()?;
    let exit_status = channel.exit_status()?;

    sess.disconnect(
        Some(DisconnectCode::ByApplication),
        "Finished Running Script",
        None,
    )?;

    println!("SUCCESS");
    println!("exit code: {exit_status}");
    println!("output:\n{output}");

I expect the session to timeout after 15 seconds, but instead it times out after 30 seconds! If instead we have timeout_ms = 5_000 then it times out after 15 seconds. From my experimenting I think the current (unexpected) behaviour is as follows:

If the command blocks for duation t and t < timeout then return Ok
If the command blocks for duation t and t > 3*timeout then return a timeout error at time 3*timeout.
If the command blocks for duation t and timeout < t < 3*timeout_ms then return a timeout error at time t

I'm running on windows and with ssh2-0.9.4

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported behavior with the Rust SSH example in the issue, varying timeout_ms and the duration of the remote sleep command. Trace how the session timeout applies during channel execution and verify that the observed timeout occurs at the configured limit rather than at a multiple of it.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.