forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_tasks.py
More file actions
490 lines (393 loc) · 16.6 KB
/
Copy pathasync_tasks.py
File metadata and controls
490 lines (393 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
"""Async concurrency utilities for production WebSocket and HTTP fan-out patterns.
Implements structured concurrency primitives that replace raw asyncio.gather():
- supervise_tasks(): FIRST_COMPLETED supervisor loop for WS handlers
- drain_tasks(): timeout-bounded task cancellation
- gather_safe(): bounded fan-out with exception observability
- gather_chunked(): chunked fan-out for large coroutine lists
- create_named_task(): tracked task creation with done-callback cleanup
- wait_for_event(): interruptible sleep that wakes on shutdown event
These replace ad-hoc asyncio.gather() patterns that cause:
1. Silent exception swallowing (return_exceptions=True with no inspection)
2. Orphaned tasks on first failure (gather without sibling cancellation)
3. Unbounded fan-out (50+ concurrent outbound connections per session)
Same pattern as utils/executors.py (thread pools) and utils/http_client.py (HTTP clients):
named utilities with bounded resources, clean shutdown, and observability.
"""
import asyncio
import logging
import sys
import threading
from dataclasses import dataclass
from types import ModuleType
from typing import Any, Awaitable, Coroutine, Dict, Generic, Iterable, List, TypeVar, cast
from prometheus_client import Counter, Histogram
logger = logging.getLogger(__name__)
T = TypeVar('T')
# ---------------------------------------------------------------------------
# Metrics
# ---------------------------------------------------------------------------
_METRIC_CACHE_MODULE = 'utils._async_tasks_metric_cache'
def _new_metric_cache_module() -> Any:
module: Any = ModuleType(_METRIC_CACHE_MODULE)
module.cache = {}
module.lock = threading.Lock()
return module
def _metric_cache_state() -> Any:
state: Any = sys.modules.get(_METRIC_CACHE_MODULE)
if state is None:
state = sys.modules.setdefault(_METRIC_CACHE_MODULE, _new_metric_cache_module())
return state
def _metric_cache() -> Any: # type: ignore[reportUnusedFunction] # exercised by tests/unit/test_async_tasks.py
return _metric_cache_state().cache
def _cacheable_value(value: Any) -> Any:
if isinstance(value, list):
items: List[Any] = cast(List[Any], value)
return tuple(_cacheable_value(item) for item in items)
if isinstance(value, dict):
entries: Dict[str, Any] = cast(Dict[str, Any], value)
return tuple(sorted((key, _cacheable_value(item)) for key, item in entries.items()))
return value
def _metric_cache_key(metric_class: Any, name: str, labelnames: Any = (), **kwargs: Any) -> Any:
return (
metric_class,
name,
tuple(labelnames),
tuple(sorted((key, _cacheable_value(value)) for key, value in kwargs.items())),
)
def _get_or_create_metric(metric_class: Any, name: str, documentation: str, labelnames: Any = (), **kwargs: Any) -> Any:
state = _metric_cache_state()
cache_key = _metric_cache_key(metric_class, name, labelnames, **kwargs)
with state.lock:
cache = state.cache
existing = cache.get(cache_key)
if existing is not None:
return existing
metric = metric_class(name, documentation, labelnames, **kwargs)
cache[cache_key] = metric
return metric
SUPERVISOR_EXIT_TOTAL = _get_or_create_metric(
Counter,
'async_supervisor_exit_total',
'Supervisor loop exits by reason',
['label', 'reason'],
)
DRAIN_TIMEOUT_TOTAL = _get_or_create_metric(
Counter,
'async_drain_timeout_total',
'Task drain operations that hit timeout',
['label'],
)
DRAIN_DURATION = _get_or_create_metric(
Histogram,
'async_drain_duration_seconds',
'Time spent draining tasks',
['label'],
buckets=[0.1, 0.5, 1.0, 2.0, 5.0, 10.0, 30.0, 60.0],
)
GATHER_FAILURES_TOTAL = _get_or_create_metric(
Counter,
'async_gather_failures_total',
'Individual coroutine failures in gather_safe',
['label'],
)
GATHER_DURATION = _get_or_create_metric(
Histogram,
'async_gather_duration_seconds',
'Total duration of gather_safe calls',
['label'],
buckets=[0.01, 0.05, 0.1, 0.5, 1.0, 5.0, 10.0, 30.0],
)
# ---------------------------------------------------------------------------
# Data types
# ---------------------------------------------------------------------------
@dataclass(slots=True)
class GatherResult(Generic[T]):
"""Result of a single coroutine in gather_safe."""
index: int
ok: bool
value: T | None = None
exception: BaseException | None = None
cancelled: bool = False
@dataclass(slots=True)
class SupervisorResult:
"""Result of supervise_tasks indicating why the supervisor loop exited."""
reason: str # "disconnect", "crash", "lifetime_done"
task_name: str
exception: BaseException | None = None
class WebSocketTaskSupervisor:
"""Session-scoped task owner for long-lived WebSocket handlers.
The supervisor centralizes the invariants each handler used to maintain by
convention: active gauge pairing, ws:{uid}:{name} task names, finite-vs-
lifetime classification, FIRST_COMPLETED supervision, and bounded drain.
"""
def __init__(self, *, uid: str, label: str, gauge: Any | None = None):
self.uid = uid
self.label = label
self.gauge = gauge
self.shutdown_event = asyncio.Event()
self._tracked_tasks: set[asyncio.Task[Any]] = set()
self._monitored_tasks: list[asyncio.Task[Any]] = []
self._finite_tasks: set[asyncio.Task[Any]] = set()
self._session_started = False
def start_session(self) -> None:
if self._session_started:
return
if self.gauge is not None:
self.gauge.inc()
self._session_started = True
def end_session(self) -> None:
if not self._session_started:
return
self.shutdown_event.set()
if self.gauge is not None:
self.gauge.dec()
self._session_started = False
def create_task(
self, coro: Coroutine[Any, Any, Any], *, name: str, monitor: bool = False, finite: bool = False
) -> asyncio.Task[Any]:
if name.startswith("ws:"):
raise ValueError("WebSocket task names must be logical names, not preformatted ws:* names")
task = create_named_task(coro, name=f"ws:{self.uid}:{name}", task_set=self._tracked_tasks)
task.add_done_callback(self._log_unhandled_exception)
if monitor:
self._monitored_tasks.append(task)
if finite:
self._finite_tasks.add(task)
return task
def create_lifetime_task(self, coro: Coroutine[Any, Any, Any], *, name: str) -> asyncio.Task[Any]:
return self.create_task(coro, name=name, monitor=True)
def create_finite_task(self, coro: Coroutine[Any, Any, Any], *, name: str) -> asyncio.Task[Any]:
return self.create_task(coro, name=name, monitor=True, finite=True)
async def supervise(self, *, receive_task: asyncio.Task[Any]) -> SupervisorResult:
return await supervise_tasks(
receive_task=receive_task,
bg_tasks=list(self._monitored_tasks),
finite_tasks=set(self._finite_tasks),
label=self.label,
)
async def drain_monitored(self, *, timeout: float, cancel: bool = False) -> int:
return await drain_tasks(self._monitored_tasks, timeout=timeout, label=f"{self.label}_bg", cancel=cancel)
async def drain_all(self, *, timeout: float, cancel: bool = True) -> int:
return await drain_tasks(
list(self._tracked_tasks), timeout=timeout, label=f"{self.label}_cleanup", cancel=cancel
)
@property
def monitored_tasks(self) -> list[asyncio.Task[Any]]:
return list(self._monitored_tasks)
def _log_unhandled_exception(self, task: asyncio.Task[Any]) -> None:
if task.cancelled():
return
try:
exc = task.exception()
except asyncio.CancelledError:
return
if exc is not None:
logger.error("Unhandled exception in WebSocket task %s [%s]: %r", task.get_name(), self.label, exc)
# ---------------------------------------------------------------------------
# supervise_tasks — WebSocket task supervision
# ---------------------------------------------------------------------------
async def supervise_tasks(
*,
receive_task: asyncio.Task[Any],
bg_tasks: list[asyncio.Task[Any]],
finite_tasks: set[asyncio.Task[Any]] | None = None,
label: str,
) -> SupervisorResult:
"""Supervisor loop using asyncio.wait(FIRST_COMPLETED).
Monitors receive_task + bg_tasks. Exits when:
- receive_task completes (disconnect)
- Any task raises an exception (crash)
- A lifetime task (not in finite_tasks) completes normally (session ending)
Re-waits when a finite task completes normally.
"""
if finite_tasks is None:
finite_tasks = set()
monitored = {receive_task, *bg_tasks}
if not monitored:
return SupervisorResult(reason="empty", task_name="none")
while monitored:
done, monitored = await asyncio.wait(monitored, return_when=asyncio.FIRST_COMPLETED)
exit_result = None
if receive_task in done:
# The client disconnect owns the exit reason for the round it lands in.
# `done` is a set, so scanning it in iteration order let a bg task that
# observed the same disconnect report "crash" on ordering luck alone.
exit_result = SupervisorResult(reason="disconnect", task_name=receive_task.get_name())
else:
for task in done:
if task.cancelled():
continue
try:
exc = task.exception()
except asyncio.CancelledError:
continue
if exc is not None:
logger.error("BG task %s crashed: %r [%s]", task.get_name(), exc, label)
exit_result = SupervisorResult(reason="crash", task_name=task.get_name(), exception=exc)
break
if task not in finite_tasks:
logger.info("Lifetime task %s completed, tearing down [%s]", task.get_name(), label)
exit_result = SupervisorResult(reason="lifetime_done", task_name=task.get_name())
break
if exit_result:
SUPERVISOR_EXIT_TOTAL.labels(label=label, reason=exit_result.reason).inc()
return exit_result
return SupervisorResult(reason="all_done", task_name="none")
# ---------------------------------------------------------------------------
# drain_tasks — timeout-bounded cancellation
# ---------------------------------------------------------------------------
async def drain_tasks(
tasks: Iterable[asyncio.Task[Any]],
*,
timeout: float = 30.0,
label: str = "drain",
cancel: bool = True,
) -> int:
"""Cancel and wait for tasks to finish within timeout.
Returns the number of tasks that had to be force-cancelled after timeout.
"""
pending = [t for t in tasks if not t.done()]
if not pending:
return 0
if cancel:
for task in pending:
task.cancel()
with DRAIN_DURATION.labels(label=label).time():
done, still_pending = await asyncio.wait(pending, timeout=timeout)
for task in done:
if not task.cancelled():
try:
exc = task.exception()
except asyncio.CancelledError:
continue
if exc is not None:
logger.debug("Task %s raised during drain [%s]: %r", task.get_name(), label, exc)
force_cancelled = 0
if still_pending:
DRAIN_TIMEOUT_TOTAL.labels(label=label).inc()
logger.warning("Drain timeout (%.1fs), force-cancelling %d tasks [%s]", timeout, len(still_pending), label)
for task in still_pending:
task.cancel()
# Bounded wait for cancel acknowledgement — never block indefinitely
_, truly_stuck = await asyncio.wait(still_pending, timeout=5.0)
force_cancelled = len(still_pending)
if truly_stuck:
logger.error(
"drain_tasks: %d tasks ignored cancellation after 5s [%s]: %s",
len(truly_stuck),
label,
[t.get_name() for t in truly_stuck],
)
return force_cancelled
# ---------------------------------------------------------------------------
# gather_safe — bounded fan-out with observability
# ---------------------------------------------------------------------------
async def gather_safe(
*coros: Awaitable[T],
label: str,
max_concurrency: int = 10,
timeout: float | None = None,
) -> list[GatherResult[T]]:
"""Fan-out coroutines with bounded concurrency and exception logging.
- Semaphore limits concurrent execution
- Exceptions are logged (not silently swallowed)
- Returns GatherResult list preserving order
- Cancellation of parent propagates to all children
"""
if not coros:
return []
sem = asyncio.Semaphore(max_concurrency)
async def _run_one(index: int, coro: Awaitable[T]) -> GatherResult[T]:
async with sem:
try:
if timeout is not None:
value = await asyncio.wait_for(coro, timeout=timeout)
else:
value = await coro
return GatherResult(index=index, ok=True, value=value)
except asyncio.CancelledError:
return GatherResult(index=index, ok=False, cancelled=True)
except Exception as e:
GATHER_FAILURES_TOTAL.labels(label=label).inc()
logger.warning("gather_safe[%s] item %d failed: %r", label, index, e)
return GatherResult(index=index, ok=False, exception=e)
tasks = [asyncio.create_task(_run_one(i, coro), name=f"{label}:{i}") for i, coro in enumerate(coros)]
try:
with GATHER_DURATION.labels(label=label).time():
results = await asyncio.gather(*tasks)
return list(results)
except asyncio.CancelledError:
await drain_tasks(tasks, timeout=5.0, label=f"{label}:cancel", cancel=True)
raise
# ---------------------------------------------------------------------------
# gather_chunked — chunked fan-out for large lists
# ---------------------------------------------------------------------------
async def gather_chunked(
coros: Iterable[Awaitable[T]],
*,
chunk_size: int = 10,
label: str,
max_concurrency: int | None = None,
timeout: float | None = None,
) -> list[GatherResult[T]]:
"""Execute coroutines in chunks with bounded concurrency.
Processes chunk_size coroutines at a time, waiting for each chunk
to complete before starting the next. Useful for large fan-outs
where even semaphore-bounded gather creates too many pending tasks.
"""
results: list[GatherResult[T]] = []
chunk: list[Awaitable[T]] = []
concurrency = max_concurrency if max_concurrency is not None else chunk_size
for coro in coros:
chunk.append(coro)
if len(chunk) >= chunk_size:
chunk_results = await gather_safe(
*chunk,
label=label,
max_concurrency=concurrency,
timeout=timeout,
)
results.extend(chunk_results)
chunk = []
if chunk:
chunk_results = await gather_safe(
*chunk,
label=label,
max_concurrency=concurrency,
timeout=timeout,
)
results.extend(chunk_results)
return results
# ---------------------------------------------------------------------------
# create_named_task — tracked task creation
# ---------------------------------------------------------------------------
async def wait_for_event(event: asyncio.Event, seconds: float) -> bool:
"""Sleep for `seconds` but wake immediately if `event` is set.
Returns True if woken early by the event (shutdown requested),
False if the full sleep elapsed normally.
"""
if event.is_set():
return True
if seconds <= 0:
return False
try:
await asyncio.wait_for(event.wait(), timeout=seconds)
return True
except asyncio.TimeoutError:
return False
def create_named_task(
coro: Coroutine[Any, Any, Any],
*,
name: str,
task_set: set[asyncio.Task[Any]] | None = None,
) -> asyncio.Task[Any]:
"""Create a named task with optional tracking set.
- Names the task for debugging (visible in asyncio.all_tasks())
- Adds to task_set if provided
- Registers done callback to auto-remove from task_set
"""
task = asyncio.create_task(coro, name=name)
if task_set is not None:
task_set.add(task)
task.add_done_callback(task_set.discard)
return task