Skip to content

[FEAT] - API for canceling prefetching #2454

Open

Description

@mxmlnkn

Is this feature for paramiko acting as a client or a server?

Client

What functionality does this feature request relate to?

SFTP

For client-side features, does this relate to a specific type of SSH server?

No response

If you're using paramiko as part of another tool, which tool/version?

No response

Desired behavior

Let's say I have a 100 GB file on the server and I am only interested in the first 10 GB. A simple SFTPClient.open().read(10 << 30) is excruciatingly slow, so I call SFTPFile.prefetch. However, when I now stop reading after the desired amount, the prefetch thread keeps going and my Python program does not quit / shows high CPU and network load for the next minutes until the rest of the huge file has been downloaded.

Example code:

import io
import sys
import time
import paramiko

client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect("127.0.0.1")

sftp = client.open_sftp()


remotepath = "wikidata-20220103-all.json.gz"
file_size = sftp.stat(remotepath).st_size
with sftp.open(remotepath, "rb", bufsize=255 * 1024) as reader:
    reader.prefetch(file_size)
    size = 0
    threshold = 1 * 1024 * 1024
    t0 = time.time()
    while True:
        data = reader.read(255 * 1024)
        size += len(data)

        if size >= threshold:
            t1 = time.time()
            if t1 - t0 > 20:
                reader._prefetch_lock = None
                sys.exit(1)
            print(f"Read {size / 1e6} MB in {t1-t0:.2f} s -> {size/(t1-t0)/1e6:.2f} MB/s")
            threshold += 1 * 1024 * 1024

        if len(data) == 0:
            break

I think, at the very least, SFTPFile should cancel and join all prefetching on __exit__ / __del__ to avoid what could be described as "hangs" when trying to quit early.

A really dirty hack, which I found to work for my use case of tracing with the Score-P python bindings, is to crash the prefetch thread by overwriting the _prefetch_lock, i.e.,:

            if t1 - t0 > 20:
                reader._prefetch_lock = None
                sys.exit(1)

The error message produced by this is not pretty though:

Exception in thread Thread-3 (_prefetch_thread):
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/home/user/.local/lib/python3.10/site-packages/paramiko/sftp_file.py", line 564, in _prefetch_thread
    with self._prefetch_lock:
AttributeError: __enter__
Traceback (most recent call last):
  File "benchmark-ssh-backends.py", line 30, in <module>
    sys.exit(1)
SystemExit: 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/user/.local/lib/python3.10/site-packages/scorep/__main__.py", line 142, in <module>
    scorep_main()
  File "/home/user/.local/lib/python3.10/site-packages/scorep/__main__.py", line 119, in scorep_main
    tracer.run(code, globs, globs)
  File "/home/user/.local/lib/python3.10/site-packages/scorep/_instrumenters/scorep_instrumenter.py", line 55, in run
    exec(cmd, globals, locals)
  File "benchmark-ssh-backends.py", line 17, in <module>
    with sftp.open(remotepath, "rb", bufsize=255 * 1024) as reader:
  File "/home/user/.local/lib/python3.10/site-packages/paramiko/util.py", line 290, in __exit__
    self.close()
  File "/home/user/.local/lib/python3.10/site-packages/paramiko/sftp_file.py", line 82, in close
    self._close(async_=False)
  File "/home/user/.local/lib/python3.10/site-packages/paramiko/sftp_file.py", line 104, in _close
    self.sftp._request(CMD_CLOSE, self.handle)
  File "/home/user/.local/lib/python3.10/site-packages/paramiko/sftp_client.py", line 857, in _request
    return self._read_response(num)
  File "/home/user/.local/lib/python3.10/site-packages/paramiko/sftp_client.py", line 915, in _read_response
    fileobj._async_response(t, msg, num)
  File "/home/user/.local/lib/python3.10/site-packages/paramiko/sftp_file.py", line 579, in _async_response
    with self._prefetch_lock:
AttributeError: __enter__

Anything else?

No response

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