Skip to content

post_send fires after task execution with await_inplace=True #586

Description

@mr-tabasco

Taskiq version

0.11.20

Python version

Python 3.13

OS

macos 26.2

What happened?

When using InMemoryBroker(await_inplace=True), the post_send hook fires after the task completes instead of after the message is sent.

Current behavior:
pre_send → pre_execute → task → post_execute → post_send

Expected behavior:
pre_send → post_send → pre_execute → task → post_execute

This breaks the semantic meaning of "post send". If I'm logging "task queued" in post_send, it ends up with a later timestamp than "task started" in pre_execute.

Is this intentional? If so, might be worth documenting. If not, the broker could call post_send before invoking the task inline.

Relevant log output

Broker initialization code

from taskiq import InMemoryBroker, TaskiqMessage
  from taskiq.middlewares import SimpleRetryMiddleware
  from taskiq.abc.middleware import TaskiqMiddleware

  class TaskLoggingMiddleware(TaskiqMiddleware):
      async def _log_event(self, message: TaskiqMessage, status: str) -> None:
          # Inserts row to DB with created_at = now()
          # Problem: SENT rows end up with later timestamps than STARTED rows
          # because post_send fires after task execution with await_inplace=True
          pass

      async def post_send(self, message: TaskiqMessage) -> None:
          await self._log_event(message, "SENT")

      async def pre_execute(self, message: TaskiqMessage) -> TaskiqMessage:
          await self._log_event(message, "STARTED")
          return message

  broker = InMemoryBroker(await_inplace=True).with_middlewares(
      TaskLoggingMiddleware(),
      SimpleRetryMiddleware(default_retry_count=3),
  )

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions