Commit 3f49877
committed
Fix voice STT task cleanup to properly await cancelled tasks
Problem:
The _cleanup_tasks() method in OpenAISTTTranscriptionSession was only calling
task.cancel() on pending tasks (listener, process_events, stream_audio, connection)
but not awaiting them. This could lead to:
1. Unhandled task exception warnings
2. Potential resource leaks (websocket connections, file descriptors)
3. Improper cleanup of background tasks
Evidence:
- Similar to recently fixed guardrail tasks cleanup (PR openai#1976)
- Similar to fixed websocket task cleanup (PR openai#1955)
- asyncio best practices require awaiting cancelled tasks
Solution:
1. Made _cleanup_tasks() async
2. Collect all real asyncio.Task objects that need to be awaited
3. Added await asyncio.gather() with return_exceptions=True to properly
collect exceptions from cancelled tasks
4. Updated close() method to await _cleanup_tasks()
Testing:
- All existing voice/STT tests pass (17 passed)
- Uses isinstance check to support mock objects in tests
- Follows the same pattern as PR openai#1976 and PR openai#19551 parent 8c4d4d0 commit 3f49877
1 file changed
+21
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
321 | 321 | | |
322 | 322 | | |
323 | 323 | | |
324 | | - | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
325 | 332 | | |
326 | 333 | | |
| 334 | + | |
| 335 | + | |
327 | 336 | | |
328 | 337 | | |
329 | 338 | | |
| 339 | + | |
| 340 | + | |
330 | 341 | | |
331 | 342 | | |
332 | 343 | | |
| 344 | + | |
| 345 | + | |
333 | 346 | | |
334 | 347 | | |
335 | 348 | | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
336 | 355 | | |
337 | 356 | | |
338 | 357 | | |
| |||
367 | 386 | | |
368 | 387 | | |
369 | 388 | | |
370 | | - | |
| 389 | + | |
371 | 390 | | |
372 | 391 | | |
373 | 392 | | |
| |||
0 commit comments