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

Socket is closed raised on Connection.put() after Connection.close() Connection.open() called #1981

Closed
AlexanderBodnya opened this issue Jun 3, 2019 · 6 comments

Comments

@AlexanderBodnya
Copy link

Might be related to Connection.sftp():

If called more than one time, memorizes the first result;

Code to reproduce

import fabric


conn = fabric.Connection(host="{}@{}".format('root', ''), connect_kwargs={"password": ''})
conn.put('./1.txt')
conn.close()
conn.open()
conn.put('./1.txt')

Traceback:

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-6-64e29c4aa4e9> in <module>
----> 1 conn.put('./1.txt')

~/venv36/lib/python3.6/site-packages/fabric/connection.py in put(self, *args, **kwargs)
    765         .. versionadded:: 2.0
    766         """
--> 767         return Transfer(self).put(*args, **kwargs)
    768
    769     # TODO: yield the socket for advanced users? Other advanced use cases

~/venv36/lib/python3.6/site-packages/fabric/transfer.py in put(self, local, remote, preserve_mode)
    231         prejoined_remote = remote
    232         remote = posixpath.join(
--> 233             self.sftp.getcwd() or self.sftp.normalize("."), remote
    234         )
    235         if remote != prejoined_remote:

~/venv36/lib/python3.6/site-packages/paramiko/sftp_client.py in normalize(self, path)
    630         path = self._adjust_cwd(path)
    631         self._log(DEBUG, "normalize({!r})".format(path))
--> 632         t, msg = self._request(CMD_REALPATH, path)
    633         if t != CMD_NAME:
    634             raise SFTPError("Expected name response")

~/venv36/lib/python3.6/site-packages/paramiko/sftp_client.py in _request(self, t, *arg)
    810
    811     def _request(self, t, *arg):
--> 812         num = self._async_request(type(None), t, *arg)
    813         return self._read_response(num)
    814

~/venv36/lib/python3.6/site-packages/paramiko/sftp_client.py in _async_request(self, fileobj, t, *arg)
    835         finally:
    836             self._lock.release()
--> 837         self._send_packet(t, msg)
    838         return num
    839

~/venv36/lib/python3.6/site-packages/paramiko/sftp.py in _send_packet(self, t, packet)
    196         if self.ultra_debug:
    197             self._log(DEBUG, util.format_binary(out, "OUT: "))
--> 198         self._write_all(out)
    199
    200     def _read_packet(self):

~/venv36/lib/python3.6/site-packages/paramiko/sftp.py in _write_all(self, out)
    160     def _write_all(self, out):
    161         while len(out) > 0:
--> 162             n = self.sock.send(out)
    163             if n <= 0:
    164                 raise EOFError()

~/venv36/lib/python3.6/site-packages/paramiko/channel.py in send(self, s)
    799         m.add_byte(cMSG_CHANNEL_DATA)
    800         m.add_int(self.remote_chanid)
--> 801         return self._send(s, m)
    802
    803     def send_stderr(self, s):

~/venv36/lib/python3.6/site-packages/paramiko/channel.py in _send(self, s, m)
   1178                 # this doesn't seem useful, but it is the documented behavior
   1179                 # of Socket
-> 1180                 raise socket.error("Socket is closed")
   1181             size = self._wait_for_send_window(size)
   1182             if size == 0:

OSError: Socket is closed
@AlexanderBodnya
Copy link
Author

AlexanderBodnya commented Jun 3, 2019

Definitely is related to the Connection.sftp()

adding following code fixes the issue:

import fabric


conn = fabric.Connection(host="{}@{}".format('root', ''), connect_kwargs={"password": ''})
conn.put('./1.txt')
conn.close()
conn._sftp = None
conn.open()
conn.put('./1.txt')

I understand the reason in current implementation, but I think it should have at least an explicit method for sftp client connection closing.

EDIT: Or Connection.close() should do _sftp = None

@GMaxera
Copy link

GMaxera commented Nov 17, 2021

I'm being hit by this bug.
Is there any chance that will be fixed soon?

@andrew-lee-1089
Copy link

I'm hitting this too - on a box with OpenSSHD V8.8.

davidjmemmett added a commit to davidjmemmett/fabric that referenced this issue Feb 15, 2022
davidjmemmett added a commit to davidjmemmett/fabric that referenced this issue Feb 15, 2022
davidjmemmett added a commit to davidjmemmett/fabric that referenced this issue Feb 15, 2022
@albundy83
Copy link

Hello,

any chance to have the fix merge ?
Here my way when using groups:

with ThreadingGroup.from_connections(cclfabric_list) as g:
    try:
        # This one to fix
        # https://github.com/fabric/fabric/issues/1981
        for conn in g:
            conn._sftp = None
            conn.open()
        results = g.put(file_to_send, file_to_send)
        for r in results:
            print(f'Succeeded {r.host}: send file')
        g.close()
        os.remove(file_to_send)
        return True
    except GroupException as e:
        results = e.result
        # Print results
        for rf in results.failed:
            conn = results.failed[rf]
            print(f'Some failed {rf.host}: {conn}')
        for rs in results.succeeded:
            print(f'Some succeeded {rs.host}: send file')
        g.close()
        os.remove(file_to_send)
        return False

@andrew-lee-1089
Copy link

I've manually tested @davidjmemmett's method and it fixes the issue for me.

@bitprophet
Copy link
Member

Merging the related PR for 3.0.x line (out soon!)

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