How to make SSHLibrary not call logger.log_background_messages() on close unless in main thread?

Hi,

I am in my keyword running some parallel tasks using concurrent.futures and using robotbackgroundlogger to log the messages. After all threads are finished I run logger.log_background_messages() in the main thread since it only allows running it in main thread.

The problem is that SSHLibrary() automatically tries to call logger.log_background_messages() in it’s close function.

From the source code I can see the following snippet

def close(self):
    """Closes the connection."""
    if self.tunnel:
        self.tunnel.close()
    self._sftp_client = None
    self._scp_transfer_client = None
    self._scp_all_client = None
    self._shell = None
    self.client.close()
    try:
        logger.log_background_messages()
    except AttributeError:
        pass

Which means that closing any SSH connection in a thread would raise an error, since you are not allowed to run the function logger.log_background_messages() unless in the main thread.

How can I disable so that SSHLibrary does not try to run logger.log_background_messages when I close my SSH connection? Or some other workaround to make it work?

Hi Kas,

It looks to me from that code snippet, that the try block is missing 1 or more except blocks.

Since it’s in the code of SSH Library itself, it’s going to need a new version of that library, so I suggest you go to the SSH Library source (gthub?) and raise and issue for the close function to handle the exception you’re getting,

if you’re comfortable with python you can submit a PR too, which will help get the fix released quicker.

Dave.

3 Likes

I ended up with a solution where I store all the SSH connections to close in a global queue object and then close them when I am back in main thread basically. Thanks for input!

1 Like