From ce96160f1ab3f6f3ab8b9fe69ef3e444f40b4eb6 Mon Sep 17 00:00:00 2001 From: AndyPC <59114493+APCBoston@users.noreply.github.com> Date: Mon, 9 Dec 2024 15:16:56 -0500 Subject: [PATCH] More Fun with Timeouts (#46) Fixes timeouts in transport.open_session() to account for paramiko's behavior of always opening the channel in blocking mode, only applying the timeout kwarg to the initial connection. --- netconf_client/connect.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/netconf_client/connect.py b/netconf_client/connect.py index 726a520..e5c8fa6 100644 --- a/netconf_client/connect.py +++ b/netconf_client/connect.py @@ -60,7 +60,10 @@ def connect_ssh( hostkey = _try_load_hostkey_b64(hostkey_b64) if hostkey_b64 else None transport.connect(username=username, password=password, pkey=pkey) try: - channel = transport.open_session(timeout=general_timeout) + # Paramiko always opens the channel in blocking mode, even when a timeout is specified. See https://github.com/paramiko/paramiko/blob/23f92003898b060df0e2b8b1d889455264e63a3e/paramiko/channel.py#L612-L633 + # This means that even if the Transport is holding a non-blocking socket, and the channel is created with a timeout, a channel.read() call can still hang if the remote misbehaves. + channel = transport.open_session(timeout=initial_timeout) + channel.settimeout(general_timeout) except Exception: transport.close() raise