pytest-sftpserver icon indicating copy to clipboard operation
pytest-sftpserver copied to clipboard

Tests hanging forever

Open AndreaCrotti opened this issue 9 years ago • 10 comments

Thanks for this great utility, I'm trying to get it working now with a very simple example:


def test_connect_to_sftp(sftpserver):
    with sftpserver.serve_content({'a_dir': {'somefile.txt': "Some content"}}):
        trans, client = sftp_utils.connect_to_sftp(
            host=sftpserver.host,
            port=sftpserver.port,
            username='user',
            password='pw',
            dsp_name='name',
        )

Where the connect is very simple and just does this

def connect_to_sftp(host, port, username, password, dsp_name):
    logger.info('Connecting to %s: %s:%s', dsp_name, host, port)
    transport = Transport((host, port))
    transport.connect(username=username, password=password)
    logger.info('Connected to %s', dsp_name)

    return transport, SFTPClient.from_transport(transport)

It works in theory but the test runner never quits it just hangs there forever. Any idea why it's happening? Is there something I can do to kill the sftpserver thread? (I tried shutdown already but nothing..)

AndreaCrotti avatar Jan 13 '16 14:01 AndreaCrotti

It looks like you're using the paramiko client? If so, then this is actually a paramiko issue. It opens Python threads in the background to do the actual communication with the server. So you need to actually close the client or it might get stuck.

mbyio avatar Apr 12 '17 07:04 mbyio

If this is indeed a paramiko issue I assume this issue can be closed?

Note to self: Maybe adding a hint in the readme wouldn't hurt though...

ulope avatar Mar 28 '18 20:03 ulope

I am having ths problem, if its a paramiko issue, is there any links ? What is the status of this.

alfredopalhares avatar Feb 01 '21 22:02 alfredopalhares

Also encountered this problem, I think sure this is the relevant paramiko issue: https://github.com/paramiko/paramiko/issues/520 (the issue is more widespread than the title). As above, explicitly closing the transport or client fixes it, e.g. from the orignal post adding trans.close() to the end of the test. I think the issue can be closed? I can't see any way of dealing with this in the fixture without monkey patching paramiko.

christopherdoyle avatar May 24 '21 15:05 christopherdoyle

Similar problem here. Has anyone figured out how to gracefully kill the sftpserver at the end of a test?

matteosantama avatar Sep 17 '21 01:09 matteosantama

Nope, still nothing on my end. Shame, because this library is not that useful withthis bug.

alfredopalhares avatar Sep 17 '21 14:09 alfredopalhares

Yeah, this happened to me too.

As above, explicitly closing the transport or client fixes it, e.g. from the orignal post adding trans.close() to the end of the test.

Not for me. :\

fiendish avatar Jun 19 '22 06:06 fiendish

@ulope running the server as a thread is probably a mistake. I was able to work around this issue by spawning the server in its own process to eliminate lock contention:

import time
from multiprocessing import Process, Queue
from pytest_sftpserver.sftp.server import SFTPServer


def run_server(q, contents):
    sftpserver = SFTPServer(content_object=contents)
    sftpserver.start()
    q.put((sftpserver.host, sftpserver.port))


def test_some_stuff():
    contents = {}  # my contents structure here

    queue = Queue()
    p = Process(target=run_server, args=(queue, contents), daemon=True)
    p.start()

    for _ in range(10):
        address = queue.get()
        if address:
            break
        time.sleep(0.5)

    assert address

    # <do my testing here>

fiendish avatar Jun 19 '22 16:06 fiendish

To anyone coming here: This might be an alternative: https://github.com/oz123/pytest-localftpserver

edit: ftp only :-(

johannes-cogitaris avatar Dec 09 '22 07:12 johannes-cogitaris

see : https://github.com/ulope/pytest-sftpserver/issues/30#issuecomment-1530896213 It worked for me:

@pytest.fixture
def sftp_fixture(sftpserver):
    # https://github.com/ulope/pytest-sftpserver/issues/30
    # Tests hanging forever
    sftpserver.daemon_threads = True
    sftpserver.block_on_close = False
    yield sftpserver

tmieulet avatar May 24 '23 10:05 tmieulet