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

fromtaskiqimportInMemoryBroker, TaskiqMessagefromtaskiq.middlewaresimportSimpleRetryMiddlewarefromtaskiq.abc.middlewareimportTaskiqMiddlewareclassTaskLoggingMiddleware(TaskiqMiddleware):
asyncdef_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=Truepassasyncdefpost_send(self, message: TaskiqMessage) ->None:
awaitself._log_event(message, "SENT")
asyncdefpre_execute(self, message: TaskiqMessage) ->TaskiqMessage:
awaitself._log_event(message, "STARTED")
returnmessagebroker=InMemoryBroker(await_inplace=True).with_middlewares(
TaskLoggingMiddleware(),
SimpleRetryMiddleware(default_retry_count=3),
)

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