Skip to content

Commit d7ec65d

Browse files
remove extra await when calling ws_connect()
The ClientSession.ws_connect() method is synchronous and returns a _RequestContextManager which takes a coroutine as parameter (here, ClientSession._ws_connect()). This context manager is in charge of closing the connection in its __aexit__() method, so it has to be used with "async with". However, this context manager can also be awaited as it has an __await__() method. In this case, it will await the _ws_connect() coroutine. This is what is done in the current code, but the connection will not be released. Remove the "await" to return the context manager, so that the user can use it with "async with", which will properly release resources. This is the documented way of using ws_connect(): https://docs.aiohttp.org/en/stable/client_quickstart.html#websockets Signed-off-by: Olivier Matz <[email protected]>
1 parent 6ff3ed2 commit d7ec65d

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

examples/pod_exec.py

+27-27
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ async def main():
100100
async with WsApiClient() as ws_api:
101101
v1_ws = client.CoreV1Api(api_client=ws_api)
102102
exec_command = ['/bin/sh']
103-
ws = await v1_ws.connect_get_namespaced_pod_exec(
103+
websocket = await v1_ws.connect_get_namespaced_pod_exec(
104104
BUSYBOX_POD,
105105
"default",
106106
command=exec_command,
@@ -117,32 +117,32 @@ async def main():
117117
]
118118
error_data = ""
119119
closed = False
120-
while commands and not closed:
121-
command = commands.pop(0)
122-
stdin_channel_prefix = chr(0)
123-
await ws.send_bytes((stdin_channel_prefix + command).encode("utf-8"))
124-
while True:
125-
try:
126-
msg = await ws.receive(timeout=1)
127-
except asyncio.TimeoutError:
128-
break
129-
if msg.type in (WSMsgType.CLOSE, WSMsgType.CLOSING, WSMsgType.CLOSED):
130-
closed = True
131-
break
132-
channel = msg.data[0]
133-
data = msg.data[1:].decode("utf-8")
134-
if not data:
135-
continue
136-
if channel == STDOUT_CHANNEL:
137-
print(f"stdout: {data}")
138-
elif channel == STDERR_CHANNEL:
139-
print(f"stderr: {data}")
140-
elif channel == ERROR_CHANNEL:
141-
error_data += data
142-
if error_data:
143-
returncode = ws_api.parse_error_data(error_data)
144-
print(f"Exit code: {returncode}")
145-
await ws.close()
120+
async with websocket as ws:
121+
while commands and not closed:
122+
command = commands.pop(0)
123+
stdin_channel_prefix = chr(0)
124+
await ws.send_bytes((stdin_channel_prefix + command).encode("utf-8"))
125+
while True:
126+
try:
127+
msg = await ws.receive(timeout=1)
128+
except asyncio.TimeoutError:
129+
break
130+
if msg.type in (WSMsgType.CLOSE, WSMsgType.CLOSING, WSMsgType.CLOSED):
131+
closed = True
132+
break
133+
channel = msg.data[0]
134+
data = msg.data[1:].decode("utf-8")
135+
if not data:
136+
continue
137+
if channel == STDOUT_CHANNEL:
138+
print(f"stdout: {data}")
139+
elif channel == STDERR_CHANNEL:
140+
print(f"stderr: {data}")
141+
elif channel == ERROR_CHANNEL:
142+
error_data += data
143+
if error_data:
144+
returncode = ws_api.parse_error_data(error_data)
145+
print(f"Exit code: {returncode}")
146146

147147
if __name__ == "__main__":
148148
loop = asyncio.get_event_loop()

kubernetes_asyncio/stream/ws_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,4 @@ async def request(self, method, url, query_params=None, headers=None,
117117

118118
else:
119119

120-
return await self.rest_client.pool_manager.ws_connect(url, headers=headers, heartbeat=self.heartbeat)
120+
return self.rest_client.pool_manager.ws_connect(url, headers=headers, heartbeat=self.heartbeat)

0 commit comments

Comments
 (0)