Skip to content

Events are not received when dispatch() occurs in an asyncio.Task #34

Description

@smithk86

I banged my head against the wall with this one for a while before I found the problem.

Steps to reproduce:

  1. Request is made to FastAPI which dispatches a starting X process. The request immediately returns a payload letting the client know the asynchronous process has started.
  2. The actual work is then done in an asyncio.Task which then dispatches a X process is complete afterwords.
  3. Any dispatched events in the Task are never received

What is actually happening:

The issue is on dispatcher.py line 57. Starting in Python 3.7, asyncio.Task copies the current context from contextvars into the Task. When line 57 is reached, the code is told the event will be dispatched in the middleware as part of the request since the request was active at the time the Task was created. In actuality, these events end up in the void as they should have been dispatched via _dispatch_as_task.

For now, anywhere an event needs to be dispatch within a Task, I import fastapi_events.in_req_res_cycle into the code and run in_req_res_cycle.set(None). This forces _dispatch() to process these events via _dispatch_as_task.

Edit: updated the link to include the specific commit revision

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions