sshtunnel: Script won't exit because paramiko thread is still running

The following script works and “FINISH” is printed, but it never exits to the shell:

import threading

from sshtunnel import SSHTunnelForwarder

with SSHTunnelForwarder(
    "localhost",
    ssh_username="localuser",
    ssh_password="localpass",
    remote_bind_address=('127.0.0.1', 5984),
    local_bind_address=('127.0.0.1', 9000)
) as server:

    print(server.local_bind_port)
    # these don't help
    # server.stop()
    # server.close()

# [t.close() for t in threading.enumerate() if t.__class__.__name__ == "Transport"]

print('FINISH!')

Python is waiting on threads to finish, but they never do. After running this script in iPython I can see the threads:

threading.enumerate()
[<_MainThread(MainThread, started 140735243972608)>,
 <HistorySavingThread(IPythonHistorySavingThread, started 123145307557888)>,
 <paramiko.Transport at 0x3880470 (cipher aes128-ctr, 128 bits) (connected; awaiting auth)>,
 <paramiko.Transport at 0x3874630 (cipher aes128-ctr, 128 bits) (connected; awaiting auth)>,
 <paramiko.Transport at 0x3191ba8 (cipher aes128-ctr, 128 bits) (connected; awaiting auth)>,
 <paramiko.Transport at 0x3874550 (cipher aes128-ctr, 128 bits) (active; 0 open channel(s))>,
 <paramiko.Transport at 0x3191c88 (cipher aes128-ctr, 128 bits) (connected; awaiting auth)>,
 <paramiko.Transport at 0x387bac8 (cipher aes128-ctr, 128 bits) (connected; awaiting auth)>,
 <paramiko.Transport at 0x3803e48 (cipher aes128-ctr, 128 bits) (connected; awaiting auth)>,
 <paramiko.Transport at 0x3874f60 (cipher aes128-ctr, 128 bits) (connected; awaiting auth)>]

If I uncomment the last line which explicitly closes the paramiko.Transport threads, the script exits correctly. It seems like I’m missing something, but even so this default behavior is confusing.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (1 by maintainers)

Commits related to this issue

Most upvoted comments

I was able to close the the connection after setting daemon_forward_servers = True

ssh_server = SSHTunnelForwarder(
        ssh_address_or_host= '98.123.89.123',
        ssh_username= 'name',
        ssh_pkey=pkey,
        remote_bind_address=(hostname, 1234))
ssh_server.daemon_forward_servers = True

with ssh_server as ssh:
        ssh.start()
        local_port = str(ssh.local_bind_port)
        conn = create_engine(f'mysql+pymysql://mysqldatabase:{local_port}')

I am using 0.1.3 and if I try using sshtunnel from an interactive prompt, then exiting with CTRL-D, my session never closes. But if I explicitly call stop() then CTRL-D my session exits immediately. I tried using atexit but it didn’t work for me. I also tried setting sshtunnel.DAEMON=True but that also didn’t help.

@stobra Thank you! When I set things up exactly this way they worked. Even a small deviation resulted in the hanging problem.

Worked for me too, thank you so much!

@stobra you sir are a legend!

@stobra Thank you! I’ve solved the hanging problem with your suggestion