Skip to content

Commit

Permalink
fix: add connect timeout for exec websockets to avoid hanging
Browse files Browse the repository at this point in the history
  • Loading branch information
benhoyt committed Jun 5, 2024
1 parent e0d5a05 commit 6dc1fd9
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ops/pebble.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 6dc1fd9

Please sign in to comment.