python runtime_error ai_generated true

Exception in thread Thread-1 (run) ... AttributeError: 'NoneType' object has no attribute 'send'

ID: python/fastapi-background-task-exception-silent

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-09-07First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.100.x active — — —
0.110.x active — — —

Root Cause

A background task raised an exception after the response was sent; Starlette logs it but the client never sees it.

generic

中文

后台任务在响应发送后抛出异常;Starlette会记录但客户端看不到。

Workarounds

  1. 90% success
    def task():\n    try:\n        work()\n    except Exception:\n        logger.exception('bg failed')
  2. 92% success
    @celery.task\ndef task(): ...\ntask.delay()

Dead Ends

Common approaches that don't work:

  1. 85% fail

    Background tasks cannot return responses to the client.

  2. 70% fail

    Still runs after response; exceptions remain invisible to client.