python
runtime_error
ai_generated
partial
后台任务中的异常被忽略:RuntimeError('task failed')
Exception in background task ignored: RuntimeError('task failed')
ID: python/starlette-background-task-exception-swallowed
80%修复率
82%置信度
0证据数
2024-12-04首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 0.30+ | active | — | — | — |
根因分析
BackgroundTask 在响应发送后抛出异常;Starlette 记录了它,但客户端已经收到 200。
English
A BackgroundTask raised an exception after the response was sent; Starlette logs it but the client already received 200.
解决方案
-
85% 成功率
import logging logger = logging.getLogger(__name__) def task(): try: do_work() except Exception: logger.exception('background failed') raise -
90% 成功率
from celery import Celery celery = Celery('app') celery.send_task('do_work')
无效尝试
常见但无效的做法:
-
80% 失败
Hides failures from monitoring; no retries or alerts.
-
85% 失败
Background tasks run after the response; the route already returned.