Skip to content

SFTPClient's getfo hangs on a particular server #1788

Open

Description

Tried downloading a small (500K) file from a remote server using getfo and it just hanged.
The function getfo uses a prefetch mechanism which:

  1. Virtually dissects the file to N equally-sized chunks.
  2. Sends an asynchronous SSH_FXP_READ request for every chunk.
  3. 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.

  1. Is anyone aware of the issue above?
  2. Should I issue a PR to fix this issue?

Activity

  1. LucaSoato commented on Apr 28, 2022

    @LucaSoato

    Same issue here, has anyone found a solution or any workaround different from using the open method?

  2. Neob91 commented on Jul 12, 2022

    @Neob91

    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
  3. Neob91 commented on Jul 12, 2022

    @Neob91

    @bitprophet Any chance we could add something like this for (optionally) rate-limiting requests? Any thoughts on this?

  4. bskinn commented on Jul 12, 2022

    @bskinn
    Contributor

    @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!

  5. adiroiban commented on Apr 29, 2024

    @adiroiban

    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.

    The paramiko client code is.

    import paramiko
    
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.connect(
        'localhost',
        port=5022,
        username='user',
        password='pass',
        )
    
    sftp = paramiko.SFTPClient.from_transport(client.get_transport())
    
    sftp.get(
        '/ubuntu-23.10.1-desktop-amd64.iso',
        '/tmp/local.iso',
        max_concurrent_prefetch_requests=90,
        )
    sftp.close()
    client.close()

    max_concurrent_prefetch_requests=90 works.

    If I use max_concurrent_prefetch_requests=91 the transfer stalls as the MSG_CHANNEL_WINDOW_ADJUST message is not sent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions