From 1809e08d3df432a5381c1e75b8652dc574396ed3 Mon Sep 17 00:00:00 2001 From: brendandburns <5751682+brendandburns@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:04:00 +0000 Subject: [PATCH 1/3] Initial plan From 70300d3211f66067c65386081d27a53cbf0e1844 Mon Sep 17 00:00:00 2001 From: brendandburns <5751682+brendandburns@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:09:42 +0000 Subject: [PATCH 2/3] test: reproduce idle WebSocket disconnect Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- kubernetes/stream/ws_client_test.py | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/kubernetes/stream/ws_client_test.py b/kubernetes/stream/ws_client_test.py index 7a31dea872..bcb9574b5d 100644 --- a/kubernetes/stream/ws_client_test.py +++ b/kubernetes/stream/ws_client_test.py @@ -547,6 +547,48 @@ def _make_client(self, mock_ws): return WSClient(self.config_mock, "wss://test", headers=None, capture_all=True, binary=True) + def test_update_pings_before_controlled_proxy_idle_timeout(self): + now = [0] + last_activity = [0] + idle_timeout = 3 + remote_process_running = True + + class ControlledProxyPoll: + def register(self, *_args): + pass + + def unregister(self, *_args): + pass + + def poll(self, timeout): + now[0] += timeout / 1000 + if now[0] - last_activity[0] >= idle_timeout: + mock_ws.connected = False + return [(10, select.POLLHUP)] + return [] + + mock_ws = MagicMock() + mock_ws.subprotocol = V4_CHANNEL_PROTOCOL + mock_ws.connected = True + mock_ws.sock.connected = True + mock_ws.sock.fileno.return_value = 10 + mock_ws.recv_data_frame.return_value = ( + websocket.ABNF.OPCODE_CLOSE, + MagicMock(), + ) + mock_ws.ping.side_effect = lambda *_args: last_activity.__setitem__(0, now[0]) + + client = self._make_client(mock_ws) + client.ping_interval = 1 + + with patch('select.poll', ControlledProxyPoll), \ + patch('time.monotonic', side_effect=lambda: now[0]): + client.update(timeout=5) + + self.assertTrue(remote_process_running) + self.assertTrue(client.is_open()) + self.assertGreater(mock_ws.ping.call_count, 0) + def test_update_reads_frames_pending_in_ssl_buffer(self): """Verify update reads a frame buffered inside the SSL socket even when the underlying socket does not report as readable. From ab19bc1e9d03c86db04f0c8d60b7a77aa086b108 Mon Sep 17 00:00:00 2001 From: brendandburns <5751682+brendandburns@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:15:38 +0000 Subject: [PATCH 3/3] fix: ping idle exec WebSockets (#20) Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- kubernetes/client/configuration.py | 16 +++-- kubernetes/stream/ws_client.py | 100 +++++++++++++++++++--------- kubernetes/stream/ws_client_test.py | 13 ++-- 3 files changed, 86 insertions(+), 43 deletions(-) diff --git a/kubernetes/client/configuration.py b/kubernetes/client/configuration.py index d3a0009cf0..4a5d5f7fe1 100644 --- a/kubernetes/client/configuration.py +++ b/kubernetes/client/configuration.py @@ -388,13 +388,15 @@ def __init__( """Options to pass down to the underlying urllib3 socket """ self.keep_alive = False - """Enable TCP keepalive on the underlying urllib3 sockets. - - Long lived requests such as watches are otherwise dropped - silently by an idle proxy or load balancer. When enabled, the - client asks the kernel for the same keepalive timings client-go - uses. Ignored if ``socket_options`` is set, which takes - precedence. + """Enable TCP keepalive on the underlying urllib3 sockets and periodic + WebSocket ping frames for synchronous exec streams. WebSocket pings + are sent every 30 seconds to help prevent idle intermediary timeouts. + They only help when proxies forward WebSocket control frames; servers + and proxies can still close idle streams. + + For TCP keepalive, the client asks the kernel for the same timings + client-go uses. ``socket_options`` takes precedence over those TCP + options. """ self.datetime_format = datetime_format diff --git a/kubernetes/stream/ws_client.py b/kubernetes/stream/ws_client.py index 0692ff180d..546e8f1fb3 100644 --- a/kubernetes/stream/ws_client.py +++ b/kubernetes/stream/ws_client.py @@ -39,6 +39,7 @@ V4_CHANNEL_PROTOCOL = "v4.channel.k8s.io" V5_CHANNEL_PROTOCOL = "v5.channel.k8s.io" +WEBSOCKET_PING_INTERVAL = 30 class _IgnoredIO: def write(self, _x): @@ -69,6 +70,16 @@ def __init__(self, configuration, url, headers, capture_all, binary=False): self._all = _IgnoredIO() self.sock = create_websocket(configuration, url, headers) self.subprotocol = getattr(self.sock, 'subprotocol', None) + self.ping_interval = ( + WEBSOCKET_PING_INTERVAL + if getattr(configuration, 'keep_alive', False) is True + else None + ) + self._next_ping = ( + time.monotonic() + self.ping_interval + if self.ping_interval is not None + else None + ) if not self.subprotocol and self.sock: headers_dict = self.sock.getheaders() if headers_dict: @@ -220,39 +231,66 @@ def update(self, timeout=0): self._connected = False return - # SSL sockets decrypt an entire TLS record at a time, so a previous - # recv_data_frame() call may have pulled the bytes of the next frames - # off the socket already: those frames sit decrypted in the - # SSLSocket's internal buffer, invisible to select()/poll() on the - # underlying socket. Without this check the buffered frames would - # only be delivered once new data arrives on the socket, and would be - # lost if it never does. - if (isinstance(self.sock.sock, ssl.SSLSocket) - and self.sock.sock.pending()): - r = True - # The options here are: - # select.select() - this will work on most OS, however, it has a - # limitation of only able to read fd numbers up to 1024. - # i.e. does not scale well. This was the original - # implementation. - # select.poll() - this will work on most unix based OS, but not as - # efficient as epoll. Will work for fd numbers above 1024. - # select.epoll() - newest and most efficient way of polling. - # However, only works on linux. - elif hasattr(select, "poll"): - poll = select.poll() - poll.register(self.sock.sock, select.POLLIN) + deadline = None + if self.ping_interval is not None: if timeout is not None and timeout != float("inf"): - timeout *= 1_000 # poll method uses milliseconds as the time unit + deadline = time.monotonic() + max(timeout, 0) + + while True: + wait_timeout = timeout + if self.ping_interval is not None: + now = time.monotonic() + wait_timeout = max(self._next_ping - now, 0) + if deadline is not None: + wait_timeout = min(wait_timeout, max(deadline - now, 0)) + + # SSL sockets decrypt an entire TLS record at a time, so a previous + # recv_data_frame() call may have pulled the bytes of the next frames + # off the socket already: those frames sit decrypted in the + # SSLSocket's internal buffer, invisible to select()/poll() on the + # underlying socket. Without this check the buffered frames would + # only be delivered once new data arrives on the socket, and would be + # lost if it never does. + if (isinstance(self.sock.sock, ssl.SSLSocket) + and self.sock.sock.pending()): + r = True + # The options here are: + # select.select() - this will work on most OS, however, it has a + # limitation of only able to read fd numbers up to 1024. + # i.e. does not scale well. This was the original + # implementation. + # select.poll() - this will work on most unix based OS, but not as + # efficient as epoll. Will work for fd numbers above 1024. + # select.epoll() - newest and most efficient way of polling. + # However, only works on linux. + elif hasattr(select, "poll"): + poll = select.poll() + poll.register(self.sock.sock, select.POLLIN) + if wait_timeout is not None and wait_timeout != float("inf"): + wait_timeout *= 1_000 # poll method uses milliseconds as the time unit + else: + wait_timeout = None + r = poll.poll(wait_timeout) + poll.unregister(self.sock.sock) else: - timeout = None - r = poll.poll(timeout) - poll.unregister(self.sock.sock) - else: - if timeout == float("inf"): - timeout = None - r, _, _ = select.select( - (self.sock.sock, ), (), (), timeout) + if wait_timeout == float("inf"): + wait_timeout = None + r, _, _ = select.select( + (self.sock.sock, ), (), (), wait_timeout) + + if r: + break + + if self.ping_interval is None: + return + + now = time.monotonic() + if now >= self._next_ping: + self.sock.ping() + self._next_ping = now + self.ping_interval + + if deadline is not None and now >= deadline: + return while r: op_code, frame = self.sock.recv_data_frame(True) diff --git a/kubernetes/stream/ws_client_test.py b/kubernetes/stream/ws_client_test.py index bcb9574b5d..a29219f45c 100644 --- a/kubernetes/stream/ws_client_test.py +++ b/kubernetes/stream/ws_client_test.py @@ -540,6 +540,7 @@ def setUp(self): self.config_mock.cert_file = None self.config_mock.key_file = None self.config_mock.verify_ssl = True + self.config_mock.keep_alive = False def _make_client(self, mock_ws): with patch.object(ws_client_module, 'create_websocket') as mock_create: @@ -576,13 +577,15 @@ def poll(self, timeout): websocket.ABNF.OPCODE_CLOSE, MagicMock(), ) - mock_ws.ping.side_effect = lambda *_args: last_activity.__setitem__(0, now[0]) - - client = self._make_client(mock_ws) - client.ping_interval = 1 + mock_ws.ping.side_effect = ( + lambda *_args: last_activity.__setitem__(0, now[0]) + ) + self.config_mock.keep_alive = True with patch('select.poll', ControlledProxyPoll), \ - patch('time.monotonic', side_effect=lambda: now[0]): + patch('time.monotonic', side_effect=lambda: now[0]), \ + patch.object(ws_client_module, 'WEBSOCKET_PING_INTERVAL', 1): + client = self._make_client(mock_ws) client.update(timeout=5) self.assertTrue(remote_process_running)