Skip to content

Try fix block #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions async-ssh2-lite/src/session_stream/impl_async_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use std::io::{Error as IoError, ErrorKind as IoErrorKind};

use async_io::{Async, Timer};
use async_trait::async_trait;
use futures_util::{future, pin_mut, ready};
use futures_util::{
future::{self, Either},
pin_mut, ready,
};
use ssh2::{BlockDirections, Error as Ssh2Error, Session};

use super::{AsyncSessionStream, BlockDirectionsExt as _};
Expand Down Expand Up @@ -51,10 +54,34 @@ where
assert!(expected_block_directions.is_readable());
assert!(expected_block_directions.is_writable());

let (ret, _) = future::select(self.readable(), self.writable())
let (ret, either) = future::select(self.readable(), self.writable())
.await
.factor_first();
ret?
ret?;
match either {
Either::Left(_) => {
let either = future::select(
self.writable(),
Box::pin(sleep(Duration::from_millis(1000))),
)
.await;
match either {
Either::Left((x, _)) => x?,
Either::Right(_) => {}
}
}
Either::Right(_) => {
let either = future::select(
self.readable(),
Box::pin(sleep(Duration::from_millis(1000))),
)
.await;
match either {
Either::Left((x, _)) => x?,
Either::Right(_) => {}
}
}
}
}
}

Expand Down
83 changes: 78 additions & 5 deletions async-ssh2-lite/src/session_stream/impl_tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use core::{
use std::io::{Error as IoError, ErrorKind as IoErrorKind};

use async_trait::async_trait;
use futures_util::ready;
use futures_util::{
future::{self, Either},
ready,
};
use ssh2::{BlockDirections, Error as Ssh2Error, Session};
use tokio::net::TcpStream;
#[cfg(unix)]
Expand Down Expand Up @@ -50,8 +53,43 @@ impl AsyncSessionStream for TcpStream {
assert!(expected_block_directions.is_readable());
assert!(expected_block_directions.is_writable());

self.ready(tokio::io::Interest::READABLE | tokio::io::Interest::WRITABLE)
.await?;
let mut n_retry = 0;
loop {
let ready = self
.ready(tokio::io::Interest::READABLE | tokio::io::Interest::WRITABLE)
.await?;
if ready.is_readable() {
let either = future::select(
Box::pin(self.writable()),
Box::pin(sleep(Duration::from_millis(1000))),
)
.await;
match either {
Either::Left((x, _)) => x?,
Either::Right(_) => {}
}
break;
} else if ready.is_writable() {
let either = future::select(
Box::pin(self.readable()),
Box::pin(sleep(Duration::from_millis(1000))),
)
.await;
match either {
Either::Left((x, _)) => x?,
Either::Right(_) => {}
}
break;
} else if ready.is_empty() {
n_retry += 1;
if n_retry > 3 {
break;
}
continue;
} else {
break;
}
}
}
}

Expand Down Expand Up @@ -146,8 +184,43 @@ impl AsyncSessionStream for UnixStream {
assert!(expected_block_directions.is_readable());
assert!(expected_block_directions.is_writable());

self.ready(tokio::io::Interest::READABLE | tokio::io::Interest::WRITABLE)
.await?;
let mut n_retry = 0;
loop {
let ready = self
.ready(tokio::io::Interest::READABLE | tokio::io::Interest::WRITABLE)
.await?;
if ready.is_readable() {
let either = future::select(
Box::pin(self.writable()),
Box::pin(sleep(Duration::from_millis(1000))),
)
.await;
match either {
Either::Left((x, _)) => x?,
Either::Right(_) => {}
}
break;
} else if ready.is_writable() {
let either = future::select(
Box::pin(self.readable()),
Box::pin(sleep(Duration::from_millis(1000))),
)
.await;
match either {
Either::Left((x, _)) => x?,
Either::Right(_) => {}
}
break;
} else if ready.is_empty() {
n_retry += 1;
if n_retry > 3 {
break;
}
continue;
} else {
break;
}
}
}
}

Expand Down