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

Hangs with Python 3.7 #138

Closed
cjltsod opened this issue Dec 11, 2018 · 31 comments
Closed

Hangs with Python 3.7 #138

cjltsod opened this issue Dec 11, 2018 · 31 comments
Labels
Milestone

Comments

@cjltsod
Copy link

cjltsod commented Dec 11, 2018

I'm using SSHTunnelForwarder with Python 3.7. After finishing my work, I run server.stop() and hangs.

I believe it caused by socketserver.ThreadingMixIn
https://docs.python.org/3.7/library/socketserver.html#socketserver.ThreadingMixIn

Changed in version 3.7: socketserver.ForkingMixIn.server_close() and socketserver.ThreadingMixIn.server_close() now waits until all child processes and non-daemonic threads complete. Add a new socketserver.ForkingMixIn.block_on_close class attribute to opt-in for the pre-3.7 behaviour.

Here's my work around. I changed the attribute server._server_list[0].block_on_close = False, and it behave like usual. It closed normally and not being hang.

I'm not sure if setting block_on_close = False is a good way or not.

Issue #135 may have the same cause.

@cjltsod
Copy link
Author

cjltsod commented Dec 12, 2018

Here's what I work around. cjltsod@2e56a5b
Still not sure if it's a good way or not.

@fernandezcuesta
Copy link
Collaborator

Hi, thanks for the valuable information.
If I understand properly, when daemon_threads is True and server_close() is executed it will not wait for the threads created by ThreadingMixIn to be closed, which is the same as block_on_close = False.
My guess is that this parameter is more focused on unifying both ForkingMixIn and ThreadingMixIn behaviour rather than changing how things worked until now.
Did you try setting DAEMON = True or just daemon_threads = True? Do you obtain the same results?

@cjltsod
Copy link
Author

cjltsod commented Dec 12, 2018

Yes, setting DAEMON = True solved my problem in Python 3.7 too.
What situation should I set DAEMON = True manually?

@fernandezcuesta
Copy link
Collaborator

Well maybe it's much easier just setting tunnel.daemon_forward_servers = True before starting the tunnel rather than patching the constant.

@cjltsod
Copy link
Author

cjltsod commented Dec 12, 2018

Thanks a lot for helping. It solved the issues. cjltsod@13c0f2e

@alejoar
Copy link

alejoar commented May 5, 2019

So are we getting a release with this fix or not? sshtunnel currently doesn't work properly in Python 3.7+

@rkkautsar
Copy link

Still hangs in 3.7+.
I tried setting daemon_forward_servers = True before starting the forwarder but it still doesn't work..

@cjltsod
Copy link
Author

cjltsod commented Jun 12, 2019

It has been patched with v0.1.5 at fa85471 (#149) which is not on pypi yet.
It has been merged into master branch, so you can simply install through GitHub by using
pip install git+https://github.com/pahaz/sshtunnel.git

@fernandezcuesta
Copy link
Collaborator

updated PYPI

@rkkautsar
Copy link

rkkautsar commented Jun 18, 2019

Any plan to also update the conda-forge package?
Related: conda-forge/sshtunnel-feedstock#10

@eannam
Copy link

eannam commented Dec 11, 2019

Hi, I am still getting this issue with python 3.7 using v0.1.5.

@pyMan
Copy link

pyMan commented Jan 8, 2020

Hi, I am still getting this issue with python 3.8 using v0.1.5.

EDIT: specifically it happens to me only if I execute any kind of query through the ssh tunnel (I'm using django 2.2.9)

@PozziSan
Copy link

PozziSan commented Apr 27, 2020

Hi, I am still getting this issue with python 3.8 using v0.1.5.

EDIT: specifically it happens to me only if I execute any kind of query through the ssh tunnel (I'm using django 2.2.9)

Hi! I'm getting the same here, after executing queries with pandas, on a flask application using sqlAlchemy.
The application is running with Python 3.8 using v0.1.5.

@dem82
Copy link

dem82 commented Jun 12, 2020

I've solved the hanging problem with #72
Python 3.7 using v0.1.5

@erfannariman

This comment has been minimized.

@audciz

This comment has been minimized.

@tscott8706

This comment has been minimized.

@youngsoul

This comment has been minimized.

@tdstarkisoc

This comment has been minimized.

@youngsoul

This comment has been minimized.

@pahaz pahaz reopened this Nov 16, 2020
@pahaz pahaz added the bug label Nov 16, 2020
@tdstarkisoc

This comment has been minimized.

@pahaz
Copy link
Owner

pahaz commented Nov 16, 2020

I reopened this issue as I found a lot of new comments!

  1. sshtunnel is always waiting until closing all connections during the stop() called.
  2. If you don't want to wait, you can use .stop(force=True)
  3. You can have a problem with hangs if you don't call .stop. For example, you have some unhandled exception in MainThread and you don't call .stop in this case. As a dirty workaround, you can use this hack at the beginning:
# I don't recommend to use this hack!
# Hack threadings https://docs.python.org/3/library/threading.html#threading.Thread.daemon ...
sshtunnel.SSHTunnelForwarder.daemon_forward_servers = True
sshtunnel.SSHTunnelForwarder.daemon_transport = True
  1. It's better to use with open_tunnel(...) which always stops the tunnel.

Do anyone have an issue related to hangs with sshtunnel==0.3.1 and .stop(force=True) ?

@tdstarkisoc

This comment has been minimized.

@erfannariman

This comment has been minimized.

@pahaz

This comment has been minimized.

@tdstarkisoc

This comment has been minimized.

@tdstarkisoc

This comment has been minimized.

@pahaz

This comment has been minimized.

@pahaz

This comment has been minimized.

@tdstarkisoc

This comment has been minimized.

@pahaz
Copy link
Owner

pahaz commented Nov 18, 2020

@tdstarkisoc thanks you for feedback and tests.

I'll close the issue. And if anyone have an issue related to hangs with sshtunnel==0.3.1 and .stop(force=True) please open a new issue. And read this first: #138 (comment)

And also we have a plan to change the default threading daemon option for sshtunnel threads.
But we needed more tests for those cases.

UPD (2021): we released 0.4.0 and changed the default threading daemon option for sshtunnel threads with functional tests.

@pahaz pahaz closed this as completed Nov 18, 2020
@pahaz pahaz added this to the 0.3.0 milestone Nov 18, 2020
Repository owner locked as resolved and limited conversation to collaborators Jan 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests