Skip to content

Commit 0b03a53

Browse files
committed
fix: await cancelled tasks for cleanup, reduce WS close_timeout to 2s
1 parent b79a30a commit 0b03a53

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

centml/cli/shell.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ def _on_stdin_ready():
120120
done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
121121
for t in pending:
122122
t.cancel()
123+
# Await cancelled tasks so their finally blocks run (e.g. remove_reader).
124+
for t in pending:
125+
try:
126+
await t
127+
except (asyncio.CancelledError, Exception):
128+
pass
123129
for t in done:
124130
if t.exception() is not None:
125131
raise t.exception()
@@ -138,7 +144,7 @@ async def _interactive_session(ws_url, token):
138144
rows, cols = shutil.get_terminal_size()
139145

140146
headers = {"Authorization": f"Bearer {token}"}
141-
async with websockets.connect(ws_url, additional_headers=headers) as ws:
147+
async with websockets.connect(ws_url, additional_headers=headers, close_timeout=2) as ws:
142148
await ws.send(json.dumps({"operation": "resize", "rows": rows, "cols": cols}))
143149

144150
loop = asyncio.get_running_loop()
@@ -188,7 +194,7 @@ async def _exec_session(ws_url, token, command):
188194
rows, cols = shutil.get_terminal_size(fallback=(80, 24))
189195
headers = {"Authorization": f"Bearer {token}"}
190196

191-
async with websockets.connect(ws_url, additional_headers=headers) as ws:
197+
async with websockets.connect(ws_url, additional_headers=headers, close_timeout=2) as ws:
192198
await ws.send(json.dumps({"operation": "resize", "rows": rows, "cols": cols}))
193199

194200
# Suppress echo/bracketed-paste, emit begin marker, run command,

0 commit comments

Comments
 (0)