Skip to content

Commit 66c5175

Browse files
committed
Try fix block
1 parent e06ad0d commit 66c5175

File tree

2 files changed

+108
-8
lines changed

2 files changed

+108
-8
lines changed

async-ssh2-lite/src/session_stream/impl_async_io.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use std::io::{Error as IoError, ErrorKind as IoErrorKind};
66

77
use async_io::{Async, Timer};
88
use async_trait::async_trait;
9-
use futures_util::{future, pin_mut, ready};
9+
use futures_util::{
10+
future::{self, Either},
11+
pin_mut, ready,
12+
};
1013
use ssh2::{BlockDirections, Error as Ssh2Error, Session};
1114

1215
use super::{AsyncSessionStream, BlockDirectionsExt as _};
@@ -53,10 +56,34 @@ where
5356
assert!(expected_block_directions.is_readable());
5457
assert!(expected_block_directions.is_writable());
5558

56-
let (ret, _) = future::select(self.readable(), self.writable())
59+
let (ret, either) = future::select(self.readable(), self.writable())
5760
.await
5861
.factor_first();
59-
ret?
62+
ret?;
63+
match either {
64+
Either::Left(_) => {
65+
let either = future::select(
66+
self.writable(),
67+
Box::pin(sleep(Duration::from_millis(1000))),
68+
)
69+
.await;
70+
match either {
71+
Either::Left((x, _)) => x?,
72+
Either::Right(_) => {}
73+
}
74+
}
75+
Either::Right(_) => {
76+
let either = future::select(
77+
self.readable(),
78+
Box::pin(sleep(Duration::from_millis(1000))),
79+
)
80+
.await;
81+
match either {
82+
Either::Left((x, _)) => x?,
83+
Either::Right(_) => {}
84+
}
85+
}
86+
}
6087
}
6188
}
6289

async-ssh2-lite/src/session_stream/impl_tokio.rs

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use core::{
55
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
66

77
use async_trait::async_trait;
8-
use futures_util::ready;
8+
use futures_util::{
9+
future::{self, Either},
10+
ready,
11+
};
912
use ssh2::{BlockDirections, Error as Ssh2Error, Session};
1013
use tokio::net::TcpStream;
1114
#[cfg(unix)]
@@ -52,8 +55,43 @@ impl AsyncSessionStream for TcpStream {
5255
assert!(expected_block_directions.is_readable());
5356
assert!(expected_block_directions.is_writable());
5457

55-
self.ready(tokio::io::Interest::READABLE | tokio::io::Interest::WRITABLE)
56-
.await?;
58+
let mut n_retry = 0;
59+
loop {
60+
let ready = self
61+
.ready(tokio::io::Interest::READABLE | tokio::io::Interest::WRITABLE)
62+
.await?;
63+
if ready.is_readable() {
64+
let either = future::select(
65+
Box::pin(self.writable()),
66+
Box::pin(sleep(Duration::from_millis(1000))),
67+
)
68+
.await;
69+
match either {
70+
Either::Left((x, _)) => x?,
71+
Either::Right(_) => {}
72+
}
73+
break;
74+
} else if ready.is_writable() {
75+
let either = future::select(
76+
Box::pin(self.readable()),
77+
Box::pin(sleep(Duration::from_millis(1000))),
78+
)
79+
.await;
80+
match either {
81+
Either::Left((x, _)) => x?,
82+
Either::Right(_) => {}
83+
}
84+
break;
85+
} else if ready.is_empty() {
86+
n_retry += 1;
87+
if n_retry > 3 {
88+
break;
89+
}
90+
continue;
91+
} else {
92+
break;
93+
}
94+
}
5795
}
5896
}
5997

@@ -152,8 +190,43 @@ impl AsyncSessionStream for UnixStream {
152190
assert!(expected_block_directions.is_readable());
153191
assert!(expected_block_directions.is_writable());
154192

155-
self.ready(tokio::io::Interest::READABLE | tokio::io::Interest::WRITABLE)
156-
.await?;
193+
let mut n_retry = 0;
194+
loop {
195+
let ready = self
196+
.ready(tokio::io::Interest::READABLE | tokio::io::Interest::WRITABLE)
197+
.await?;
198+
if ready.is_readable() {
199+
let either = future::select(
200+
Box::pin(self.writable()),
201+
Box::pin(sleep(Duration::from_millis(1000))),
202+
)
203+
.await;
204+
match either {
205+
Either::Left((x, _)) => x?,
206+
Either::Right(_) => {}
207+
}
208+
break;
209+
} else if ready.is_writable() {
210+
let either = future::select(
211+
Box::pin(self.readable()),
212+
Box::pin(sleep(Duration::from_millis(1000))),
213+
)
214+
.await;
215+
match either {
216+
Either::Left((x, _)) => x?,
217+
Either::Right(_) => {}
218+
}
219+
break;
220+
} else if ready.is_empty() {
221+
n_retry += 1;
222+
if n_retry > 3 {
223+
break;
224+
}
225+
continue;
226+
} else {
227+
break;
228+
}
229+
}
157230
}
158231
}
159232

0 commit comments

Comments
 (0)