From 62f5fde0335d6db9f26dcd775bf374bfa1af143c Mon Sep 17 00:00:00 2001 From: Nikolai Khlopkov Date: Mon, 16 Nov 2026 03:23:45 +0300 Subject: [PATCH] feat: let middlewares declare task-only labels The kicker hardcoded the `types_of_exceptions` label name to keep it off the wire. That leaks middleware-specific knowledge into core code. Add a `task_only_labels` declaration to TaskiqMiddleware. Labels named there should be read only from the registered task and are skipped when kicker builds a message, so they are not serialized. --- taskiq/abc/middleware.py | 14 ++++++ taskiq/kicker.py | 11 ++-- taskiq/middlewares/simple_retry_middleware.py | 11 ++++ taskiq/middlewares/smart_retry_middleware.py | 11 ++++ tests/test_kicker.py | 50 +++++++++++++++---- 5 files changed, 82 insertions(+), 15 deletions(-) diff --git a/taskiq/abc/middleware.py b/taskiq/abc/middleware.py index cba52e16..c0294869 100644 --- a/taskiq/abc/middleware.py +++ b/taskiq/abc/middleware.py @@ -1,4 +1,5 @@ from collections.abc import Coroutine +from collections.abc import Set as AbstractSet from types import CoroutineType from typing import TYPE_CHECKING, Any, Union @@ -140,3 +141,16 @@ def on_error( :param result: returned value. :param exception: found exception. """ + + @classmethod + def task_only_labels(cls) -> AbstractSet[str]: + """ + Returns labels that should not be sent over the wire. + + Implementations can add labels here when their values are not suitable + for serialization. The labels remain on the registered task but are + omitted from outgoing messages. + + :return: Names of the task-only labels. + """ + return frozenset() diff --git a/taskiq/kicker.py b/taskiq/kicker.py index 07f4e51a..bb10ef89 100644 --- a/taskiq/kicker.py +++ b/taskiq/kicker.py @@ -323,12 +323,13 @@ def _prepare_message( for kwarg_name, kwarg_val in kwargs.items(): formatted_kwargs[kwarg_name] = self._prepare_arg(kwarg_val) + task_only_labels = { + label + for middleware in self.broker.middlewares + for label in middleware.task_only_labels() + } for label, label_val in self.labels.items(): - # `types_of_exceptions` is only ever read back from the - # locally registered task's labels (see retry middlewares), - # never from the wire. It holds exception classes, which - # can't be faithfully serialized, so don't ship it at all. - if label == "types_of_exceptions": + if label in task_only_labels: continue labels[label], labels_types[label] = prepare_label(label_val) diff --git a/taskiq/middlewares/simple_retry_middleware.py b/taskiq/middlewares/simple_retry_middleware.py index 4c2f9a45..e6792c84 100644 --- a/taskiq/middlewares/simple_retry_middleware.py +++ b/taskiq/middlewares/simple_retry_middleware.py @@ -1,4 +1,5 @@ from collections.abc import Iterable +from collections.abc import Set as AbstractSet from logging import getLogger from typing import Any @@ -114,3 +115,13 @@ async def on_error( "Task '%s' invocation failed. Maximum retries count is reached.", message.task_name, ) + + @classmethod + def task_only_labels(cls) -> AbstractSet[str]: + """ + Adds ``types_of_exceptions`` to the set of task-only labels. + + Per-task ``types_of_exceptions`` set via the task decorator is read + from the registered task object and cannot be properly serialized. + """ + return super().task_only_labels() | {"types_of_exceptions"} diff --git a/taskiq/middlewares/smart_retry_middleware.py b/taskiq/middlewares/smart_retry_middleware.py index 9752b07f..bc50bb51 100644 --- a/taskiq/middlewares/smart_retry_middleware.py +++ b/taskiq/middlewares/smart_retry_middleware.py @@ -1,6 +1,7 @@ import datetime import random from collections.abc import Iterable +from collections.abc import Set as AbstractSet from logging import getLogger from typing import Any @@ -213,3 +214,13 @@ async def on_error( "Task '%s' invocation failed. Maximum retries count is reached.", message.task_name, ) + + @classmethod + def task_only_labels(cls) -> AbstractSet[str]: + """ + Adds ``types_of_exceptions`` to the set of task-only labels. + + Per-task ``types_of_exceptions`` set via the task decorator is read + from the registered task object and cannot be properly serialized. + """ + return super().task_only_labels() | {"types_of_exceptions"} diff --git a/tests/test_kicker.py b/tests/test_kicker.py index 05afc0cb..6f3b1072 100644 --- a/tests/test_kicker.py +++ b/tests/test_kicker.py @@ -1,14 +1,30 @@ +from collections.abc import Set as AbstractSet from typing import Any -from taskiq import InMemoryBroker +from taskiq import InMemoryBroker, SimpleRetryMiddleware +from taskiq.abc.middleware import TaskiqMiddleware from taskiq.kicker import AsyncKicker -async def test_types_of_exceptions_not_serialized() -> None: - """`types_of_exceptions` label should never be sent over the wire.""" - broker = InMemoryBroker() +class _TaskOnlyLabelMiddleware(TaskiqMiddleware): + """Middleware that declares a label as task-only (never goes on wire).""" - @broker.task(types_of_exceptions=(ValueError, TypeError)) + @classmethod + def task_only_labels(cls) -> AbstractSet[str]: + return super().task_only_labels() | {"label_not_on_wire"} + + +async def test_task_only_labels_not_serialized() -> None: + """Labels doesn't go on wire when corresponding middlewares are registered.""" + broker = InMemoryBroker().with_middlewares( + SimpleRetryMiddleware(), + _TaskOnlyLabelMiddleware(), + ) + + @broker.task( + types_of_exceptions=(ValueError, TypeError), + label_not_on_wire="test_label_value", + ) async def run_task() -> None: pass @@ -17,26 +33,40 @@ async def run_task() -> None: assert "types_of_exceptions" not in message.labels assert "types_of_exceptions" not in (message.labels_types or {}) + assert "label_not_on_wire" not in message.labels + assert "label_not_on_wire" not in (message.labels_types or {}) -async def test_types_of_exceptions_still_local_on_task() -> None: - """The registered task still keeps the real exception types locally.""" - broker = InMemoryBroker() +async def test_task_only_labels_still_local_on_task() -> None: + """The registered task still keeps the real label values locally.""" + broker = InMemoryBroker().with_middlewares( + SimpleRetryMiddleware(), + _TaskOnlyLabelMiddleware(), + ) - @broker.task(types_of_exceptions=(ValueError, TypeError)) + @broker.task( + types_of_exceptions=(ValueError, TypeError), + label_not_on_wire="test_label_value", + ) async def run_task() -> None: pass task = broker.find_task(run_task.task_name) assert task is not None assert task.labels["types_of_exceptions"] == (ValueError, TypeError) + assert task.labels["label_not_on_wire"] == "test_label_value" async def test_other_labels_still_serialized() -> None: """Unrelated labels are unaffected by the fix.""" + broker = InMemoryBroker().with_middlewares( + SimpleRetryMiddleware(), + _TaskOnlyLabelMiddleware(), + ) + kicker: AsyncKicker[Any, Any] = AsyncKicker( task_name="some_task", - broker=InMemoryBroker(), + broker=broker, labels={"retries": 3, "queue": "high_priority"}, ) message = kicker._prepare_message()