Skip to content

Commit c3a3128

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 9cb98d0 commit c3a3128

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

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

kubernetes_asyncio/stream/ws_client.py

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

109109
else:
110110

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

0 commit comments

Comments
 (0)