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
- Stop transport threads on login failure, possible solution for #72 — committed to pahaz/sshtunnel by fernandezcuesta 8 years ago
- Stop transport threads on login failure, possible solution for #72 — committed to pahaz/sshtunnel by fernandezcuesta 8 years ago
I was able to close the the connection after setting daemon_forward_servers = True
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 usingatexitbut it didn’t work for me. I also tried settingsshtunnel.DAEMON=Truebut 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