Commit a52d0c9
committed
Fix voice result streaming task cleanup to properly await cancelled tasks
Problem:
The _cleanup_tasks() method in VoiceStreamResult was only calling
task.cancel() on pending tasks but not awaiting them. Additionally,
_check_errors() could raise CancelledError when checking cancelled tasks.
This could lead to:
1. Unhandled task exception warnings
2. Potential resource leaks from abandoned tasks
3. CancelledError masking real exceptions
Evidence:
- Similar to fixed guardrail tasks cleanup (PR openai#1976)
- Similar to fixed voice STT cleanup (PR openai#1977)
- Similar to fixed websocket cleanup (PR openai#1955)
- Bug documented in .claude/bug-analysis/03-resource-leaks.md
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 _check_errors() to skip cancelled tasks using task.cancelled()
check to avoid CancelledError when calling task.exception()
5. Updated stream() async generator to await _cleanup_tasks()
Testing:
- Linting passes
- No breaking changes to public API
- Follows same pattern as PR openai#1976, openai#1977, openai#19551 parent 03dca68 commit a52d0c9
1 file changed
+31
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
243 | 243 | | |
244 | 244 | | |
245 | 245 | | |
246 | | - | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
247 | 252 | | |
248 | 253 | | |
| 254 | + | |
249 | 255 | | |
250 | 256 | | |
251 | 257 | | |
| 258 | + | |
| 259 | + | |
252 | 260 | | |
253 | 261 | | |
254 | 262 | | |
| 263 | + | |
| 264 | + | |
255 | 265 | | |
256 | 266 | | |
257 | 267 | | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
258 | 274 | | |
259 | 275 | | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
260 | 280 | | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
265 | 290 | | |
266 | 291 | | |
267 | 292 | | |
| |||
281 | 306 | | |
282 | 307 | | |
283 | 308 | | |
284 | | - | |
| 309 | + | |
285 | 310 | | |
286 | 311 | | |
287 | 312 | | |
0 commit comments