Skip to content

Commit

Permalink
Merge pull request #214 from pahaz/pahaz-patch-2
Browse files Browse the repository at this point in the history
Major update 2: fix blocking thread on Linux
  • Loading branch information
pahaz authored Nov 15, 2020
2 parents c88c809 + d40303a commit 856aa94
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
5 changes: 4 additions & 1 deletion changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ CHANGELOG

- v.0.3.0 (`Pahaz`_)
+ Change default with context behavior to use `.stop(force=True)` on exit (is not fully backward compatible)
+ Remove useless `daemon_forward_servers = True` hack for hangs prevention (is not fully backward compatible)
+ Set transport keepalive to 5 second by default (disabled for version < 0.3.0)
+ Set default transport timeout to 0.1
+ Deprecate and remove `block_on_close` option
+ Drop `daemon_forward_servers = True` hack for "deadlocks" / "tunneling hangs" (`#173`_, `#201`_, `#162`_)
+ Fix "deadlocks" / "tunneling hangs" (`#173`_, `#201`_, `#162`_, `#211`_)

- v.0.2.2 (`Pahaz`_)
+ Add `.stop(force=True)` for force close active connections (`#201`_)
Expand Down Expand Up @@ -161,4 +163,5 @@ CHANGELOG
.. _#162: https://github.com/pahaz/sshtunnel/issues/162
.. _#173: https://github.com/pahaz/sshtunnel/issues/173
.. _#201: https://github.com/pahaz/sshtunnel/issues/201
.. _#211: https://github.com/pahaz/sshtunnel/issues/211
.. _detail: https://github.com/pahaz/sshtunnel/commit/64af238b799b0e0057c4f9b386cda247e0006da9#diff-76bc1662a114401c2954deb92b740081R127
32 changes: 20 additions & 12 deletions sshtunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,24 @@
__author__ = 'pahaz'


DEFAULT_LOGLEVEL = logging.ERROR #: default level if no logger passed (ERROR)
TUNNEL_TIMEOUT = 1.0 #: Timeout (seconds) for tunnel connection
#: Timeout (seconds) for transport socket (``socket.settimeout``)
SSH_TIMEOUT = 0.1 # ``None`` may cause a block of transport thread
#: Timeout (seconds) for tunnel connection (open_channel timeout)
TUNNEL_TIMEOUT = 1.0

_DAEMON = False #: Use daemon threads in connections
TRACE_LEVEL = 1
_CONNECTION_COUNTER = 1
_LOCK = threading.Lock()
#: Timeout (seconds) for the connection to the SSH gateway, ``None`` to disable
SSH_TIMEOUT = None
DEPRECATIONS = {
_DEPRECATIONS = {
'ssh_address': 'ssh_address_or_host',
'ssh_host': 'ssh_address_or_host',
'ssh_private_key': 'ssh_pkey',
'raise_exception_if_any_forwarder_have_a_problem': 'mute_exceptions'
}

# logging
DEFAULT_LOGLEVEL = logging.ERROR #: default level if no logger passed (ERROR)
TRACE_LEVEL = 1
logging.addLevelName(TRACE_LEVEL, 'TRACE')

if os.name == 'posix':
Expand Down Expand Up @@ -1180,12 +1183,17 @@ def _get_transport(self):
_socket.settimeout(SSH_TIMEOUT)
_socket.connect((self.ssh_host, self.ssh_port))
transport = paramiko.Transport(_socket)
if isinstance(transport.sock, socket.socket):
transport.sock.settimeout(SSH_TIMEOUT)
sock = transport.sock
if isinstance(sock, socket.socket):
sock.settimeout(SSH_TIMEOUT)
transport.set_keepalive(self.set_keepalive)
transport.use_compression(compress=self.compression)
transport.daemon = self.daemon_transport

if isinstance(sock, socket.socket):
sock_timeout = sock.gettimeout()
sock_info = repr((sock.family, sock.type, sock.proto))
self.logger.debug('Transport socket info: {0}, timeout={1}'
.format(sock_info, sock_timeout))
return transport

def _create_tunnels(self):
Expand Down Expand Up @@ -1242,19 +1250,19 @@ def _process_deprecated(attrib, deprecated_attrib, kwargs):
"""
Processes optional deprecate arguments
"""
if deprecated_attrib not in DEPRECATIONS:
if deprecated_attrib not in _DEPRECATIONS:
raise ValueError('{0} not included in deprecations list'
.format(deprecated_attrib))
if deprecated_attrib in kwargs:
warnings.warn("'{0}' is DEPRECATED use '{1}' instead"
.format(deprecated_attrib,
DEPRECATIONS[deprecated_attrib]),
_DEPRECATIONS[deprecated_attrib]),
DeprecationWarning)
if attrib:
raise ValueError("You can't use both '{0}' and '{1}'. "
"Please only use one of them"
.format(deprecated_attrib,
DEPRECATIONS[deprecated_attrib]))
_DEPRECATIONS[deprecated_attrib]))
else:
return kwargs.pop(deprecated_attrib)
return attrib
Expand Down
4 changes: 2 additions & 2 deletions tests/test_forwarder.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ def test_deprecate_warnings_are_shown(self):
open_tunnel(**_kwargs)
logged_message = "'{0}' is DEPRECATED use '{1}' instead"\
.format(deprecated_arg,
sshtunnel.DEPRECATIONS[deprecated_arg])
sshtunnel._DEPRECATIONS[deprecated_arg])
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
self.assertEqual(logged_message, str(w[-1].message))

Expand All @@ -709,7 +709,7 @@ def test_deprecate_warnings_are_shown(self):
open_tunnel(**_kwargs)
logged_message = "'{0}' is DEPRECATED use '{1}' instead"\
.format(deprecated_arg,
sshtunnel.DEPRECATIONS[deprecated_arg])
sshtunnel._DEPRECATIONS[deprecated_arg])
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
self.assertEqual(logged_message, str(w[-1].message))

Expand Down

0 comments on commit 856aa94

Please sign in to comment.