Repository navigation
Description
Are you using paramiko as a client or server?
Client
What feature(s) aren't working right?
SFTP
What version(s) of paramiko are you using?
3.4.0
What version(s) of Python are you using?
3.10.12
What operating system and version are you using?
Ubuntu 22.04
If you're connecting as a client, which SSH server are you connecting to?
No response
If you're using paramiko as part of another tool, which tool/version?
No response
Expected/desired behavior
On prefetching failure, Paramiko should manually request the desired file chunk and continue.
Actual behavior
Paramiko disables all future prefetching before returning "None" in "_read_prefetch" on failure.
This horrifically reduces download speeds for the rest of the file transfer, especially if the file is exceptionally large.
```
def _read_prefetch(self, size):
"""
read data out of the prefetch buffer, if possible. if the data isn't
in the buffer, return None. otherwise, behaves like a normal read.
"""
# while not closed, and haven't fetched past the current position,
# and haven't reached EOF...
while True:
offset = self._data_in_prefetch_buffers(self._realpos)
if offset is not None:
break
if self._prefetch_done or self._closed:
break
self.sftp._read_response()
self._check_exception()
if offset is None:
self._prefetching = False
return None
prefetch = self._prefetch_data[offset]
del self._prefetch_data[offset]
buf_offset = self._realpos - offset
if buf_offset > 0:
self._prefetch_data[offset] = prefetch[:buf_offset]
prefetch = prefetch[buf_offset:]
if size < len(prefetch):
self._prefetch_data[self._realpos + size] = prefetch[size:]
prefetch = prefetch[:size]
return prefetch
```
How to reproduce
Set up an SFTP server/client configuration.
Create a large file on the server head -c 10G </dev/urandom >myfile
Create a Paramiko SFTP Client and attempt to download the file. Eventually (~1Gb in seems to be most common for an Ethernet configuration between my two machines), a Paramiko prefetch request will fail and cripple the download rate of the remaining file.
path = 'myfile'
remSize = sftp.lstat(path).st_size
remFile = sftp.open(path, 'r', paramiko.SFTPFile.MAX_REQUEST_SIZE)
remFile.prefetch(remSize, 64)
bytesRead = 0
with open('localfile', 'wb') as localFile:
while True:
chunk = remFile.read(paramiko.SFTPFile.MAX_REQUEST_SIZE)
if chunk == b'':
break
localFile.write(chunk)
print(str(bytesRead))
Anything else?
I've attempted to remove the self._prefetching = False from my test environment and it doesn't appear to impact file integrity as far as I can tell.
Would appreciate some insight if this is done for a particular reason.
Activity