Skip to content

Commit c5544ee

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 36e2dee commit c5544ee

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

145145
if __name__ == "__main__":
146146
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)