Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to properly close (SSH) file systems? #1682

Open
mxmlnkn opened this issue Sep 19, 2024 · 0 comments
Open

How to properly close (SSH) file systems? #1682

mxmlnkn opened this issue Sep 19, 2024 · 0 comments

Comments

@mxmlnkn
Copy link

mxmlnkn commented Sep 19, 2024

I did open an SSH file system and used it successfully:

o = fsspec.open("ssh://user@127.0.0.1")
o.fs.listdir("/")

Then, I lost the SSH connection because I lost WLAN connectivity because I entered suspend mode on my notebook. After that, I got this error on o.fs.listdir:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/.local/lib/python3.12/site-packages/fsspec/spec.py", line 1593, in listdir
    return self.ls(path, detail=detail, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.12/site-packages/fsspec/implementations/sftp.py", line 127, in ls
    stats = [self._decode_stat(stat, path) for stat in self.ftp.listdir_iter(path)]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/paramiko/sftp_client.py", line 278, in listdir_iter
    t, msg = self._request(CMD_OPENDIR, path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/paramiko/sftp_client.py", line 821, in _request
    num = self._async_request(type(None), t, *arg)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/paramiko/sftp_client.py", line 846, in _async_request
    self._send_packet(t, msg)
  File "/usr/lib/python3/dist-packages/paramiko/sftp.py", line 198, in _send_packet
    self._write_all(out)
  File "/usr/lib/python3/dist-packages/paramiko/sftp.py", line 162, in _write_all
    n = self.sock.send(out)
        ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/paramiko/channel.py", line 801, in send
    return self._send(s, m)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/paramiko/channel.py", line 1198, in _send
    raise socket.error("Socket is closed")
OSError: Socket is closed

I was not able to get around this error without closing and restarting the Python interactive interpreter itself. Note that this error takes a while. Directly after loosing connectivity, or enabling VPN, the listdir call hangs multiple minutes before I loose patience. I tried unsuccessfully:

  • o.close()
  • o.fs.close() # no attribute "close"
  • Simply reopening the same SSH connection: fsspec.open("ssh://user@127.0.0.1").fs.listdir('/')
  • o.fs.client.close(). This fixes the hang, but now I get the Socket is closed error, which I also got after ~30 minutes (timeout?), with no way to reopen the same connection again.
  • quit ipython3 and reopen it: works fine

I am especially stumped as to why the last one did not work. It seems that SSH connections are somehow cached ad some layer.

I also looked into the paramiko API specification and found this:

close()

Close this SSHClient and its underlying Transport.

This should be called anytime you are done using the client object.

Warning

Paramiko registers garbage collection hooks that will try to automatically close connections for you, but this is not presently reliable. Failure to explicitly close your client after use may lead to end-of-process hangs!

The fsspec SFTP implementation never calls this close method on the Client. As far as I can see, the client is only opened, never closed.

I would expect all other remote file systems to have similar issues, i.e., how can I ensure that the connection is properly closed?

Edit: I noticed that there is some kind of caching going on. This would explain why simply trying to reopen the SSH mount did not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant