@@ -227,9 +227,13 @@ def open_session(self):
227227 """Open new channel from session"""
228228 chan = self .session .open_session ()
229229 errno = self .session .last_errno ()
230- while chan is None and errno == LIBSSH2_ERROR_EAGAIN :
230+ while (chan is None and errno == LIBSSH2_ERROR_EAGAIN ) \
231+ or chan == LIBSSH2_ERROR_EAGAIN :
231232 wait_select (self .session )
232- chan = self .session .open_session ()
233+ try :
234+ chan = self .session .open_session ()
235+ except Exception as ex :
236+ raise SessionError (ex )
233237 errno = self .session .last_errno ()
234238 if chan is None and errno != LIBSSH2_ERROR_EAGAIN :
235239 raise SessionError (errno )
@@ -269,6 +273,14 @@ def read_output(self, channel, timeout=None):
269273 """
270274 return _read_output (self .session , channel .read , timeout = timeout )
271275
276+ def _select_timeout (self , func , timeout ):
277+ ret = func ()
278+ while ret == LIBSSH2_ERROR_EAGAIN :
279+ wait_select (self .session , timeout = timeout )
280+ ret = func ()
281+ if ret == LIBSSH2_ERROR_EAGAIN and timeout is not None :
282+ raise Timeout
283+
272284 def wait_finished (self , channel , timeout = None ):
273285 """Wait for EOF from channel, close channel and wait for
274286 close acknowledgement.
@@ -284,19 +296,13 @@ def wait_finished(self, channel, timeout=None):
284296 # If .eof() returns EAGAIN after a select with a timeout, it means
285297 # it reached timeout without EOF and the channel should not be
286298 # closed as the command is still running.
287- ret = channel .wait_eof ()
288- while ret == LIBSSH2_ERROR_EAGAIN :
289- wait_select (self .session , timeout = timeout )
290- ret = channel .wait_eof ()
291- if ret == LIBSSH2_ERROR_EAGAIN and timeout is not None :
292- raise Timeout
299+ self ._select_timeout (channel .wait_eof , timeout )
293300 # Close channel to indicate no more commands will be sent over it
294301 self .close_channel (channel )
295302
296303 def close_channel (self , channel ):
297304 logger .debug ("Closing channel" )
298305 self ._eagain (channel .close )
299- self ._eagain (channel .wait_closed )
300306
301307 def _eagain (self , func , * args , ** kwargs ):
302308 ret = func (* args , ** kwargs )
@@ -373,11 +379,18 @@ def run_command(self, command, sudo=False, user=None,
373379
374380 def _make_sftp (self ):
375381 """Make SFTP client from open transport"""
376- sftp = self .session .sftp_init ()
382+ try :
383+ sftp = self .session .sftp_init ()
384+ except Exception as ex :
385+ raise SFTPError (ex )
377386 errno = self .session .last_errno ()
378- while sftp is None and errno == LIBSSH2_ERROR_EAGAIN :
387+ while (sftp is None and errno == LIBSSH2_ERROR_EAGAIN ) \
388+ or sftp == LIBSSH2_ERROR_EAGAIN :
379389 wait_select (self .session )
380- sftp = self .session .sftp_init ()
390+ try :
391+ sftp = self .session .sftp_init ()
392+ except Exception as ex :
393+ raise SFTPError (ex )
381394 errno = self .session .last_errno ()
382395 if sftp is None and errno != LIBSSH2_ERROR_EAGAIN :
383396 raise SFTPError ("Error initialising SFTP - error code %s" ,
@@ -572,11 +585,18 @@ def _sftp_readdir(self, dir_h):
572585 yield line
573586
574587 def _sftp_openfh (self , open_func , remote_file , * args ):
575- fh = open_func (remote_file , * args )
588+ try :
589+ fh = open_func (remote_file , * args )
590+ except Exception as ex :
591+ raise SFTPError (ex )
576592 errno = self .session .last_errno ()
577- while fh is None and errno == LIBSSH2_ERROR_EAGAIN :
593+ while (fh is None and errno == LIBSSH2_ERROR_EAGAIN ) \
594+ or fh == LIBSSH2_ERROR_EAGAIN :
578595 wait_select (self .session , timeout = 0.1 )
579- fh = open_func (remote_file , * args )
596+ try :
597+ fh = open_func (remote_file , * args )
598+ except Exception as ex :
599+ raise SFTPError (ex )
580600 errno = self .session .last_errno ()
581601 if fh is None and errno != LIBSSH2_ERROR_EAGAIN :
582602 msg = "Error opening file handle for file %s - error no: %s"
0 commit comments