@@ -5,7 +5,10 @@ use core::{
55use std:: io:: { Error as IoError , ErrorKind as IoErrorKind } ;
66
77use async_trait:: async_trait;
8- use futures_util:: ready;
8+ use futures_util:: {
9+ future:: { self , Either } ,
10+ ready,
11+ } ;
912use ssh2:: { BlockDirections , Error as Ssh2Error , Session } ;
1013use tokio:: net:: TcpStream ;
1114#[ cfg( unix) ]
@@ -50,8 +53,43 @@ impl AsyncSessionStream for TcpStream {
5053 assert ! ( expected_block_directions. is_readable( ) ) ;
5154 assert ! ( expected_block_directions. is_writable( ) ) ;
5255
53- self . ready ( tokio:: io:: Interest :: READABLE | tokio:: io:: Interest :: WRITABLE )
54- . await ?;
56+ let mut n_retry = 0 ;
57+ loop {
58+ let ready = self
59+ . ready ( tokio:: io:: Interest :: READABLE | tokio:: io:: Interest :: WRITABLE )
60+ . await ?;
61+ if ready. is_readable ( ) {
62+ let either = future:: select (
63+ Box :: pin ( self . writable ( ) ) ,
64+ Box :: pin ( sleep ( Duration :: from_millis ( 1000 ) ) ) ,
65+ )
66+ . await ;
67+ match either {
68+ Either :: Left ( ( x, _) ) => x?,
69+ Either :: Right ( _) => { }
70+ }
71+ break ;
72+ } else if ready. is_writable ( ) {
73+ let either = future:: select (
74+ Box :: pin ( self . readable ( ) ) ,
75+ Box :: pin ( sleep ( Duration :: from_millis ( 1000 ) ) ) ,
76+ )
77+ . await ;
78+ match either {
79+ Either :: Left ( ( x, _) ) => x?,
80+ Either :: Right ( _) => { }
81+ }
82+ break ;
83+ } else if ready. is_empty ( ) {
84+ n_retry += 1 ;
85+ if n_retry > 3 {
86+ break ;
87+ }
88+ continue ;
89+ } else {
90+ break ;
91+ }
92+ }
5593 }
5694 }
5795
@@ -146,8 +184,43 @@ impl AsyncSessionStream for UnixStream {
146184 assert ! ( expected_block_directions. is_readable( ) ) ;
147185 assert ! ( expected_block_directions. is_writable( ) ) ;
148186
149- self . ready ( tokio:: io:: Interest :: READABLE | tokio:: io:: Interest :: WRITABLE )
150- . await ?;
187+ let mut n_retry = 0 ;
188+ loop {
189+ let ready = self
190+ . ready ( tokio:: io:: Interest :: READABLE | tokio:: io:: Interest :: WRITABLE )
191+ . await ?;
192+ if ready. is_readable ( ) {
193+ let either = future:: select (
194+ Box :: pin ( self . writable ( ) ) ,
195+ Box :: pin ( sleep ( Duration :: from_millis ( 1000 ) ) ) ,
196+ )
197+ . await ;
198+ match either {
199+ Either :: Left ( ( x, _) ) => x?,
200+ Either :: Right ( _) => { }
201+ }
202+ break ;
203+ } else if ready. is_writable ( ) {
204+ let either = future:: select (
205+ Box :: pin ( self . readable ( ) ) ,
206+ Box :: pin ( sleep ( Duration :: from_millis ( 1000 ) ) ) ,
207+ )
208+ . await ;
209+ match either {
210+ Either :: Left ( ( x, _) ) => x?,
211+ Either :: Right ( _) => { }
212+ }
213+ break ;
214+ } else if ready. is_empty ( ) {
215+ n_retry += 1 ;
216+ if n_retry > 3 {
217+ break ;
218+ }
219+ continue ;
220+ } else {
221+ break ;
222+ }
223+ }
151224 }
152225 }
153226
0 commit comments