You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SFTPClient's getfo hangs on a particular server #1788
Tried downloading a small (500K) file from a remote server using getfo and it just hanged.
The function getfo uses a prefetch mechanism which:
Virtually dissects the file to N equally-sized chunks.
Sends an asynchronous SSH_FXP_READ request for every chunk.
Waits for a SSH_FXP_DATA response for every request and assembles the file buffer locally
It appears that the server only sends a response for the first chunk or the first two chunks and then stops, leaving the client hanging on "receive_response".
I compared with WinSCP which manages to download files from that server and saw that WinSCP basically does the same but on every response sends a single additional SSH_FXP_READ (for an offset beyond EOF). Mimicking this behavior works.
The fact that WinSCP handles this case makes me believe that it is not a one-off weird implementation of this particular server but a known issue.
In our case we just gave up on getfo and used the open -> shutil.copyfileobj workaround which is considerably slower but works.
I had a similar issue where the prefetch was triggering hundreds of requests at once, and then waiting for responses. I think the volume of requests triggered some sort of DOS protection mechanism.
I solved it by modifying the library locally.
I added a semaphore to the sftp_file.py.
diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py
index 6c2a1336..0d7f643b 100644
--- a/paramiko/sftp_file.py+++ b/paramiko/sftp_file.py@@ -69,6 +69,7 @@ class SFTPFile(BufferedFile):
self._prefetch_data = {}
self._prefetch_extents = {}
self._prefetch_lock = threading.Lock()
+ self._prefetch_semaphore = threading.Semaphore(100)
self._saved_exception = None
self._reqs = deque()
@@ -534,6 +535,7 @@ class SFTPFile(BufferedFile):
# do these read requests in a temporary thread because there may be
# a lot of them, so it may block.
for offset, length in chunks:
+ self._prefetch_semaphore.acquire()
num = self.sftp._async_request(
self, CMD_READ, self.handle, long(offset), int(length)
)
@@ -558,6 +560,7 @@ class SFTPFile(BufferedFile):
offset, length = self._prefetch_extents[num]
self._prefetch_data[offset] = data
del self._prefetch_extents[num]
+ self._prefetch_semaphore.release()
if len(self._prefetch_extents) == 0:
self._prefetch_done = True
break
I also got into this bug, using Twisted as the SFTP server.
By now, there is get(source, destination, max_concurrent_prefetch_requests=INTEGER)
If a transfer is made for a large file with max_concurrent_prefetch_requests=None the transfer hangs as the MSG_CHANNEL_WINDOW_ADJUST messages are not sent to the server.
Activity
LucaSoato commentedon Apr 28, 2022
Same issue here, has anyone found a solution or any workaround different from using the
openmethod?Neob91 commentedon Jul 12, 2022
I had a similar issue where the prefetch was triggering hundreds of requests at once, and then waiting for responses. I think the volume of requests triggered some sort of DOS protection mechanism.
I solved it by modifying the library locally.
I added a semaphore to the
sftp_file.py.Neob91 commentedon Jul 12, 2022
@bitprophet Any chance we could add something like this for (optionally) rate-limiting requests? Any thoughts on this?
bskinn commentedon Jul 12, 2022
@Neob91 Your proposal makes sense to me; it'll be bitprophet's call, though.
That said, it seems tangential to the original issue. Could you repost this as a new feature request? Thanks!
adiroiban commentedon Apr 29, 2024
I also got into this bug, using Twisted as the SFTP server.
By now, there is
get(source, destination, max_concurrent_prefetch_requests=INTEGER)If a transfer is made for a large file with
max_concurrent_prefetch_requests=Nonethe transfer hangs as theMSG_CHANNEL_WINDOW_ADJUSTmessages are not sent to the server.The paramiko client code is.
max_concurrent_prefetch_requests=90works.If I use
max_concurrent_prefetch_requests=91the transfer stalls as theMSG_CHANNEL_WINDOW_ADJUSTmessage is not sent