@@ -227,9 +227,13 @@ def open_session(self):
227
227
"""Open new channel from session"""
228
228
chan = self .session .open_session ()
229
229
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 :
231
232
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 )
233
237
errno = self .session .last_errno ()
234
238
if chan is None and errno != LIBSSH2_ERROR_EAGAIN :
235
239
raise SessionError (errno )
@@ -269,6 +273,14 @@ def read_output(self, channel, timeout=None):
269
273
"""
270
274
return _read_output (self .session , channel .read , timeout = timeout )
271
275
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
+
272
284
def wait_finished (self , channel , timeout = None ):
273
285
"""Wait for EOF from channel, close channel and wait for
274
286
close acknowledgement.
@@ -284,19 +296,13 @@ def wait_finished(self, channel, timeout=None):
284
296
# If .eof() returns EAGAIN after a select with a timeout, it means
285
297
# it reached timeout without EOF and the channel should not be
286
298
# 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 )
293
300
# Close channel to indicate no more commands will be sent over it
294
301
self .close_channel (channel )
295
302
296
303
def close_channel (self , channel ):
297
304
logger .debug ("Closing channel" )
298
305
self ._eagain (channel .close )
299
- self ._eagain (channel .wait_closed )
300
306
301
307
def _eagain (self , func , * args , ** kwargs ):
302
308
ret = func (* args , ** kwargs )
@@ -373,11 +379,18 @@ def run_command(self, command, sudo=False, user=None,
373
379
374
380
def _make_sftp (self ):
375
381
"""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 )
377
386
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 :
379
389
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 )
381
394
errno = self .session .last_errno ()
382
395
if sftp is None and errno != LIBSSH2_ERROR_EAGAIN :
383
396
raise SFTPError ("Error initialising SFTP - error code %s" ,
@@ -572,11 +585,18 @@ def _sftp_readdir(self, dir_h):
572
585
yield line
573
586
574
587
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 )
576
592
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 :
578
595
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 )
580
600
errno = self .session .last_errno ()
581
601
if fh is None and errno != LIBSSH2_ERROR_EAGAIN :
582
602
msg = "Error opening file handle for file %s - error no: %s"
0 commit comments