From 6dc1fd9d0aacca8ad027e1ee8c4a0ba0a799755a Mon Sep 17 00:00:00 2001 From: Ben Hoyt Date: Wed, 5 Jun 2024 16:10:54 +1200 Subject: [PATCH 1/2] fix: add connect timeout for exec websockets to avoid hanging --- ops/pebble.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ops/pebble.py b/ops/pebble.py index 831c778a0..b8231a9f7 100644 --- a/ops/pebble.py +++ b/ops/pebble.py @@ -2832,10 +2832,17 @@ def _cancel_stdin(): def _connect_websocket(self, task_id: str, websocket_id: str) -> '_WebSocket': sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + # Set socket timeout to a short timeout during connection phase, in + # case Pebble side times out (5s), so this side doesn't hang. See: + # https://github.com/canonical/operator/issues/1246 + sock.settimeout(self.timeout) sock.connect(self.socket_path) url = self._websocket_url(task_id, websocket_id) ws: _WebSocket = websocket.WebSocket(skip_utf8_validation=True) # type: ignore ws.connect(url, socket=sock) + # Reset to no timeout so connection can be "long polling" when data is + # being received. + sock.settimeout(None) return ws def _websocket_url(self, task_id: str, websocket_id: str) -> str: From 108a13725a05c5442070d8c591588eb8b39e557c Mon Sep 17 00:00:00 2001 From: Ben Hoyt Date: Fri, 7 Jun 2024 10:12:35 +1200 Subject: [PATCH 2/2] Update ops/pebble.py Co-authored-by: Tony Meyer --- ops/pebble.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ops/pebble.py b/ops/pebble.py index b8231a9f7..bc4c0328f 100644 --- a/ops/pebble.py +++ b/ops/pebble.py @@ -2833,7 +2833,7 @@ def _cancel_stdin(): def _connect_websocket(self, task_id: str, websocket_id: str) -> '_WebSocket': sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) # Set socket timeout to a short timeout during connection phase, in - # case Pebble side times out (5s), so this side doesn't hang. See: + # case the Pebble side times out (5s), so this side doesn't hang. See: # https://github.com/canonical/operator/issues/1246 sock.settimeout(self.timeout) sock.connect(self.socket_path)